forked from mipstian/catch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTCFileUtils.m
77 lines (58 loc) · 2.55 KB
/
CTCFileUtils.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#import "CTCFileUtils.h"
static NSString * const kCTCFileUtilsTorrentExtension = @".torrent";
@implementation CTCFileUtils
+ (NSData *)bookmarkForURL:(NSURL *)url
error:(NSError * __autoreleasing *)outError {
// Create a bookmark so we can transfer access to the downloads path
// to the feed checker service
NSError *error = nil;
NSData *downloadFolderBookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationMinimalBookmark
includingResourceValuesForKeys:@[]
relativeToURL:nil
error:&error];
if (!downloadFolderBookmark || error) {
*outError = error;
return nil;
}
return downloadFolderBookmark;
}
+ (NSURL *)URLFromBookmark:(NSData *)bookmark
error:(NSError * __autoreleasing *)outError {
NSError *error = nil;
BOOL isStale = NO;
NSURL *URL = [NSURL URLByResolvingBookmarkData:bookmark
options:kNilOptions
relativeToURL:nil
bookmarkDataIsStale:&isStale
error:&error];
if (!URL || error) {
NSLog(@"Could not get URL from bookmark: %@", error);
*outError = error;
return nil;
}
return URL;
}
+ (NSString *)computeFilenameFromURL:(NSURL*)fileURL {
// Compute destination filename
NSString *filename = fileURL.path.pathComponents.lastObject;
// Reverse urlencode
return [filename stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
+ (NSString *)torrentFilenameFromString:(NSString*)name {
NSString *cleanName = [self fileNameFromString:name];
BOOL hasExtension = [cleanName hasSuffix:kCTCFileUtilsTorrentExtension];
// Add .torrent extension if needed
return hasExtension ? cleanName : [cleanName stringByAppendingString:kCTCFileUtilsTorrentExtension];
}
+ (NSString *)userDownloadsDirectory {
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
// Might be nil!
return searchPaths.firstObject;
}
+ (NSString *)userHomeDirectory {
return NSHomeDirectory();
}
+ (NSString *)fileNameFromString:(NSString *)name {
return [[name stringByReplacingOccurrencesOfString:@"/" withString:@""] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
}
@end