diff --git a/src/common/io/io.h b/src/common/io/io.h index c783e5323..fc4f4b48b 100644 --- a/src/common/io/io.h +++ b/src/common/io/io.h @@ -241,6 +241,7 @@ static inline bool ffSearchUserConfigFile(const FFlist* configDirs, const char* } FFNativeFD ffGetNullFD(void); +bool ffRemoveFile(const char* fileName); #ifdef _WIN32 // Only O_RDONLY is supported diff --git a/src/common/io/io_unix.c b/src/common/io/io_unix.c index cd4f3fae5..c8437a52c 100644 --- a/src/common/io/io_unix.c +++ b/src/common/io/io_unix.c @@ -372,3 +372,8 @@ FFNativeFD ffGetNullFD(void) hNullFile = open("/dev/null", O_WRONLY | O_CLOEXEC); return hNullFile; } + +bool ffRemoveFile(const char* fileName) +{ + return unlink(fileName) == 0; +} diff --git a/src/common/io/io_windows.c b/src/common/io/io_windows.c index 419a29929..ae1d584e9 100644 --- a/src/common/io/io_windows.c +++ b/src/common/io/io_windows.c @@ -371,3 +371,8 @@ FFNativeFD ffGetNullFD(void) }); return hNullFile; } + +bool ffRemoveFile(const char* fileName) +{ + return DeleteFileA(fileName) != FALSE; +} diff --git a/src/detection/media/media.c b/src/detection/media/media.c index 7ff7e16bd..1e5a38bc5 100644 --- a/src/detection/media/media.c +++ b/src/detection/media/media.c @@ -17,6 +17,7 @@ const FFMediaResult* ffDetectMedia(bool saveCover) ffStrbufInit(&result.url); ffStrbufInit(&result.status); ffStrbufInit(&result.cover); + result.removeCoverAfterUse = false; ffDetectMediaImpl(&result, saveCover); if(result.song.length == 0 && result.error.length == 0) diff --git a/src/detection/media/media.h b/src/detection/media/media.h index 2bee25c41..41ca65f55 100644 --- a/src/detection/media/media.h +++ b/src/detection/media/media.h @@ -14,6 +14,7 @@ typedef struct FFMediaResult FFstrbuf url; FFstrbuf status; FFstrbuf cover; + bool removeCoverAfterUse; } FFMediaResult; const FFMediaResult* ffDetectMedia(bool saveCover); diff --git a/src/detection/media/media_apple.m b/src/detection/media/media_apple.m index c3cb41bd9..d822b9435 100644 --- a/src/detection/media/media_apple.m +++ b/src/detection/media/media_apple.m @@ -44,7 +44,7 @@ static const char* getMediaByMediaRemote(FFMediaResult* result, bool saveCover) FF_CFTYPE_AUTO_RELEASE CFStringRef ext = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension); #pragma clang diagnostic pop NSString *tmpDir = NSTemporaryDirectory(); - NSString *uuid = [[NSUUID UUID] UUIDString]; + NSString *uuid = NSUUID.UUID.UUIDString; NSString *path = [tmpDir stringByAppendingPathComponent:[NSString stringWithFormat:@"ff_%@.%@", uuid, ext ? (__bridge NSString *) ext : @"img"]]; if ([artworkData writeToFile:path atomically:NO]) ffStrbufSetS(&result->cover, path.UTF8String); @@ -160,5 +160,7 @@ void ffDetectMediaImpl(FFMediaResult* media, bool saveCover) if (ffStrbufStartsWithIgnCaseS(&media->player, "com.")) ffStrbufSubstrAfter(&media->player, strlen("com.") - 1); ffStrbufReplaceAllC(&media->player, '.', ' '); + if (media->cover.length > 0) + media->removeCoverAfterUse = true; } } diff --git a/src/detection/media/media_windows.c b/src/detection/media/media_windows.c index 51810e423..899b6551a 100644 --- a/src/detection/media/media_windows.c +++ b/src/detection/media/media_windows.c @@ -34,6 +34,8 @@ static const char* getMedia(FFMediaResult* media, bool saveCover) ffStrbufSetWS(&media->album, result.album); ffStrbufSetWS(&media->cover, result.cover); ffStrbufSetStatic(&media->status, result.status); + if (media->cover.length > 0) + media->removeCoverAfterUse = true; return NULL; } diff --git a/src/logo/logo.c b/src/logo/logo.c index bb1398e5e..3081dc2c4 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -9,6 +9,7 @@ #include "util/stringUtils.h" #include +#include #include typedef enum __attribute__((__packed__)) FFLogoSize @@ -474,6 +475,16 @@ static bool logoPrintData(bool doColorReplacement, FFstrbuf* source) return true; } +static void removeMediaCoverFile(void) +{ + const FFMediaResult* media = ffDetectMedia(true); + if (media->cover.length > 0) + { + ffRemoveFile(media->cover.chars); + ffStrbufDestroy((FFstrbuf*) &media->cover); + } +} + static bool updateLogoPath(void) { FFOptionsLogo* options = &instance.config.logo; @@ -490,6 +501,7 @@ static bool updateLogoPath(void) if (media->cover.length == 0) return false; ffStrbufSet(&options->source, &media->cover); + if (media->removeCoverAfterUse) atexit(removeMediaCoverFile); return true; }