Skip to content

Commit

Permalink
Cleanup download code (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyby authored Sep 9, 2022
1 parent e80b71b commit dec8f71
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Application/Sources/Model/Download.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ typedef NS_ENUM(NSInteger, DownloadState) {
+ (void)removeUnusedDownloadedFiles;

/**
* Update the unplayable downloads and their files (or remove if no solutions)
* Fix unplayable downloads and their files (or remove if no fix was possible)
*/
+ (void)updateUnplayableDownloads;

Expand Down
27 changes: 14 additions & 13 deletions Application/Sources/Model/Download.m
Original file line number Diff line number Diff line change
Expand Up @@ -268,30 +268,31 @@ + (void)removeUnusedDownloadedFiles

+ (void)updateUnplayableDownloads
{
NSMutableArray<Download *> *unplayableDownloadeds = NSMutableArray.array;
NSMutableArray<Download *> *unplayableDownloads = NSMutableArray.array;
for (Download *download in Download.downloads) {
if (download.state == DownloadStateDownloaded && [download.localMediaFileName.pathExtension isEqualToString:@"octet-stream"]) {
// Try to move media file with the download url extension
if (download.downloadMediaURL.pathExtension != nil) {
NSURL *atURL = download.localMediaFileURL;
if (download.downloadMediaURL.pathExtension) {
NSURL *sourceURL = download.localMediaFileURL;

NSString *localMediaFileName = [download.localMediaFileName stringByReplacingOccurrencesOfString:download.localMediaFileName.pathExtension
withString:download.downloadMediaURL.pathExtension];
NSString *mediaFilePath = [[Download downloadsDirectoryURLString] stringByAppendingPathComponent:localMediaFileName];
NSURL *toURL = [NSURL fileURLWithPath:mediaFilePath];
[NSFileManager.defaultManager moveItemAtURL:atURL toURL:toURL error:nil];
NSString *localMediaFileName = [[download.localMediaFileName stringByDeletingPathExtension] stringByAppendingPathExtension:download.downloadMediaURL.pathExtension];
NSString *destinationPath = [[Download downloadsDirectoryURLString] stringByAppendingPathComponent:localMediaFileName];
NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath];
[NSFileManager.defaultManager moveItemAtURL:sourceURL toURL:destinationURL error:nil];

download.localMediaFileName = localMediaFileName;
if (! download.localMediaFileURL) {
[unplayableDownloadeds addObject:download];
if ([NSFileManager.defaultManager fileExistsAtPath:destinationPath]) {
download.localMediaFileName = localMediaFileName;
}
else {
[unplayableDownloads addObject:download];
}
}
else {
[unplayableDownloadeds addObject:download];
[unplayableDownloads addObject:download];
}
}
}
[Download removeDownloads:unplayableDownloadeds.copy];
[Download removeDownloads:unplayableDownloads.copy];
[self saveDownloadsDictionary];
}

Expand Down

0 comments on commit dec8f71

Please sign in to comment.