Skip to content

Commit

Permalink
Removed attributes, cleaned up README
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdell committed Aug 2, 2016
1 parent 03a61b6 commit 3564b6b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
8 changes: 2 additions & 6 deletions FS.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ type StatResult = {
isDirectory: () => boolean; // Is the file a directory?
};

type WriteFileOptions = {
// iOS only. See https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/doc/constant_group/File_Attribute_Keys
};

type Headers = { [name: string]: string };
type Fields = { [name: string]: string };

Expand Down Expand Up @@ -247,7 +243,7 @@ var RNFS = {
throw new Error('Invalid encoding type "' + options.encoding + '"');
}

return RNFSManager.writeFile(filepath, b64, {}).then(() => void 0);
return RNFSManager.writeFile(filepath, b64).then(() => void 0);
},

appendFile(filepath: string, contents: string, encodingOrOptions?: any): Promise<void> {
Expand Down Expand Up @@ -275,7 +271,7 @@ var RNFS = {
throw new Error('Invalid encoding type "' + options.encoding + '"');
}

return RNFSManager.appendFile(filepath, b64, {});
return RNFSManager.appendFile(filepath, b64);
},

downloadFile(options: DownloadFileOptions): Promise<DownloadResult> {
Expand Down
50 changes: 23 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,8 @@ RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
var path = RNFS.DocumentDirectoryPath + '/test.txt';

return RNFS.unlink(path)
// spread is a method offered by bluebird to allow for more than a
// single return value of a promise. If you use `then`, you will receive
// the values inside of an array
.spread((success, path) => {
console.log('FILE DELETED', success, path);
.then(() => {
console.log('FILE DELETED');
})
// `unlink` will throw an error, if the item to unlink does not exist
.catch((err) => {
Expand Down Expand Up @@ -219,19 +216,18 @@ var uploadProgress = (response) => {

// upload files
RNFS.uploadFiles({
toUrl: uploadUrl,
files: files,
method: 'POST',
headers: {
'Accept': 'application/json',
},
fields: {
'hello': 'world',
},
begin: uploadBegin,
progress: uploadProgress
})
.then((response) => {
toUrl: uploadUrl,
files: files,
method: 'POST',
headers: {
'Accept': 'application/json',
},
fields: {
'hello': 'world',
},
begin: uploadBegin,
progress: uploadProgress
}).then((response) => {
if (response.statusCode == 200) {
console.log('FILES UPLOADED!'); // response.statusCode, response.headers, response.body
} else {
Expand Down Expand Up @@ -301,16 +297,10 @@ Reads the file at `path` and return contents. `encoding` can be one of `utf8` (d

Note: you will take quite a performance hit if you are reading big files

### `writeFile(filepath: string, contents: string, encoding?: string, options?: WriteFileOptions): Promise<void>`
### `writeFile(filepath: string, contents: string, encoding?: string): Promise<void>`

Write the `contents` to `filepath`. `encoding` can be one of `utf8` (default), `ascii`, `base64`. `options` optionally takes an object specifying the file's properties, like mode etc.

```
type WriteFileOptions = {
// iOS only. See https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html#//apple_ref/doc/constant_group/File_Attribute_Keys
};
```

### `appendFile(filepath: string, contents: string, encoding?: string): Promise<void>`

Append the `contents` to `filepath`. `encoding` can be one of `utf8` (default), `ascii`, `base64`.
Expand All @@ -327,8 +317,6 @@ Copies the file located at `filepath` to `destPath`.

Unlinks the item at `filepath`. If the item does not exist, an error will be thrown.

The promise resolves with an array, which contains a boolean and the path that has been unlinked. Tip: use `spread` to receive the two arguments instead of a single array in your handler.

Also recursively deletes directories (works like Linux `rm -rf`).

### `exists(filepath: string): Promise<boolean>`
Expand Down Expand Up @@ -416,6 +404,14 @@ type UploadFileOptions = {
progress?: (res: UploadProgressCallbackResult) => void;
};

```
```
type UploadResult = {
jobId: number; // The upload job ID, required if one wishes to cancel the upload. See `stopUpload`.
statusCode: number; // The HTTP status code
headers: Headers; // The HTTP response headers from the server
body: string; // The HTTP response body
};
```

Each file should have the following structure:
Expand Down
6 changes: 2 additions & 4 deletions RNFSManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,12 @@ - (dispatch_queue_t)methodQueue

RCT_EXPORT_METHOD(writeFile:(NSString *)filepath
contents:(NSString *)base64Content
attributes:(NSDictionary *)attributes
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Content options:NSDataBase64DecodingIgnoreUnknownCharacters];

BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:attributes];
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:nil];

if (!success) {
return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil);
Expand All @@ -109,7 +108,6 @@ - (dispatch_queue_t)methodQueue

RCT_EXPORT_METHOD(appendFile:(NSString *)filepath
contents:(NSString *)base64Content
attributes:(NSDictionary *)attributes
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
Expand All @@ -119,7 +117,7 @@ - (dispatch_queue_t)methodQueue

if (![fM fileExistsAtPath:filepath])
{
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:attributes];
BOOL success = [[NSFileManager defaultManager] createFileAtPath:filepath contents:data attributes:nil];

if (!success) {
return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil);
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/com/rnfs/RNFSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getName() {
}

@ReactMethod
public void writeFile(String filepath, String base64Content, ReadableMap options, Promise promise) {
public void writeFile(String filepath, String base64Content, Promise promise) {
try {
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);

Expand All @@ -68,7 +68,7 @@ public void writeFile(String filepath, String base64Content, ReadableMap options
}

@ReactMethod
public void appendFile(String filepath, String base64Content, ReadableMap options, Promise promise) {
public void appendFile(String filepath, String base64Content, Promise promise) {
try {
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);

Expand Down

0 comments on commit 3564b6b

Please sign in to comment.