Skip to content

Commit

Permalink
Invalidate covers on every try
Browse files Browse the repository at this point in the history
  • Loading branch information
probablykasper committed Sep 25, 2024
1 parent acc4b3f commit 5e7562f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
4 changes: 1 addition & 3 deletions ferrum-addon/addon.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ export declare function new_playlist(name: string, description: string, isFolder
export declare function update_playlist(id: string, name: string, description: string): void
export declare function move_playlist(id: string, fromId: string, toId: string, toIndex: number): void
/** Returns `None` if the file does not have an image */
export declare function get_modified_timestamp_ms(path: string): number | null
/** Returns `None` if the file does not have an image */
export declare function read_cache_cover_async(path: string, index: number, dateModifiedMs: number, cacheDbPath: string): Promise<Buffer | null>
export declare function read_small_cover_async(path: string, index: number, cacheDbPath: string): Promise<Buffer | null>
export interface TrackMd {
name: string
artist: string
Expand Down
34 changes: 14 additions & 20 deletions src-native/tracks/cover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ fn init_cache_db(path: String) -> Result<()> {
}

/// Returns `None` if the file does not have an image
#[napi(js_name = "get_modified_timestamp_ms")]
#[allow(dead_code)]
fn get_modified_timestamp_ms_js(path: String) -> Result<Option<i64>> {
let modified_timestamp_ms: i64 = match get_modified_timestamp_ms(&path)? {
Some(modified_timestamp_ms) => modified_timestamp_ms.try_into().unwrap(),
None => return Ok(None),
};
Ok(Some(modified_timestamp_ms))
}

fn get_modified_timestamp_ms(path: &str) -> Result<Option<u128>> {
let file_metadata = match fs::metadata(path) {
Ok(file_metadata) => file_metadata,
Expand Down Expand Up @@ -124,17 +114,14 @@ fn write_to_cache(cache_db: &Database, path: &str, value: CacheEntry) -> Result<
}

/// Returns `None` if the file does not have an image
#[napi(js_name = "read_cache_cover_async")]
#[napi(js_name = "read_small_cover_async")]
#[allow(dead_code)]
pub async fn read_cache_cover_async(
pub async fn read_small_cover_async(
path: String,
index: u16,
date_modified_ms: i64,
cache_db_path: String,
) -> Result<Option<Buffer>> {
if date_modified_ms <= 0 {
throw!("date_modified_ms must be greater than 0")
} else if path == "" {
if path == "" {
throw!("path must not be empty")
} else if cache_db_path == "" {
throw!("cache_db_path must not be empty")
Expand All @@ -145,8 +132,13 @@ pub async fn read_cache_cover_async(
let cache_db_mutex = CACHE_DB.read().unwrap();
let cache_db = cache_db_mutex.as_ref().unwrap();

if let Some(img_bytes) = get_cached_image(cache_db, &path, date_modified_ms)? {
return Ok(Some(img_bytes.into()));
let date_modified_ms: Option<i64> =
get_modified_timestamp_ms(&path)?.map(|n| n.try_into().unwrap());

if let Some(date_modified_ms) = date_modified_ms {
if let Some(img_bytes) = get_cached_image(cache_db, &path, date_modified_ms)? {
return Ok(Some(img_bytes.into()));
}
}

let tag = Tag::read_from_path(&PathBuf::from(path.clone()))?;
Expand All @@ -157,8 +149,10 @@ pub async fn read_cache_cover_async(

let img_bytes = to_resized_image(image.data, 84)?;

let value = (date_modified_ms, img_bytes.clone());
write_to_cache(cache_db, &path, value)?;
if let Some(date_modified_ms) = date_modified_ms {
let value = (date_modified_ms, img_bytes.clone());
write_to_cache(cache_db, &path, value)?;
}

Ok(Some(img_bytes.into()))
}
Expand Down
4 changes: 1 addition & 3 deletions src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ app.whenReady().then(async () => {
const url_raw = new URL(request.url)
const track_path = decodeURIComponent(url_raw.searchParams.get('path') ?? '')
const cache_db_path = decodeURIComponent(url_raw.searchParams.get('cache_db_path') ?? '')
const date_modified = decodeURIComponent(url_raw.searchParams.get('date_modified') ?? '')

addon
// .read_cache_cover_async(pathname, 0, Number(date_modified), cache_db_path)
.read_cache_cover_async(track_path, 0, parseInt(date_modified), cache_db_path)
.read_small_cover_async(track_path, 0, cache_db_path)
.then((buffer) => {
if (buffer === null) {
resolve(new Response(null, { status: 404 }))
Expand Down

0 comments on commit 5e7562f

Please sign in to comment.