[READ-ONLY] Mirror of https://github.com/probablykasper/ferrum. Music library app for Mac, Linux and Windows ferrum.kasper.space
electron linux macos music music-library music-player napi windows
0

Configure Feed

Select the types of activity you want to include in your feed.

Move cover functions into cover.rs

Kasper (Sep 27, 2024, 7:42 AM +0200) 62b487df 729d7fcf

+43 -39
+40 -1
src-native/tracks/cover.rs
··· 1 - use super::Tag; 1 + use crate::data::Data; 2 + use crate::data_js::get_data; 3 + 4 + use super::{id_to_track, Tag}; 2 5 use anyhow::{anyhow, bail, Context, Result}; 3 6 use fast_image_resize::images::Image; 4 7 use fast_image_resize::{IntoImageView, Resizer}; ··· 7 10 use image::{ImageEncoder, ImageFormat, ImageReader}; 8 11 use lazy_static::lazy_static; 9 12 use napi::bindgen_prelude::Buffer; 13 + use napi::{Env, JsBuffer, JsObject, Task}; 10 14 use redb::{Database, TableDefinition}; 11 15 use std::fs; 12 16 use std::io::{BufWriter, Cursor}; ··· 213 217 }; 214 218 Ok(img_bytes) 215 219 } 220 + 221 + /// File path, artwork index 222 + struct ReadCover(PathBuf, usize); 223 + impl Task for ReadCover { 224 + type Output = Vec<u8>; 225 + type JsValue = JsBuffer; 226 + fn compute(&mut self) -> napi::Result<Self::Output> { 227 + let path = &self.0; 228 + let index = self.1; 229 + 230 + let tag = Tag::read_from_path(path)?; 231 + let image = match tag.get_image_consume(index)? { 232 + Some(image) => image, 233 + None => { 234 + return Err(anyhow!("No image").into()); 235 + } 236 + }; 237 + 238 + Ok(image.data) 239 + } 240 + fn resolve(&mut self, env: Env, output: Self::Output) -> napi::Result<Self::JsValue> { 241 + let result = env.create_buffer_copy(output)?; 242 + return Ok(result.into_raw()); 243 + } 244 + } 245 + #[napi(js_name = "read_cover_async", ts_return_type = "Promise<ArrayBuffer>")] 246 + #[allow(dead_code)] 247 + pub fn read_cover_async(track_id: String, index: u16, env: Env) -> napi::Result<JsObject> { 248 + let data: &mut Data = get_data(&env)?; 249 + let track = id_to_track(&env, &track_id)?; 250 + let tracks_dir = &data.paths.tracks_dir; 251 + let file_path = tracks_dir.join(&track.file); 252 + let task = ReadCover(file_path, index.into()); 253 + env.spawn(task).map(|t| t.promise_object()) 254 + }
+3 -38
src-native/tracks/mod.rs
··· 2 2 use crate::data_js::get_data; 3 3 use crate::get_now_timestamp; 4 4 use crate::library_types::{ItemId, MsSinceUnixEpoch, Track, TrackID, TRACK_ID_MAP}; 5 - use anyhow::{anyhow, bail, Context, Result}; 6 - use napi::{Env, JsArrayBuffer, JsBuffer, JsObject, Task}; 5 + use anyhow::{bail, Context, Result}; 6 + use napi::{Env, JsArrayBuffer, JsBuffer}; 7 7 use std::fs; 8 - use std::path::{Path, PathBuf}; 8 + use std::path::Path; 9 9 10 10 pub mod cover; 11 11 pub mod import; ··· 106 106 tracks.get(&id).context("Track ID not found")?; 107 107 data.library.playTime.push((id, start, dur_ms)); 108 108 Ok(()) 109 - } 110 - 111 - /// File path, artwork index 112 - struct ReadCover(PathBuf, usize); 113 - impl Task for ReadCover { 114 - type Output = Vec<u8>; 115 - type JsValue = JsBuffer; 116 - fn compute(&mut self) -> napi::Result<Self::Output> { 117 - let path = &self.0; 118 - let index = self.1; 119 - 120 - let tag = Tag::read_from_path(path)?; 121 - let image = match tag.get_image_consume(index)? { 122 - Some(image) => image, 123 - None => { 124 - return Err(anyhow!("No image").into()); 125 - } 126 - }; 127 - 128 - Ok(image.data) 129 - } 130 - fn resolve(&mut self, env: Env, output: Self::Output) -> napi::Result<Self::JsValue> { 131 - let result = env.create_buffer_copy(output)?; 132 - return Ok(result.into_raw()); 133 - } 134 - } 135 - #[napi(js_name = "read_cover_async", ts_return_type = "Promise<ArrayBuffer>")] 136 - #[allow(dead_code)] 137 - pub fn read_cover_async(track_id: String, index: u16, env: Env) -> napi::Result<JsObject> { 138 - let data: &mut Data = get_data(&env)?; 139 - let track = id_to_track(&env, &track_id)?; 140 - let tracks_dir = &data.paths.tracks_dir; 141 - let file_path = tracks_dir.join(&track.file); 142 - let task = ReadCover(file_path, index.into()); 143 - env.spawn(task).map(|t| t.promise_object()) 144 109 } 145 110 146 111 fn sanitize_filename(input: &String) -> String {