-
Notifications
You must be signed in to change notification settings - Fork 32
/
SampleAppFileTransferHandler.m
149 lines (114 loc) · 5.25 KB
/
SampleAppFileTransferHandler.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/******************************************************************************
* By accessing or copying this work, you agree to comply with the following *
* terms: *
* *
* Copyright (c) 2019-2023 mesibo *
* https://mesibo.com *
* All rights reserved. *
* *
* Redistribution is not permitted. Use of this software is subject to the *
* conditions specified at https://mesibo.com . When using the source code, *
* maintain the copyright notice, conditions, disclaimer, and links to mesibo *
* website, documentation and the source code repository. *
* *
* Do not use the name of mesibo or its contributors to endorse products from *
* this software without prior written permission. *
* *
* This software is provided "as is" without warranties. mesibo and its *
* contributors are not liable for any damages arising from its use. *
* *
* Documentation: https://mesibo.com/documentation/ *
* *
* Source Code Repository: https://github.com/mesibo/ *
*******************************************************************************/
#import "SampleAppFileTransferHandler.h"
#import "SampleAPI.h"
#import <Photos/Photos.h>
@implementation SampleAppFileTransferHandler
- (void) initialize {
[MesiboInstance addListener:self];
}
-(BOOL) Mesibo_onStartUpload:(MesiboFileTransfer *)file {
NSMutableDictionary *post = [[NSMutableDictionary alloc] init];
[post setObject:@"upload" forKey:@"op"];
[post setValue:[MesiboInstance getUploadAuthToken] forKey:@"auth"];
[post setValue:[@(file.mid) stringValue] forKey:@"mid"];
[post setValue:[@([MesiboInstance getUid]) stringValue] forKey:@"uid"];
[post setValue:[@(file.source) stringValue] forKey:@"source"];
Mesibo_onHTTPProgress handler = ^BOOL(MesiboHttp *http, int state, int progress) {
if(100 == progress && MESIBO_HTTPSTATE_DOWNLOAD == state) {
NSError *jsonerror = nil;
NSData *data = [[http getDataString] dataUsingEncoding:NSUTF8StringEncoding];
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonerror];
NSDictionary *returnedDict = (NSMutableDictionary *)jsonObject;
NSString *url = [returnedDict valueForKey:@"url"];
BOOL result = [[returnedDict valueForKey:@"result"] boolValue];
if(!url || !url.length || !result) {
[file setResult:NO];
return YES;
}
[file setResult:YES url:url];
return YES;
}
if(progress < 0) {
[file setResult:NO];
return YES;
}
[file setProgress:progress];
return YES;
};
MesiboHttp *http = [MesiboHttp new];
http.url = [MesiboInstance getUploadUrl];
http.uploadPhAsset = [file getPHAsset];
http.uploadLocalIdentifier = [file getLocalIdentifier];
http.uploadFile = [file getPath];
http.postBundle = post;
http.uploadFileField = @"photo";
http.listener = handler;
[file setFileTransferContext:http];
return [http execute];
}
-(BOOL) Mesibo_onStartDownload:(MesiboFileTransfer *)file {
if(MESIBO_ORIGIN_REALTIME != file.origin && !file.priority)
return NO;
NSString *url = [file getUrl];
if(!url || !url.length)
return NO;
if (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"]) {
return NO;
}
Mesibo_onHTTPProgress handler = ^BOOL(MesiboHttp *http, int state, int progress) {
if(100 == progress && MESIBO_HTTPSTATE_DOWNLOAD == state) {
[file setResult:YES url:nil];
return YES;
}
if(progress < 0) {
[file setResult:NO];
return YES;
}
[file setProgress:progress];
return YES;
};
MesiboHttp *http = [MesiboHttp new];
http.url = url;
http.downloadFile = [file getPath];
http.resume = YES;
http.listener = handler;
http.maxRetries = 10;
[file setFileTransferContext:http];
return [http execute];
}
-(BOOL) Mesibo_onStartFileTransfer:(MesiboFileTransfer *)ft {
if(!ft.upload) {
return [self Mesibo_onStartDownload:ft];
}
return [self Mesibo_onStartUpload:ft];
}
-(BOOL) Mesibo_onStopFileTransfer:(MesiboFileTransfer *)ft {
MesiboHttp *http = [ft getFileTransferContext];
if(http) {
[http cancel];
}
return YES;
}
@end