@
gaitana 贴代码 有点长
```C
//
// QiniuManager.swift
// summer
//
// Created by Alex D. on 15/8/12.
//
import Qiniu
class QiniuManager {
static let instance = QiniuManager()
var upManager:QNUploadManager?
private init() {
upManager = QNUploadManager()
}
//MARK: 上传文件
func upload(token:String, data:NSData, key:String, process:((key:String, percent:Float)->Void)?, complete:((info:QNResponseInfo, key:String, resp:NSDictionary?)->Void)?, cancel:(()->Bool)?) {
let opt = QNUploadOption(mime: "text/plain", progressHandler: { (key, percent) -> Void in
print(key)
process?(key: key, percent: percent)
}, params: ["x:foo" : "fooval"], checkCrc: true) { () -> Bool in
//取消
if let block = cancel?() {
return block
}else {
return false
}
}
upManager?.putData(data, key: key, token: token, complete: { (info, key, resp) -> Void in
complete?(info: info, key: key, resp: resp)
}, option: opt)
}
//MARK: 断点续传
func upload(folder:String, token:String, data:NSData, key:String, process:((key:String, percent:Float)->Void)?, complete:((info:QNResponseInfo, key:String, resp:NSDictionary?)->Void)?, cancel:(()->Bool)?) {
do {
//写标记
self.upManager = try QNUploadManager(recorder: QNFileRecorder(folder: folder))
} catch {
self.upManager = QNUploadManager()
print("启动断点续传失败\(error),自动切换到连续上传")
}
self.upload(token, data: data, key: key, process: process, complete: { (info, key, resp) -> Void in
self.upManager = QNUploadManager()
complete?(info: info, key: key, resp: resp)
}) { () -> Bool in
cancel?()
if cancel?() == true {
self.upManager = QNUploadManager()
return true
}
return false
}
}
}
```
```C
//
// QiniuManager.m
// GiftBox
//
// Created by __ on 15/9/29.
//
#define kGetQiniuTokenURL xxxxx
#import "QiniuManager.h"
@
interface QiniuManager()
@
property (nonatomic, strong) QNUploadManager * manager;
@
property (nonatomic, copy) NSString * token;
@
property (nonatomic, assign) NSTimeInterval lastTime;
@
end@
implementation QiniuManager
+ (QiniuManager *)sharedInstance {
static QiniuManager * manager;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
manager = [[QiniuManager alloc] init];
manager.manager = [[QNUploadManager alloc] init];
});
return manager;
}
- (void)uploadData:(NSData *)data
key:(NSString *)key
success:(void(^)(NSString * key))success
fail:(void (^)(NSError *))fail {
if (self.needRefreshToken) {
[self refreshTokenWithSuccess:^{
[self uploadData:data key:key success:success fail:fail];
} fail:^(NSError *error) {
if (fail) {
fail(error);
}
}];
} else {
[self.manager putData:data key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
if (info.isOK && resp != nil) {
if (success) {
success(key);
}
} else {
if (fail) {
fail(info.error);
}
}
} option:nil];
}
}
- (void)refreshTokenWithSuccess:(void(^)())success fail:(void(^)(NSError * error))fail {
[NetworkManager addGetRequestWithURLString:[NSString stringWithFormat:kGetQiniuTokenURL, IS_DEBUG ? 4 : 2] tag:100 contentType:GBContentType success:^(NetworkManager *network, id responseObject) {
NSString * status = [responseObject objectForKey:@"status"];
if (status.integerValue == 0) {
NSString * qiniuToken = [responseObject objectForKey:@"token"];
self.token = qiniuToken;
self.lastTime = [NSDate date].timeIntervalSince1970;
if (success) {
success();
}
} else {
if (fail) {
fail([NSError errorWithDomain:[responseObject objectForKey:@"err_msg"] code:[[responseObject objectForKey:@"code"] integerValue] userInfo:nil]);
}
}
} fail:^(NetworkManager *network, NSError *error) {
if (fail) {
fail(error);
}
}];
}
//十分钟
- (BOOL)needRefreshToken {
NSTimeInterval nowTime = [NSDate date].timeIntervalSince1970;
return nowTime - self.lastTime > 60 * 10;
}
@
end```