Skip to content

Commit f3b46c6

Browse files
committed
Merge pull request #53 from longbai/use_nsfilehandle
use_nsfilehandle
2 parents f8d3d84 + 94c8a67 commit f3b46c6

File tree

13 files changed

+425
-70
lines changed

13 files changed

+425
-70
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: objective-c
22
before_install:
33
# - brew update
44
# - brew upgrade xctool
5-
- gem install cocoapods
5+
# - gem install cocoapods
66
before_script:
77
- export QINIU_TEST_ENV="travis"
88
script:

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#Changelog
22

3-
## 7.0.12 (2015-07-22)
3+
## 7.0.12 (2015-07-25)
44

55
### 增加
66
* 使用 happydns 进行解析
7+
* 支持ALAsset上传,多谢 github.com/NSFish的代码
8+
9+
### 修正
10+
* 当文件超过200M时,iOS上传失败
711

812
## 7.0.11.1 (2015-06-23)
913

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
DF0D23CD19DCE6C400D6B68F /* QNResponseInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = DF0D23CC19DCE6C400D6B68F /* QNResponseInfo.h */; };
2828
DF0D23CF19DCE6E500D6B68F /* QNResponseInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = DF0D23CE19DCE6E500D6B68F /* QNResponseInfo.m */; };
2929
DF0D23D019DCE6E500D6B68F /* QNResponseInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = DF0D23CE19DCE6E500D6B68F /* QNResponseInfo.m */; };
30+
DF24DDA01B63FF0E00EF57E6 /* QNFile.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF126F01B639F3B0005C39C /* QNFile.m */; };
31+
DF24DDA11B63FF1800EF57E6 /* QNALAssetFile.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF126F51B63ABED0005C39C /* QNALAssetFile.m */; };
3032
DF293C9119DB85CB00799011 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = DF293C9019DB85CB00799011 /* libz.dylib */; };
3133
DF293C9219DB85EB00799011 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = DF293C9019DB85CB00799011 /* libz.dylib */; };
3234
DF293C9719DB865800799011 /* QNCrc32Test.m in Sources */ = {isa = PBXBuildFile; fileRef = DF293C9619DB865800799011 /* QNCrc32Test.m */; };
@@ -88,6 +90,11 @@
8890
DFA9B65A19E0B53700A15FD1 /* QNFileRecorder.m in Sources */ = {isa = PBXBuildFile; fileRef = DFA9B65719E0B53700A15FD1 /* QNFileRecorder.m */; };
8991
DFA9B65C19E0B58900A15FD1 /* QNRecorderDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = DFA9B65B19E0B58900A15FD1 /* QNRecorderDelegate.h */; };
9092
DFBC622519DE459800458C4B /* QNHttpTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DFBC622419DE459800458C4B /* QNHttpTest.m */; };
93+
DFF126EE1B63909A0005C39C /* QNFileDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF126ED1B63909A0005C39C /* QNFileDelegate.h */; };
94+
DFF126F11B639F3B0005C39C /* QNFile.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF126EF1B639F3B0005C39C /* QNFile.h */; };
95+
DFF126F21B639F3B0005C39C /* QNFile.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF126F01B639F3B0005C39C /* QNFile.m */; };
96+
DFF126F61B63ABED0005C39C /* QNALAssetFile.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF126F41B63ABED0005C39C /* QNALAssetFile.h */; };
97+
DFF126F71B63ABED0005C39C /* QNALAssetFile.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF126F51B63ABED0005C39C /* QNALAssetFile.m */; };
9198
DFF525311A6235D100D02BA1 /* QNSessionManager.h in Headers */ = {isa = PBXBuildFile; fileRef = DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */; };
9299
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
93100
DFF525341A6235D100D02BA1 /* QNSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DFF525301A6235D100D02BA1 /* QNSessionManager.m */; };
@@ -175,6 +182,11 @@
175182
DFA9B65B19E0B58900A15FD1 /* QNRecorderDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNRecorderDelegate.h; sourceTree = "<group>"; };
176183
DFA9B65E19E391A100A15FD1 /* QNTestConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNTestConfig.h; sourceTree = "<group>"; };
177184
DFBC622419DE459800458C4B /* QNHttpTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNHttpTest.m; sourceTree = "<group>"; };
185+
DFF126ED1B63909A0005C39C /* QNFileDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNFileDelegate.h; sourceTree = "<group>"; };
186+
DFF126EF1B639F3B0005C39C /* QNFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNFile.h; sourceTree = "<group>"; };
187+
DFF126F01B639F3B0005C39C /* QNFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNFile.m; sourceTree = "<group>"; };
188+
DFF126F41B63ABED0005C39C /* QNALAssetFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNALAssetFile.h; sourceTree = "<group>"; };
189+
DFF126F51B63ABED0005C39C /* QNALAssetFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNALAssetFile.m; sourceTree = "<group>"; };
178190
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNSessionManager.h; sourceTree = "<group>"; };
179191
DFF525301A6235D100D02BA1 /* QNSessionManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNSessionManager.m; sourceTree = "<group>"; };
180192
DFF525361A626A3700D02BA1 /* QNHttpDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNHttpDelegate.h; sourceTree = "<group>"; };
@@ -320,6 +332,11 @@
320332
DFA9B63A19DF904000A15FD1 /* QNEtag.m */,
321333
DF437CD01B2426270099587B /* GTM_Base64.h */,
322334
DF437CD11B2426270099587B /* GTM_Base64.m */,
335+
DFF126ED1B63909A0005C39C /* QNFileDelegate.h */,
336+
DFF126EF1B639F3B0005C39C /* QNFile.h */,
337+
DFF126F01B639F3B0005C39C /* QNFile.m */,
338+
DFF126F41B63ABED0005C39C /* QNALAssetFile.h */,
339+
DFF126F51B63ABED0005C39C /* QNALAssetFile.m */,
323340
);
324341
path = Common;
325342
sourceTree = "<group>";
@@ -381,17 +398,20 @@
381398
buildActionMask = 2147483647;
382399
files = (
383400
DF0D23CD19DCE6C400D6B68F /* QNResponseInfo.h in Headers */,
401+
DFF126F61B63ABED0005C39C /* QNALAssetFile.h in Headers */,
384402
DF6099FA1A5676AA00AC7297 /* QNDns.h in Headers */,
385403
DF437CD21B2426270099587B /* GTM_Base64.h in Headers */,
386404
DF293C9E19DBC2AE00799011 /* QNUserAgent.h in Headers */,
387405
DF2CDE6719DAC6A400CE01FB /* QNVersion.h in Headers */,
388406
DF293CA419DC04C200799011 /* QNHttpManager.h in Headers */,
389407
DF437CD91B2429E10099587B /* QNUpToken.h in Headers */,
390408
DFA9B65819E0B53700A15FD1 /* QNFileRecorder.h in Headers */,
409+
DFF126EE1B63909A0005C39C /* QNFileDelegate.h in Headers */,
391410
DF482FD81B0DA8A2000DAD98 /* QNConfiguration.h in Headers */,
392411
DF609A051A58E39D00AC7297 /* QNFormUpload.h in Headers */,
393412
DFF525371A626A3700D02BA1 /* QNHttpDelegate.h in Headers */,
394413
DF2CDE6019DAC6A400CE01FB /* QNCrc32.h in Headers */,
414+
DFF126F11B639F3B0005C39C /* QNFile.h in Headers */,
395415
DFA9B63B19DF904000A15FD1 /* QNEtag.h in Headers */,
396416
DF2CDE5C19DAC6A400CE01FB /* QNUrlSafeBase64.h in Headers */,
397417
DFA9B64919E0018800A15FD1 /* QNUploadOption.h in Headers */,
@@ -683,6 +703,8 @@
683703
DF293CA619DC05B800799011 /* QNHttpManager.m in Sources */,
684704
DF293CAB19DC0E5300799011 /* QNResumeUpload.m in Sources */,
685705
DF437CDA1B2429E10099587B /* QNUpToken.m in Sources */,
706+
DFF126F71B63ABED0005C39C /* QNALAssetFile.m in Sources */,
707+
DFF126F21B639F3B0005C39C /* QNFile.m in Sources */,
686708
DFF525321A6235D100D02BA1 /* QNSessionManager.m in Sources */,
687709
DFA39AA519F1272800A1A158 /* QNAsyncRun.m in Sources */,
688710
DFA9B64A19E0018800A15FD1 /* QNUploadOption.m in Sources */,
@@ -713,6 +735,8 @@
713735
isa = PBXSourcesBuildPhase;
714736
buildActionMask = 2147483647;
715737
files = (
738+
DF24DDA11B63FF1800EF57E6 /* QNALAssetFile.m in Sources */,
739+
DF24DDA01B63FF0E00EF57E6 /* QNFile.m in Sources */,
716740
DF2CDE5E19DAC6A400CE01FB /* QNUrlSafeBase64.m in Sources */,
717741
DF0D23D019DCE6E500D6B68F /* QNResponseInfo.m in Sources */,
718742
DFA9B63D19DF904000A15FD1 /* QNEtag.m in Sources */,

QiniuSDK/Common/QNALAssetFile.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// QNALAssetFile.h
3+
// QiniuSDK
4+
//
5+
// Created by bailong on 15/7/25.
6+
// Copyright (c) 2015年 Qiniu. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
#import "QNFileDelegate.h"
12+
13+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED)
14+
@class ALAsset;
15+
@interface QNALAssetFile : NSObject <QNFileDelegate>
16+
17+
/**
18+
* 打开指定文件
19+
*
20+
* @param path 文件路径
21+
* @param error 输出的错误信息
22+
*
23+
* @return 实例
24+
*/
25+
- (instancetype)init:(ALAsset *)asset
26+
error:(NSError *__autoreleasing *)error;
27+
@end
28+
#endif

QiniuSDK/Common/QNALAssetFile.m

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// QNALAssetFile.m
3+
// QiniuSDK
4+
//
5+
// Created by bailong on 15/7/25.
6+
// Copyright (c) 2015年 Qiniu. All rights reserved.
7+
//
8+
9+
#import "QNALAssetFile.h"
10+
11+
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
12+
#import <AssetsLibrary/AssetsLibrary.h>
13+
14+
#import "QNResponseInfo.h"
15+
16+
@interface QNALAssetFile ()
17+
18+
@property (nonatomic) ALAsset *asset;
19+
20+
@property (readonly) int64_t fileSize;
21+
22+
@property (readonly) int64_t fileModifyTime;
23+
24+
@end
25+
26+
@implementation QNALAssetFile
27+
- (instancetype)init:(ALAsset *)asset
28+
error:(NSError *__autoreleasing *)error {
29+
if (self = [super init]) {
30+
NSDate *createTime = [asset valueForProperty:ALAssetPropertyDate];
31+
int64_t t = 0;
32+
if (createTime != nil) {
33+
t = [createTime timeIntervalSince1970];
34+
}
35+
_fileModifyTime = t;
36+
_fileSize = asset.defaultRepresentation.size;
37+
_asset = asset;
38+
}
39+
40+
return self;
41+
}
42+
43+
- (NSData *)read:(long)offset
44+
size:(long)size {
45+
ALAssetRepresentation *rep = [self.asset defaultRepresentation];
46+
Byte *buffer = (Byte *)malloc(size);
47+
NSUInteger buffered = [rep getBytes:buffer fromOffset:offset length:size error:nil];
48+
49+
return [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
50+
}
51+
52+
- (NSData *)readAll {
53+
return [self read:0 size:(long)_fileSize];
54+
}
55+
56+
- (void)close {
57+
}
58+
59+
-(NSString *)path {
60+
ALAssetRepresentation *rep = [self.asset defaultRepresentation];
61+
return [rep url].path;
62+
}
63+
64+
- (int64_t)modifyTime {
65+
return _fileModifyTime;
66+
}
67+
68+
- (int64_t)size {
69+
return _fileSize;
70+
}
71+
@end
72+
#endif
73+

QiniuSDK/Common/QNFile.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// QNFile.h
3+
// QiniuSDK
4+
//
5+
// Created by bailong on 15/7/25.
6+
// Copyright (c) 2015年 Qiniu. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "QNFileDelegate.h"
11+
12+
@interface QNFile : NSObject <QNFileDelegate>
13+
/**
14+
* 打开指定文件
15+
*
16+
* @param path 文件路径
17+
* @param error 输出的错误信息
18+
*
19+
* @return 实例
20+
*/
21+
- (instancetype)init:(NSString *)path
22+
error:(NSError *__autoreleasing *)error;
23+
24+
@end

QiniuSDK/Common/QNFile.m

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// QNFile.m
3+
// QiniuSDK
4+
//
5+
// Created by bailong on 15/7/25.
6+
// Copyright (c) 2015年 Qiniu. All rights reserved.
7+
//
8+
9+
#import "QNFile.h"
10+
#import "QNResponseInfo.h"
11+
12+
@interface QNFile ()
13+
14+
@property (nonatomic, readonly) NSString *filepath;
15+
16+
@property (nonatomic) NSData *data;
17+
18+
@property (readonly) int64_t fileSize;
19+
20+
@property (readonly) int64_t fileModifyTime;
21+
22+
@property (nonatomic) NSFileHandle *file;
23+
24+
@end
25+
26+
@implementation QNFile
27+
28+
- (instancetype)init:(NSString *)path
29+
error:(NSError *__autoreleasing *)error {
30+
if (self = [super init]) {
31+
_filepath = path;
32+
NSError *error2 = nil;
33+
NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error2];
34+
if (error2 != nil) {
35+
if (error != nil) {
36+
*error = error2;
37+
}
38+
return self;
39+
}
40+
NSNumber *fileSizeNumber = fileAttr[NSFileSize];
41+
_fileSize = [fileSizeNumber intValue];
42+
NSDate *modifyTime = fileAttr[NSFileModificationDate];
43+
int64_t t = 0;
44+
if (modifyTime != nil) {
45+
t = [modifyTime timeIntervalSince1970];
46+
}
47+
_fileModifyTime = t;
48+
NSFileHandle *f = nil;
49+
NSData *d = nil;
50+
//[NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error] 不能用在大于 200M的文件上,改用filehandle
51+
// 参见 https://issues.apache.org/jira/browse/CB-5790
52+
if (_fileSize > 32*1024*1024) {
53+
f = [NSFileHandle fileHandleForReadingAtPath:path];
54+
if (f == nil) {
55+
if (error != nil) {
56+
*error =[[NSError alloc] initWithDomain:path code:kQNFileError userInfo:nil];
57+
}
58+
return self;
59+
}
60+
}else{
61+
d = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:&error2];
62+
if (error2 != nil) {
63+
if (error != nil) {
64+
*error = error2;
65+
}
66+
return self;
67+
}
68+
}
69+
_file = f;
70+
_data = d;
71+
}
72+
73+
return self;
74+
}
75+
76+
- (NSData *)read:(long)offset
77+
size:(long)size {
78+
if (_data != nil) {
79+
return [_data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
80+
}
81+
[_file seekToFileOffset:offset];
82+
return [_file readDataOfLength:size];
83+
}
84+
85+
- (NSData *)readAll {
86+
return [self read:0 size:(long)_fileSize];
87+
}
88+
89+
- (void)close {
90+
if (_file != nil) {
91+
[_file closeFile];
92+
}
93+
}
94+
95+
-(NSString *)path {
96+
return _filepath;
97+
}
98+
99+
- (int64_t)modifyTime {
100+
return _fileModifyTime;
101+
}
102+
103+
- (int64_t)size {
104+
return _fileSize;
105+
}
106+
107+
@end

0 commit comments

Comments
 (0)