2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-03-29 23:38:38 +00:00
|
|
|
|
|
|
|
#import "BlocklistDownloader.h"
|
2008-08-31 19:47:11 +00:00
|
|
|
#import "BlocklistDownloaderViewController.h"
|
2008-09-01 15:46:00 +00:00
|
|
|
#import "BlocklistScheduler.h"
|
2012-05-29 01:03:21 +00:00
|
|
|
#import "Controller.h"
|
2008-03-29 23:38:38 +00:00
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface BlocklistDownloader ()
|
2008-03-29 23:38:38 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic) NSURLSession* fSession;
|
|
|
|
@property(nonatomic) NSUInteger fCurrentSize;
|
|
|
|
@property(nonatomic) long long fExpectedSize;
|
2023-04-15 21:33:43 +00:00
|
|
|
@property(nonatomic) BlocklistDownloadState fState;
|
2022-02-22 16:04:20 +00:00
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistDownloader
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
BlocklistDownloader* fBLDownloader = nil;
|
|
|
|
|
|
|
|
+ (BlocklistDownloader*)downloader
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2012-03-13 03:20:09 +00:00
|
|
|
if (!fBLDownloader)
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2012-03-13 03:20:09 +00:00
|
|
|
fBLDownloader = [[BlocklistDownloader alloc] init];
|
|
|
|
[fBLDownloader startDownload];
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
return fBLDownloader;
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (BOOL)isRunning
|
2008-08-31 20:26:41 +00:00
|
|
|
{
|
2012-03-13 03:20:09 +00:00
|
|
|
return fBLDownloader != nil;
|
2008-08-31 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setViewController:(BlocklistDownloaderViewController*)viewController
|
2008-04-06 04:49:39 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_viewController = viewController;
|
|
|
|
if (_viewController)
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
switch (self.fState)
|
2008-08-31 21:10:45 +00:00
|
|
|
{
|
2023-04-15 21:33:43 +00:00
|
|
|
case BlocklistDownloadStateStart:
|
2022-02-22 16:04:20 +00:00
|
|
|
[_viewController setStatusStarting];
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2023-04-15 21:33:43 +00:00
|
|
|
case BlocklistDownloadStateDownloading:
|
2022-02-22 16:04:20 +00:00
|
|
|
[_viewController setStatusProgressForCurrentSize:self.fCurrentSize expectedSize:self.fExpectedSize];
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2023-04-15 21:33:43 +00:00
|
|
|
case BlocklistDownloadStateProcessing:
|
2022-02-22 16:04:20 +00:00
|
|
|
[_viewController setStatusProcessing];
|
2021-08-15 09:41:48 +00:00
|
|
|
break;
|
2008-08-31 21:10:45 +00:00
|
|
|
}
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
2008-04-06 04:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)cancelDownload
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[_viewController setFinished];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fSession invalidateAndCancel];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[BlocklistScheduler.scheduler updateSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
fBLDownloader = nil;
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
- (void)URLSession:(NSURLSession*)session
|
|
|
|
downloadTask:(NSURLSessionDownloadTask*)downloadTask
|
|
|
|
didWriteData:(int64_t)bytesWritten
|
|
|
|
totalBytesWritten:(int64_t)totalBytesWritten
|
|
|
|
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2021-11-21 17:36:06 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2023-04-15 21:33:43 +00:00
|
|
|
self.fState = BlocklistDownloadStateDownloading;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fCurrentSize = totalBytesWritten;
|
|
|
|
self.fExpectedSize = totalBytesExpectedToWrite;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.viewController setStatusProgressForCurrentSize:self.fCurrentSize expectedSize:self.fExpectedSize];
|
2021-11-21 17:36:06 +00:00
|
|
|
});
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
- (void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task didCompleteWithError:(NSError*)error
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2021-11-21 17:36:06 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (error)
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.viewController setFailed:error.localizedDescription];
|
2021-11-21 17:36:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults setObject:[NSDate date] forKey:@"BlocklistNewLastUpdate"];
|
|
|
|
[BlocklistScheduler.scheduler updateSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-12-12 15:12:04 +00:00
|
|
|
[self.fSession finishTasksAndInvalidate];
|
2021-11-21 17:36:06 +00:00
|
|
|
fBLDownloader = nil;
|
|
|
|
});
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
- (void)URLSession:(NSURLSession*)session
|
|
|
|
downloadTask:(NSURLSessionDownloadTask*)downloadTask
|
|
|
|
didFinishDownloadingToURL:(NSURL*)location
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2023-04-15 21:33:43 +00:00
|
|
|
self.fState = BlocklistDownloadStateProcessing;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.viewController setStatusProcessing];
|
2021-11-21 17:36:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
NSString* filename = downloadTask.response.suggestedFilename;
|
|
|
|
if (filename == nil)
|
|
|
|
{
|
|
|
|
filename = @"transmission-blocklist.tmp";
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* tempFile = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
|
|
|
|
NSString* blocklistFile = [NSTemporaryDirectory() stringByAppendingPathComponent:@"transmission-blocklist"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
[NSFileManager.defaultManager moveItemAtPath:location.path toPath:tempFile error:nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
if ([@"text/plain" isEqualToString:downloadTask.response.MIMEType])
|
|
|
|
{
|
|
|
|
blocklistFile = tempFile;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-30 21:52:23 +00:00
|
|
|
[self decompressFrom:[NSURL fileURLWithPath:tempFile] to:[NSURL fileURLWithPath:blocklistFile] error:nil];
|
2021-11-21 17:36:06 +00:00
|
|
|
[NSFileManager.defaultManager removeItemAtPath:tempFile error:nil];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2022-10-25 16:14:42 +00:00
|
|
|
auto const count = tr_blocklistSetContent(((Controller*)NSApp.delegate).sessionHandle, blocklistFile.UTF8String);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
//delete downloaded file
|
|
|
|
[NSFileManager.defaultManager removeItemAtPath:blocklistFile error:nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
if (count > 0)
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.viewController setFinished];
|
2021-11-21 17:36:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-30 21:52:23 +00:00
|
|
|
[self.viewController
|
|
|
|
setFailed:NSLocalizedString(@"The specified blocklist file did not contain any valid rules.", "blocklist fail message")];
|
2021-11-21 17:36:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
//update last updated date for schedule
|
|
|
|
NSDate* date = [NSDate date];
|
|
|
|
[NSUserDefaults.standardUserDefaults setObject:date forKey:@"BlocklistNewLastUpdate"];
|
|
|
|
[NSUserDefaults.standardUserDefaults setObject:date forKey:@"BlocklistNewLastUpdateSuccess"];
|
|
|
|
[BlocklistScheduler.scheduler updateSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"BlocklistUpdated" object:nil];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-12-12 15:12:04 +00:00
|
|
|
[self.fSession finishTasksAndInvalidate];
|
2021-11-21 17:36:06 +00:00
|
|
|
fBLDownloader = nil;
|
2012-03-13 03:20:09 +00:00
|
|
|
});
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)startDownload
|
2008-03-29 23:38:38 +00:00
|
|
|
{
|
2023-04-15 21:33:43 +00:00
|
|
|
self.fState = BlocklistDownloadStateStart;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
self.fSession = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.ephemeralSessionConfiguration delegate:self
|
|
|
|
delegateQueue:nil];
|
2021-11-10 18:55:15 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[BlocklistScheduler.scheduler cancelSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* urlString = [NSUserDefaults.standardUserDefaults stringForKey:@"BlocklistURL"];
|
2010-10-31 19:05:46 +00:00
|
|
|
if (!urlString)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2010-10-31 19:05:46 +00:00
|
|
|
urlString = @"";
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2023-10-24 15:45:45 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2023-10-24 15:45:45 +00:00
|
|
|
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
|
|
|
if (![urlString isEqualToString:@""] && [urlString rangeOfString:@"://"].location == NSNotFound)
|
|
|
|
{
|
|
|
|
urlString = [@"https://" stringByAppendingString:urlString];
|
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSURLSessionDownloadTask* task = [self.fSession downloadTaskWithURL:[NSURL URLWithString:urlString]];
|
2021-11-10 18:55:15 +00:00
|
|
|
[task resume];
|
2008-03-29 23:38:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 22:51:40 +00:00
|
|
|
- (BOOL)decompressFrom:(NSURL*)file to:(NSURL*)destination error:(NSError**)error
|
2021-11-21 17:36:06 +00:00
|
|
|
{
|
|
|
|
if ([self untarFrom:file to:destination])
|
|
|
|
{
|
2022-04-29 22:51:40 +00:00
|
|
|
return YES;
|
2021-11-21 17:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ([self unzipFrom:file to:destination])
|
|
|
|
{
|
2022-04-29 22:51:40 +00:00
|
|
|
return YES;
|
2021-11-21 17:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ([self gunzipFrom:file to:destination])
|
|
|
|
{
|
2022-04-29 22:51:40 +00:00
|
|
|
return YES;
|
2021-11-21 17:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it doesn't look like archive just copy it to destination
|
|
|
|
else
|
|
|
|
{
|
2022-04-29 22:51:40 +00:00
|
|
|
return [NSFileManager.defaultManager copyItemAtURL:file toURL:destination error:error];
|
2021-11-21 17:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)untarFrom:(NSURL*)file to:(NSURL*)destination
|
2011-01-25 01:43:11 +00:00
|
|
|
{
|
2023-02-01 21:05:32 +00:00
|
|
|
// We need to check validity of archive before listing or unpacking.
|
|
|
|
NSTask* tarListCheck = [[NSTask alloc] init];
|
|
|
|
|
|
|
|
tarListCheck.launchPath = @"/usr/bin/tar";
|
|
|
|
tarListCheck.arguments = @[ @"--list", @"--file", file.path ];
|
|
|
|
tarListCheck.standardOutput = nil;
|
|
|
|
tarListCheck.standardError = nil;
|
|
|
|
|
|
|
|
@try
|
|
|
|
{
|
|
|
|
[tarListCheck launch];
|
|
|
|
[tarListCheck waitUntilExit];
|
|
|
|
|
|
|
|
if (tarListCheck.terminationStatus != 0)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@catch (NSException* exception)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
NSTask* tarList = [[NSTask alloc] init];
|
|
|
|
|
|
|
|
tarList.launchPath = @"/usr/bin/tar";
|
2022-03-30 21:52:23 +00:00
|
|
|
tarList.arguments = @[ @"--list", @"--file", file.path ];
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
NSPipe* pipe = [[NSPipe alloc] init];
|
|
|
|
tarList.standardOutput = pipe;
|
|
|
|
tarList.standardError = nil;
|
|
|
|
|
|
|
|
NSString* filename;
|
|
|
|
|
|
|
|
@try
|
|
|
|
{
|
|
|
|
[tarList launch];
|
|
|
|
[tarList waitUntilExit];
|
|
|
|
|
|
|
|
if (tarList.terminationStatus != 0)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSData* data = [pipe.fileHandleForReading readDataToEndOfFile];
|
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
NSString* output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
filename = [output componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet].firstObject;
|
|
|
|
}
|
|
|
|
@catch (NSException* exception)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's a directory, skip
|
|
|
|
if ([filename hasSuffix:@"/"])
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2022-06-29 04:20:42 +00:00
|
|
|
NSURL* destinationDir = destination.URLByDeletingLastPathComponent;
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
NSTask* untar = [[NSTask alloc] init];
|
|
|
|
untar.launchPath = @"/usr/bin/tar";
|
|
|
|
untar.currentDirectoryPath = destinationDir.path;
|
2022-03-30 21:52:23 +00:00
|
|
|
untar.arguments = @[ @"--extract", @"--file", file.path, filename ];
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
@try
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2021-11-21 17:36:06 +00:00
|
|
|
[untar launch];
|
|
|
|
[untar waitUntilExit];
|
|
|
|
|
|
|
|
if (untar.terminationStatus != 0)
|
2017-01-24 17:53:16 +00:00
|
|
|
{
|
2021-11-21 17:36:06 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@catch (NSException* exception)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
NSURL* result = [destinationDir URLByAppendingPathComponent:filename];
|
|
|
|
|
|
|
|
[NSFileManager.defaultManager moveItemAtURL:result toURL:destination error:nil];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)gunzipFrom:(NSURL*)file to:(NSURL*)destination
|
|
|
|
{
|
2022-06-29 04:20:42 +00:00
|
|
|
NSURL* destinationDir = destination.URLByDeletingLastPathComponent;
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
NSTask* gunzip = [[NSTask alloc] init];
|
|
|
|
gunzip.launchPath = @"/usr/bin/gunzip";
|
|
|
|
gunzip.currentDirectoryPath = destinationDir.path;
|
2022-10-24 15:12:33 +00:00
|
|
|
gunzip.arguments = @[ @"--keep", @"--force", file.path ];
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
@try
|
|
|
|
{
|
|
|
|
[gunzip launch];
|
|
|
|
[gunzip waitUntilExit];
|
|
|
|
|
|
|
|
if (gunzip.terminationStatus != 0)
|
|
|
|
{
|
|
|
|
return NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
}
|
2021-11-21 17:36:06 +00:00
|
|
|
}
|
2022-03-30 21:52:23 +00:00
|
|
|
@catch (NSException* exception)
|
2021-11-21 17:36:06 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2022-06-29 04:20:42 +00:00
|
|
|
NSURL* result = file.URLByDeletingPathExtension;
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
[NSFileManager.defaultManager moveItemAtURL:result toURL:destination error:nil];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)unzipFrom:(NSURL*)file to:(NSURL*)destination
|
|
|
|
{
|
|
|
|
NSTask* zipinfo = [[NSTask alloc] init];
|
|
|
|
zipinfo.launchPath = @"/usr/bin/zipinfo";
|
|
|
|
zipinfo.arguments = @[
|
|
|
|
@"-1", /* just the filename */
|
2022-12-28 04:08:48 +00:00
|
|
|
file.path /* source zip file */
|
2021-11-21 17:36:06 +00:00
|
|
|
];
|
|
|
|
NSPipe* pipe = [[NSPipe alloc] init];
|
|
|
|
zipinfo.standardOutput = pipe;
|
|
|
|
zipinfo.standardError = nil;
|
|
|
|
|
|
|
|
NSString* filename;
|
|
|
|
|
|
|
|
@try
|
|
|
|
{
|
|
|
|
[zipinfo launch];
|
|
|
|
[zipinfo waitUntilExit];
|
|
|
|
|
|
|
|
if (zipinfo.terminationStatus != 0)
|
2017-01-24 17:53:16 +00:00
|
|
|
{
|
2021-11-21 17:36:06 +00:00
|
|
|
return NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 17:36:06 +00:00
|
|
|
NSData* data = [pipe.fileHandleForReading readDataToEndOfFile];
|
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
NSString* output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
filename = [output componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet].firstObject;
|
|
|
|
}
|
2022-03-30 21:52:23 +00:00
|
|
|
@catch (NSException* exception)
|
2021-11-21 17:36:06 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's a directory, skip
|
|
|
|
if ([filename hasSuffix:@"/"])
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2022-06-29 04:20:42 +00:00
|
|
|
NSURL* destinationDir = destination.URLByDeletingLastPathComponent;
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
NSTask* unzip = [[NSTask alloc] init];
|
|
|
|
unzip.launchPath = @"/usr/bin/unzip";
|
|
|
|
unzip.currentDirectoryPath = destinationDir.path;
|
2022-03-30 21:52:23 +00:00
|
|
|
unzip.arguments = @[ file.path, filename ];
|
2021-11-21 17:36:06 +00:00
|
|
|
|
|
|
|
@try
|
|
|
|
{
|
|
|
|
[unzip launch];
|
|
|
|
[unzip waitUntilExit];
|
|
|
|
|
|
|
|
if (unzip.terminationStatus != 0)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2021-11-21 17:36:06 +00:00
|
|
|
return NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-30 21:52:23 +00:00
|
|
|
@catch (NSException* exception)
|
2021-11-21 17:36:06 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSURL* result = [destinationDir URLByAppendingPathComponent:filename];
|
|
|
|
|
|
|
|
[NSFileManager.defaultManager moveItemAtURL:result toURL:destination error:nil];
|
|
|
|
return YES;
|
2011-01-25 01:43:11 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 23:38:38 +00:00
|
|
|
@end
|