Skip to content

Commit c353bf3

Browse files
committed
Logo: adds automatic cleanup of temporary media cover files
1 parent 338b552 commit c353bf3

File tree

8 files changed

+30
-1
lines changed

8 files changed

+30
-1
lines changed

src/common/io/io.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ static inline bool ffSearchUserConfigFile(const FFlist* configDirs, const char*
241241
}
242242

243243
FFNativeFD ffGetNullFD(void);
244+
bool ffRemoveFile(const char* fileName);
244245

245246
#ifdef _WIN32
246247
// Only O_RDONLY is supported

src/common/io/io_unix.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,8 @@ FFNativeFD ffGetNullFD(void)
372372
hNullFile = open("/dev/null", O_WRONLY | O_CLOEXEC);
373373
return hNullFile;
374374
}
375+
376+
bool ffRemoveFile(const char* fileName)
377+
{
378+
return unlink(fileName) == 0;
379+
}

src/common/io/io_windows.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,8 @@ FFNativeFD ffGetNullFD(void)
371371
});
372372
return hNullFile;
373373
}
374+
375+
bool ffRemoveFile(const char* fileName)
376+
{
377+
return DeleteFileA(fileName) != FALSE;
378+
}

src/detection/media/media.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const FFMediaResult* ffDetectMedia(bool saveCover)
1717
ffStrbufInit(&result.url);
1818
ffStrbufInit(&result.status);
1919
ffStrbufInit(&result.cover);
20+
result.removeCoverAfterUse = false;
2021
ffDetectMediaImpl(&result, saveCover);
2122

2223
if(result.song.length == 0 && result.error.length == 0)

src/detection/media/media.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct FFMediaResult
1414
FFstrbuf url;
1515
FFstrbuf status;
1616
FFstrbuf cover;
17+
bool removeCoverAfterUse;
1718
} FFMediaResult;
1819

1920
const FFMediaResult* ffDetectMedia(bool saveCover);

src/detection/media/media_apple.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
FF_CFTYPE_AUTO_RELEASE CFStringRef ext = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension);
4545
#pragma clang diagnostic pop
4646
NSString *tmpDir = NSTemporaryDirectory();
47-
NSString *uuid = [[NSUUID UUID] UUIDString];
47+
NSString *uuid = NSUUID.UUID.UUIDString;
4848
NSString *path = [tmpDir stringByAppendingPathComponent:[NSString stringWithFormat:@"ff_%@.%@", uuid, ext ? (__bridge NSString *) ext : @"img"]];
4949
if ([artworkData writeToFile:path atomically:NO])
5050
ffStrbufSetS(&result->cover, path.UTF8String);
@@ -160,5 +160,7 @@ void ffDetectMediaImpl(FFMediaResult* media, bool saveCover)
160160
if (ffStrbufStartsWithIgnCaseS(&media->player, "com."))
161161
ffStrbufSubstrAfter(&media->player, strlen("com.") - 1);
162162
ffStrbufReplaceAllC(&media->player, '.', ' ');
163+
if (media->cover.length > 0)
164+
media->removeCoverAfterUse = true;
163165
}
164166
}

src/detection/media/media_windows.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ static const char* getMedia(FFMediaResult* media, bool saveCover)
3434
ffStrbufSetWS(&media->album, result.album);
3535
ffStrbufSetWS(&media->cover, result.cover);
3636
ffStrbufSetStatic(&media->status, result.status);
37+
if (media->cover.length > 0)
38+
media->removeCoverAfterUse = true;
3739
return NULL;
3840
}
3941

src/logo/logo.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "util/stringUtils.h"
1010

1111
#include <ctype.h>
12+
#include <stdlib.h>
1213
#include <string.h>
1314

1415
typedef enum __attribute__((__packed__)) FFLogoSize
@@ -474,6 +475,16 @@ static bool logoPrintData(bool doColorReplacement, FFstrbuf* source)
474475
return true;
475476
}
476477

478+
static void removeMediaCoverFile(void)
479+
{
480+
const FFMediaResult* media = ffDetectMedia(true);
481+
if (media->cover.length > 0)
482+
{
483+
ffRemoveFile(media->cover.chars);
484+
ffStrbufDestroy((FFstrbuf*) &media->cover);
485+
}
486+
}
487+
477488
static bool updateLogoPath(void)
478489
{
479490
FFOptionsLogo* options = &instance.config.logo;
@@ -490,6 +501,7 @@ static bool updateLogoPath(void)
490501
if (media->cover.length == 0)
491502
return false;
492503
ffStrbufSet(&options->source, &media->cover);
504+
if (media->removeCoverAfterUse) atexit(removeMediaCoverFile);
493505
return true;
494506
}
495507

0 commit comments

Comments
 (0)