-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQiniuManager.m
89 lines (73 loc) · 2.29 KB
/
QiniuManager.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
//
// QiniuManager.m
// awesomeMobile
//
// Created by scott on 15/12/17.
// Copyright © 2015年 Facebook. All rights reserved.
//
#import "QiniuManager.h"
#import "RCTBridge.h"
@implementation QiniuManager
- (NSString *)mimeTypeForData:(NSData *)data {
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return @"image/jpeg";
break;
case 0x89:
return @"image/png";
break;
case 0x47:
return @"image/gif";
break;
case 0x49:
case 0x4D:
return @"image/tiff";
break;
case 0x25:
return @"application/pdf";
break;
case 0xD0:
return @"application/vnd";
break;
case 0x46:
return @"text/plain";
break;
default:
return @"application/octet-stream";
}
return nil;
}
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(uploadToQiniu:(NSString *)uri
key:(NSString *)key
token:(NSString *)token
params:(NSDictionary *)params
callback:(RCTResponseSenderBlock)callback)
{
// QNUploadManager *upManager = [[QNUploadManager alloc] init];
QNUploadManager *upManager = [QNUploadManager sharedInstanceWithConfiguration:nil];
// NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile: uri options:0 error:&error];
NSString *mimeString = [self mimeTypeForData:data];
NSMutableDictionary *mutableDict = [params mutableCopy];
[mutableDict setObject:mimeString forKey:@"x:mimeType"];
params = [mutableDict mutableCopy];
// NSLog(@"----params: %@", params);
self.opt = [[QNUploadOption alloc] initWithMime:mimeString progressHandler:nil params:params checkCrc:YES cancellationSignal:nil];
// NSLog(@"------qiniu opt: %@", self.opt);
[upManager putData:data key:key token:token
complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
// NSLog(@"-----update callback params:%@", resp);
if (resp == nil) {
// NSDictionary *res = [NSDictionary];
NSDictionary *res = @{@"status":@500, @"message": @"上传失败"};
callback(@[res]);
}else{
callback(@[resp]);
}
} option: self.opt];
}
@end