Skip to content

Commit

Permalink
Move cover functions into cover.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
probablykasper committed Sep 27, 2024
1 parent 4a28ba8 commit 721606d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
41 changes: 40 additions & 1 deletion src-native/tracks/cover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::Tag;
use crate::data::Data;
use crate::data_js::get_data;

use super::{id_to_track, Tag};
use anyhow::{anyhow, bail, Context, Result};
use fast_image_resize::images::Image;
use fast_image_resize::{IntoImageView, Resizer};
Expand All @@ -7,6 +10,7 @@ use image::codecs::png::PngEncoder;
use image::{ImageEncoder, ImageFormat, ImageReader};
use lazy_static::lazy_static;
use napi::bindgen_prelude::Buffer;
use napi::{Env, JsBuffer, JsObject, Task};
use redb::{Database, TableDefinition};
use std::fs;
use std::io::{BufWriter, Cursor};
Expand Down Expand Up @@ -213,3 +217,38 @@ fn to_resized_image(image_bytes: Vec<u8>, max_size: u32) -> Result<Vec<u8>> {
};
Ok(img_bytes)
}

/// File path, artwork index
struct ReadCover(PathBuf, usize);
impl Task for ReadCover {
type Output = Vec<u8>;
type JsValue = JsBuffer;
fn compute(&mut self) -> napi::Result<Self::Output> {
let path = &self.0;
let index = self.1;

let tag = Tag::read_from_path(path)?;
let image = match tag.get_image_consume(index)? {
Some(image) => image,
None => {
return Err(anyhow!("No image").into());
}
};

Ok(image.data)
}
fn resolve(&mut self, env: Env, output: Self::Output) -> napi::Result<Self::JsValue> {
let result = env.create_buffer_copy(output)?;
return Ok(result.into_raw());
}
}
#[napi(js_name = "read_cover_async", ts_return_type = "Promise<ArrayBuffer>")]
#[allow(dead_code)]
pub fn read_cover_async(track_id: String, index: u16, env: Env) -> napi::Result<JsObject> {
let data: &mut Data = get_data(&env)?;
let track = id_to_track(&env, &track_id)?;
let tracks_dir = &data.paths.tracks_dir;
let file_path = tracks_dir.join(&track.file);
let task = ReadCover(file_path, index.into());
env.spawn(task).map(|t| t.promise_object())
}
41 changes: 3 additions & 38 deletions src-native/tracks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::data::Data;
use crate::data_js::get_data;
use crate::get_now_timestamp;
use crate::library_types::{ItemId, MsSinceUnixEpoch, Track, TrackID, TRACK_ID_MAP};
use anyhow::{anyhow, bail, Context, Result};
use napi::{Env, JsArrayBuffer, JsBuffer, JsObject, Task};
use anyhow::{bail, Context, Result};
use napi::{Env, JsArrayBuffer, JsBuffer};
use std::fs;
use std::path::{Path, PathBuf};
use std::path::Path;

pub mod cover;
pub mod import;
Expand Down Expand Up @@ -108,41 +108,6 @@ pub fn add_play_time(id: TrackID, start: MsSinceUnixEpoch, dur_ms: i64, env: Env
Ok(())
}

/// File path, artwork index
struct ReadCover(PathBuf, usize);
impl Task for ReadCover {
type Output = Vec<u8>;
type JsValue = JsBuffer;
fn compute(&mut self) -> napi::Result<Self::Output> {
let path = &self.0;
let index = self.1;

let tag = Tag::read_from_path(path)?;
let image = match tag.get_image_consume(index)? {
Some(image) => image,
None => {
return Err(anyhow!("No image").into());
}
};

Ok(image.data)
}
fn resolve(&mut self, env: Env, output: Self::Output) -> napi::Result<Self::JsValue> {
let result = env.create_buffer_copy(output)?;
return Ok(result.into_raw());
}
}
#[napi(js_name = "read_cover_async", ts_return_type = "Promise<ArrayBuffer>")]
#[allow(dead_code)]
pub fn read_cover_async(track_id: String, index: u16, env: Env) -> napi::Result<JsObject> {
let data: &mut Data = get_data(&env)?;
let track = id_to_track(&env, &track_id)?;
let tracks_dir = &data.paths.tracks_dir;
let file_path = tracks_dir.join(&track.file);
let task = ReadCover(file_path, index.into());
env.spawn(task).map(|t| t.promise_object())
}

fn sanitize_filename(input: &String) -> String {
let mut string = input.replace('/', "_");
string = string.replace('?', "_");
Expand Down

0 comments on commit 721606d

Please sign in to comment.