Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

My proposed 0.10.9 changes #489

Merged
merged 46 commits into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
0dd09a4
Fix path argument in iOS excludeFromBackupKey (#473)
grylance Aug 9, 2017
57af353
Fix link to fs.readStream() and to fs.writeStream() and insert link t…
lll000111 Aug 10, 2017
3a31e35
Fix the documentation part of https://github.com/wkh237/react-native-…
lll000111 Aug 10, 2017
23ef0f7
More fixes for issue https://github.com/wkh237/react-native-fetch-blo…
lll000111 Aug 11, 2017
647e88f
Fix one issue raised in https://github.com/wkh237/react-native-fetch-…
lll000111 Aug 12, 2017
05d6e03
fix some access rights, remove unused items
lll000111 Aug 14, 2017
3952293
update gradle version setting in build.gradle
lll000111 Aug 14, 2017
4c7bc55
Revert gradle settings to previous values :-(
lll000111 Aug 14, 2017
d22bdc6
add a missing closing ")"
lll000111 Aug 14, 2017
0fea62a
Removed the part of an obsolete callback function parameter that I ha…
lll000111 Aug 14, 2017
a2f13b1
let mkdir resolve with "undefined" instead of "null" (my mistake)
lll000111 Aug 14, 2017
d3e47cd
mkdir: normalize iOS and Android error if something already exists (f…
lll000111 Aug 14, 2017
2a4b11b
fix a long/int issue
lll000111 Aug 14, 2017
2233980
my mistake - according to https://facebook.github.io/react-native/doc…
lll000111 Aug 14, 2017
34e2377
Adde "utf8" as default encoding for fs.readFile - fixes #450 and #484
lll000111 Aug 15, 2017
a530f52
follow my IDEA IDE's recommendations - SparseArray instead of HashMap…
lll000111 Aug 15, 2017
7588f82
polyfill/File.js: add a parameter===undefined? check (this happened s…
lll000111 Aug 15, 2017
9682c2c
make var static again
lll000111 Aug 15, 2017
0f371ac
Normalized errors for fs.ls()
lll000111 Aug 15, 2017
0bac254
forgot one parameter
lll000111 Aug 15, 2017
30af8e9
more parameter checks
lll000111 Aug 15, 2017
c2e1d63
forgot to resolve the promise
lll000111 Aug 15, 2017
51d7453
Forgot ;
lll000111 Aug 15, 2017
4674692
add more error parameter checks
lll000111 Aug 15, 2017
3bb3bf1
change readStream()/writeStream() default encoding to utf8 to match t…
lll000111 Aug 16, 2017
45dd56c
default encoding is set in fs.js (now), no need to do it twice
lll000111 Aug 16, 2017
c2803a1
fs.js: forgot one more error refactoring
bcpclone Aug 3, 2017
a9cb606
ReadStream error events: Set a default error code "EUNSPECIFIED" if n…
lll000111 Aug 17, 2017
1080fae
writeFile() Android and iOS: improve errors; ReadStream: Add "ENOENT"…
lll000111 Aug 17, 2017
36195ca
oops - one "}" too many - removed
lll000111 Aug 17, 2017
9cde86a
add EISDIR error to readFile()s error vocabulary (iOS and Android)
lll000111 Aug 17, 2017
54afd01
"or directory" is misplaced in a "no such file" error message for rea…
lll000111 Aug 17, 2017
ea6e4d9
Android: two reject() calls did not have a code, iOS: slice() did not…
lll000111 Aug 17, 2017
9d64f5b
writeStream: return ENOENT, EISDIR and EUNSPECIFIED according to the …
lll000111 Aug 17, 2017
3394cd1
"+ +" was one plus sign too many
lll000111 Aug 17, 2017
354dc48
this if has a whole block (that ois why I prefer a style where {} are…
lll000111 Aug 17, 2017
ef9745d
I renamed this variable
lll000111 Aug 17, 2017
3292b38
1) #491 "writeStream() does not create file if it doesn't exist?"
lll000111 Aug 17, 2017
131dab2
Java: getParentFolder() may return null - prevent a NullPointerExcept…
lll000111 Aug 17, 2017
e74c944
Relating to #298 -- looping through an array is not supposed to be do…
lll000111 Aug 21, 2017
39abada
Merge branch '0.10.9' of github.com:lll000111/react-native-fetch-blob…
wkh237 Aug 27, 2017
dbafc98
Fix IOS syntax errors in #489
wkh237 Aug 27, 2017
0b90448
#489 Fix typo and missing return statement
wkh237 Aug 31, 2017
9dc2506
fix error code
wkh237 Aug 31, 2017
f00fafb
Merge branch '0.10.9' into 0.10.9
lll000111 Aug 31, 2017
c93e97e
Merge branch 'review-489' of https://github.com/wkh237/react-native-f…
lll000111 Aug 31, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
writeFile() Android and iOS: improve errors; ReadStream: Add "ENOENT"…
… (no such file) error event to Android version and add the thus far missing "code" parameter to iOS version
  • Loading branch information
lll000111 committed Aug 17, 2017
commit 1080fae5b6675fdf3cb287f3615c05642b021303
56 changes: 31 additions & 25 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,27 @@ static void writeFile(String path, String encoding, String data, final boolean a
int written;
File f = new File(path);
File dir = f.getParentFile();
if(!dir.exists()) {
boolean result = dir.mkdirs();
if (!result) {
promise.reject("EUNSPECIFIED", "Failed to create parent directory '" + path + "'");
return;
}
}

if(!f.exists()) {
boolean result = f.createNewFile();
if (!result) {
promise.reject("EUNSPECIFIED", "Failed to create file '" + path + "'");
if(!dir.exists()) {
if (!dir.mkdirs()) {
promise.reject("EUNSPECIFIED", "Failed to create parent directory '" + path + "'");
return;
}
}
if(!f.createNewFile()) {
promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created");
return;
}
}

FileOutputStream fout = new FileOutputStream(f, append);
// write data from a file
if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
String normalizedData = normalizePath(data);
File src = new File(normalizedData);
if (!src.exists()) {
promise.reject("ENOENT", "No such file '" + normalizedData + "'");
promise.reject("ENOENT", "No such file '" + path + "' " + "('" + normalizedData + "')");
fout.close();
return;
}
Expand Down Expand Up @@ -114,20 +114,20 @@ static void writeFile(String path, ReadableArray data, final boolean append, fin
try {
File f = new File(path);
File dir = f.getParentFile();
if(!dir.exists()) {
boolean result = dir.mkdirs();
if (!result) {
promise.reject("EUNSPECIFIED", "Failed to create parent directory '" + path + "'");
return;
}
}

if(!f.exists()) {
boolean result = f.createNewFile();
if (!result) {
promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created, or it is a directory");
if(!dir.exists()) {
if (!dir.mkdirs()) {
promise.reject("EUNSPECIFIED", "Failed to create parent directory '" + path + "'");
return;
}
}
if(!f.createNewFile()) {
promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created");
return;
}
}

FileOutputStream os = new FileOutputStream(f, append);
byte[] bytes = new byte[data.size()];
for(int i=0;i<data.size();i++) {
Expand All @@ -138,7 +138,7 @@ static void writeFile(String path, ReadableArray data, final boolean append, fin
promise.resolve(data.size());
} catch (FileNotFoundException e) {
// According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created, or it is a directory");
promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created");
} catch (Exception e) {
promise.reject("EUNSPECIFIED", e.getLocalizedMessage());
}
Expand Down Expand Up @@ -266,8 +266,8 @@ void readStream(String path, String encoding, int bufferSize, int tick, final St
String resolved = normalizePath(path);
if(resolved != null)
path = resolved;
try {

try {
int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
if(bufferSize > 0)
chunkSize = bufferSize;
Expand All @@ -276,7 +276,6 @@ void readStream(String path, String encoding, int bufferSize, int tick, final St

if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));

}
// fix issue 287
else if(resolved == null) {
Expand Down Expand Up @@ -336,7 +335,14 @@ else if(resolved == null) {
emitStreamEvent(streamId, "end", "");
fs.close();
buffer = null;

} catch (FileNotFoundException err) {
emitStreamEvent(
streamId,
"error",
"ENOENT",
"No such file '" + path + "'"
);
}
} catch (Exception err) {
emitStreamEvent(
streamId,
Expand Down
150 changes: 87 additions & 63 deletions ios/RNFetchBlobFS.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ + (void) readStream:(NSString *)uri
if([[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
{
NSString * message = [NSString stringWithFormat:@"File does not exist at path %@", path];
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"detail": message };
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"ENOENT", @"detail": message };
[event sendDeviceEventWithName:streamId body:payload];
free(buffer);
return ;
Expand Down Expand Up @@ -199,7 +199,7 @@ + (void) readStream:(NSString *)uri
}
else
{
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"detail": @"RNFetchBlob.readStream unable to resolve URI" };
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"ENOENT", @"detail": @"Unable to resolve URI" };
[event sendDeviceEventWithName:streamId body:payload];
}
// release buffer
Expand All @@ -209,7 +209,7 @@ + (void) readStream:(NSString *)uri
}
@catch (NSError * err)
{
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"detail": [NSString stringWithFormat:@"RNFetchBlob.readStream error %@", [err description]] };
NSDictionary * payload = @{ @"event": FS_EVENT_ERROR, @"code": @"EUNSPECIFIED", @"detail": [err description] };
[event sendDeviceEventWithName:streamId body:payload];
}
@finally
Expand Down Expand Up @@ -347,15 +347,21 @@ + (void) writeFile:(NSString *)path
NSString * folder = [path stringByDeletingLastPathComponent];
encoding = [encoding lowercaseString];

if(![fm fileExistsAtPath:folder]) {
BOOL isDir = NO;
BOOL exists = NO;
exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory: &isDir];

if (isDir) {
return reject(@"EISDIR", [NSString stringWithFormat:@"Expecting a file but '%@' is a directory", path], nil);
}

if(!exists) {
[fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
if(err != nil) {
reject(@"EUNSPECIFIED", [err description], nil);
return;
return reject(@"EUNSPECIFIED", @[[NSString stringWithFormat:@"Failed to create parent directory '%@', error: %@", path, [err description]]]], nil);
}
if(![fm createFileAtPath:path contents:nil attributes:nil]) {
reject(@"ENOENT", [NSString stringWithFormat:@"File '%@' does not exist and could not be created, or it is a directory", path], nil);
return;
return reject(@"ENOENT", [NSString stringWithFormat:@"File '%@' does not exist and could not be created", path], nil);
}
}

Expand Down Expand Up @@ -408,11 +414,19 @@ + (void) writeFileArray:(NSString *)path
// check if the folder exists, if not exists, create folders recursively
// after the folders created, write data into the file
NSString * folder = [path stringByDeletingLastPathComponent];
if(![fm fileExistsAtPath:folder]) {

BOOL isDir = NO;
BOOL exists = NO;
exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory: &isDir];

if (isDir) {
return reject(@"EISDIR", [NSString stringWithFormat:@"Expecting a file but '%@' is a directory", path], nil);
}

if(!exists) {
[fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
if(err != nil) {
reject(@"EUNSPECIFIED", [err description], nil);
return;
return reject(@"EUNSPECIFIED", @[[NSString stringWithFormat:@"Failed to create parent directory '%@', error: %@", path, [err description]]]], nil);
}
}

Expand All @@ -423,8 +437,11 @@ + (void) writeFileArray:(NSString *)path
bytes[i] = [[data objectAtIndex:i] charValue];
}
[fileContent appendBytes:bytes length:data.count];
if(![fm fileExistsAtPath:path]) {
[fm createFileAtPath:path contents:fileContent attributes:NULL];

if(!exists) {
if(![fm createFileAtPath:path contents:fileContent attributes:NULL]) {
return reject(@"ENOENT", [NSString stringWithFormat:@"File '%@' does not exist and could not be created", path], nil);
}
}
// if file exists, write file
else {
Expand Down Expand Up @@ -513,73 +530,78 @@ + (void) readFile:(NSString *)path

# pragma mark - hash

RCT_EXPORT_METHOD(hash:(NSString *)filepath
RCT_EXPORT_METHOD(hash:(NSString *)path
algorithm:(NSString *)algorithm
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath];

if (!fileExists) {
return reject(@"ENOENT", [NSString stringWithFormat:@"No such file '%@'", filepath], nil);
}
BOOL isDir = NO;
BOOL exists = NO;
exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory: &isDir];

NSError *error = nil;
if (isDir) {
return reject(@"EISDIR", [NSString stringWithFormat:@"Expecting a file but '%@' is a directory", path], nil);
}
if (!fileExists) {
return reject(@"ENOENT", [NSString stringWithFormat:@"No such file '%@'", path], nil);
}

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error];
NSError *error = nil;

if (error) {
return [self reject:reject withError:error];
}
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&error];

if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) {
return reject(@"EISDIR", [NSString stringWithFormat:@"Expecting a file but '%@' is a directory", filepath], nil);
}
if (error) {
return [self reject:reject withError:error];
}

NSData *content = [[NSFileManager defaultManager] contentsAtPath:filepath];
if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) {
return reject(@"EISDIR", [NSString stringWithFormat:@"Expecting a file but '%@' is a directory", path], nil);
}

NSArray *keys = [NSArray arrayWithObjects:@"md5", @"sha1", @"sha224", @"sha256", @"sha384", @"sha512", nil];
NSData *content = [[NSFileManager defaultManager] contentsAtPath:path];

NSArray *digestLengths = [NSArray arrayWithObjects:
@CC_MD5_DIGEST_LENGTH,
@CC_SHA1_DIGEST_LENGTH,
@CC_SHA224_DIGEST_LENGTH,
@CC_SHA256_DIGEST_LENGTH,
@CC_SHA384_DIGEST_LENGTH,
@CC_SHA512_DIGEST_LENGTH,
nil];
NSArray *keys = [NSArray arrayWithObjects:@"md5", @"sha1", @"sha224", @"sha256", @"sha384", @"sha512", nil];

NSDictionary *keysToDigestLengths = [NSDictionary dictionaryWithObjects:digestLengths forKeys:keys];
NSArray *digestLengths = [NSArray arrayWithObjects:
@CC_MD5_DIGEST_LENGTH,
@CC_SHA1_DIGEST_LENGTH,
@CC_SHA224_DIGEST_LENGTH,
@CC_SHA256_DIGEST_LENGTH,
@CC_SHA384_DIGEST_LENGTH,
@CC_SHA512_DIGEST_LENGTH,
nil];

int digestLength = [[keysToDigestLengths objectForKey:algorithm] intValue];
NSDictionary *keysToDigestLengths = [NSDictionary dictionaryWithObjects:digestLengths forKeys:keys];

if (!digestLength) {
return reject(@"EINVAL", [NSString stringWithFormat:@"Invalid algorithm '%@', must be one of md5, sha1, sha224, sha256, sha384, sha512", algorithm], nil);
}
int digestLength = [[keysToDigestLengths objectForKey:algorithm] intValue];

unsigned char buffer[digestLength];
if (!digestLength) {
return reject(@"EINVAL", [NSString stringWithFormat:@"Invalid algorithm '%@', must be one of md5, sha1, sha224, sha256, sha384, sha512", algorithm], nil);
}

if ([algorithm isEqualToString:@"md5"]) {
CC_MD5(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha1"]) {
CC_SHA1(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha224"]) {
CC_SHA224(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha256"]) {
CC_SHA256(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha384"]) {
CC_SHA384(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha512"]) {
CC_SHA512(content.bytes, (CC_LONG)content.length, buffer);
} else {
return reject(@"EINVAL", [NSString stringWithFormat:@"Invalid algorithm '%@', must be one of md5, sha1, sha224, sha256, sha384, sha512", algorithm], nil);
}
unsigned char buffer[digestLength];

if ([algorithm isEqualToString:@"md5"]) {
CC_MD5(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha1"]) {
CC_SHA1(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha224"]) {
CC_SHA224(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha256"]) {
CC_SHA256(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha384"]) {
CC_SHA384(content.bytes, (CC_LONG)content.length, buffer);
} else if ([algorithm isEqualToString:@"sha512"]) {
CC_SHA512(content.bytes, (CC_LONG)content.length, buffer);
} else {
return reject(@"EINVAL", [NSString stringWithFormat:@"Invalid algorithm '%@', must be one of md5, sha1, sha224, sha256, sha384, sha512", algorithm], nil);
}

NSMutableString *output = [NSMutableString stringWithCapacity:digestLength * 2];
for(int i = 0; i < digestLength; i++)
[output appendFormat:@"%02x",buffer[i]];
NSMutableString *output = [NSMutableString stringWithCapacity:digestLength * 2];
for(int i = 0; i < digestLength; i++)
[output appendFormat:@"%02x",buffer[i]];

resolve(output);
resolve(output);
}

# pragma mark - mkdir
Expand Down Expand Up @@ -740,7 +762,9 @@ + (void)slice:(NSString *)path
long max = MIN(size, [end longValue]);

if(![fm fileExistsAtPath:dest]) {
[fm createFileAtPath:dest contents:@"" attributes:nil];
if(![fm createFileAtPath:dest contents:@"" attributes:nil]) {
return reject(@"ENOENT", [NSString stringWithFormat:@"File '%@' does not exist and could not be created", path], nil);
}
}
[handle seekToFileOffset:[start longValue]];
while(read < expected) {
Expand Down