-
Notifications
You must be signed in to change notification settings - Fork 6
/
MVDroplrLinkService.m
97 lines (85 loc) · 2.74 KB
/
MVDroplrLinkService.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#import "MVDroplrLinkService.h"
#import "MVGetRedirectedURL.h"
#import "NSString+QueryParsing.h"
@interface MVDroplrLinkService ()
@property (strong, readwrite) NSURL *url;
@property (strong, readwrite) NSString *itemType;
@property (strong, readwrite) NSURL *downloadUrl;
@property (readwrite) BOOL informationFetched;
@property (readwrite) BOOL error;
@end
@implementation MVDroplrLinkService
@synthesize url = url_,
itemType = itemType_,
downloadUrl = downloadUrl_,
informationFetched = informationFetched_,
error = error_;
- (id)initWithURL:(NSURL*)url
itemType:(NSString*)type
{
self = [super init];
if(self)
{
url_ = url;
itemType_ = type;
downloadUrl_ = nil;
informationFetched_ = NO;
error_ = NO;
}
return self;
}
- (BOOL)isImage
{
return [self.itemType isEqualToString:@"i"];
}
#pragma mark -
#pragma mark MVService Methods
- (void)fetchInformation
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSURL *shortURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@+",self.url.absoluteString]];
MVGetRedirectedURL *getRedirectedURL = [[MVGetRedirectedURL alloc] initWithURL:shortURL];
[getRedirectedURL get:^(NSURL *redirectedURL, NSString *suggestedFilename) {
@try {
if(redirectedURL)
{
suggestedFilename = [suggestedFilename stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
self.downloadUrl = [NSURL URLWithString:
[NSString stringWithFormat:@"%@+#chat_filename=%@",
self.url.absoluteString,
suggestedFilename]];
}
else
{
self.error = YES;
}
}
@catch (NSException *exception) {
self.error = YES;
}
self.informationFetched = YES;
}];
});
}
+ (MVDroplrLinkService*)serviceForURL:(NSURL*)url;
{
if([url.host isEqualToString:@"d.pr"])
{
if(url.pathComponents.count >= 2)
{
NSSet *availableTypes = [NSSet setWithObjects:@"n", @"a", @"v", @"i", @"f", nil];
NSString *type = ([[url.pathComponents objectAtIndex:0] isEqualToString:@"/"] ?
[url.pathComponents objectAtIndex:1] :
[url.pathComponents objectAtIndex:0]);
if(type && [availableTypes containsObject:type])
{
MVDroplrLinkService *service = [[MVDroplrLinkService alloc] initWithURL:url
itemType:type];
return service;
}
}
}
return nil;
}
@end