Skip to content

Commit

Permalink
Prepare files updates (#54)
Browse files Browse the repository at this point in the history
updates to new lib
  • Loading branch information
andrewxhill committed Mar 18, 2019
1 parent ce778cf commit fa8c131
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 36 deletions.
51 changes: 45 additions & 6 deletions android/src/main/java/io/textile/rnmobile/TextileNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,21 @@ public void run() {
// Files ---------------->

@ReactMethod
public void prepareFiles(final String path, final String threadId, final Promise promise) {
public void prepareFiles(final String strBase64, final String threadId, final Promise promise) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
promise.resolve(encode(node.prepareFiles(path, threadId)));
node.prepareFiles(strBase64, threadId, new Callback() {
@Override
public void call(byte[] data, Exception e) {
if (e == null) {
promise.resolve(encode(data));
} else {
promise.reject("prepareFiles", e);
}
}
});
}
catch (Exception e) {
promise.reject("prepareFiles", e);
Expand All @@ -386,24 +395,54 @@ public void run() {
}

@ReactMethod
public void prepareFilesAsync(final String path, final String threadId, final Promise promise) {
public void prepareFilesSync(final String strBase64, final String threadId, final Promise promise) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
promise.resolve(encode(node.prepareFilesSync(strBase64, threadId)));
}
catch (Exception e) {
promise.reject("prepareFilesSync", e);
}
}
});
}

@ReactMethod
public void prepareFilesByPath(final String path, final String threadId, final Promise promise) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
node.prepareFilesAsync(path, threadId, new Callback() {
node.prepareFilesByPath(path, threadId, new Callback() {
@Override
public void call(byte[] data, Exception e) {
if (e == null) {
promise.resolve(encode(data));
} else {
promise.reject("prepareFilesAsync", e);
promise.reject("prepareFilesByPath", e);
}
}
});
}
catch (Exception e) {
promise.reject("prepareFilesAsync", e);
promise.reject("prepareFilesByPath", e);
}
}
});
}

@ReactMethod
public void prepareFilesByPathSync(final String path, final String threadId, final Promise promise) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
promise.resolve(encode(node.prepareFilesByPathSync(path, threadId)));
}
catch (Exception e) {
promise.reject("prepareFilesByPathSync", e);
}
}
});
Expand Down
92 changes: 80 additions & 12 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,10 @@ API.data('QmTgtbb4LckHaXh1YhpNcBu48cFY8zgT1Lh49q7q7ksf3M');
* [list](#list)
* [peerId](#peerid)
* [peers](#peers)
* [prepare](#prepare)
* [prepareAsync](#prepareasync)
* [prepareFiles](#preparefiles)
* [prepareFilesByPath](#preparefilesbypath)
* [prepareFilesByPathSync](#preparefilesbypathsync)
* [prepareFilesSync](#preparefilessync)
* [read](#read)
* [readAll](#readall)
* [refreshSession](#refreshsession)
Expand All @@ -238,6 +240,7 @@ API.data('QmTgtbb4LckHaXh1YhpNcBu48cFY8zgT1Lh49q7q7ksf3M');
* [stop](#stop)
* [summary](#summary)
* [threads](#threads)
* [timestampToDate](#timestamptodate)
* [username](#username)
* [version](#version)

Expand Down Expand Up @@ -789,16 +792,38 @@ const contacts: pb.IContactList = API.account.peers();
**Returns:** `Promise`<`IContactList`>

___
<a id="prepare"></a>
<a id="preparefiles"></a>

### prepare
### prepareFiles

**prepare**(path: *`string`*, threadId: *`string`*): `Promise`<`IMobilePreparedFiles`>
**prepareFiles**(data: *`string`*, threadId: *`string`*): `Promise`<`IMobilePreparedFiles`>

Use a Thread's Mill to prepare a raw file for adding to a Thread.
Use a Thread's Mill to prepare a file data (as bse64 string) for adding to a Thread.

```typescript
API.files.prepare(path, threadId);
API.files.prepareFiles(data, threadId);
```

**Parameters:**

| Name | Type |
| ------ | ------ |
| data | `string` |
| threadId | `string` |

**Returns:** `Promise`<`IMobilePreparedFiles`>

___
<a id="preparefilesbypath"></a>

### prepareFilesByPath

**prepareFilesByPath**(path: *`string`*, threadId: *`string`*): `Promise`<`IMobilePreparedFiles`>

Use a Thread's Mill to prepare a file for adding to a Thread.

```typescript
API.files.prepareFilesByPath(path, threadId);
```

**Parameters:**
Expand All @@ -811,16 +836,16 @@ API.files.prepare(path, threadId);
**Returns:** `Promise`<`IMobilePreparedFiles`>

___
<a id="prepareasync"></a>
<a id="preparefilesbypathsync"></a>

### prepareAsync
### prepareFilesByPathSync

**prepareAsync**(path: *`string`*, threadId: *`string`*): `Promise`<`IMobilePreparedFiles`>
**prepareFilesByPathSync**(path: *`string`*, threadId: *`string`*): `Promise`<`IMobilePreparedFiles`>

prepare by async
Use a Thread's Mill to synchronously prepare a file for adding to a Thread.

```typescript
API.files.prepareAsync(path, threadId);
API.files.prepareFilesByPathSync(path, threadId);
```

**Parameters:**
Expand All @@ -832,6 +857,28 @@ API.files.prepareAsync(path, threadId);

**Returns:** `Promise`<`IMobilePreparedFiles`>

___
<a id="preparefilessync"></a>

### prepareFilesSync

**prepareFilesSync**(data: *`string`*, threadId: *`string`*): `Promise`<`IMobilePreparedFiles`>

Use a Thread's Mill to synchronously prepare a file data (as bse64 string) for adding to a Thread.

```typescript
API.files.prepareFiles(data, threadId);
```

**Parameters:**

| Name | Type |
| ------ | ------ |
| data | `string` |
| threadId | `string` |

**Returns:** `Promise`<`IMobilePreparedFiles`>

___
<a id="read"></a>

Expand Down Expand Up @@ -1152,6 +1199,27 @@ API.contacts.threads(id);

**Returns:** `Promise`<`IThreadList`>

___
<a id="timestamptodate"></a>

### timestampToDate

**timestampToDate**(timestamp?: *`pb.google.protobuf.ITimestamp`*): `Date`

Converts a protobuf timestamp into a Javascript Date

```typescript
utils.timestampToDate(timestamp);
```

**Parameters:**

| Name | Type |
| ------ | ------ |
| `Optional` timestamp | `pb.google.protobuf.ITimestamp` |

**Returns:** `Date`

___
<a id="username"></a>

Expand Down
28 changes: 24 additions & 4 deletions ios/TextileNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,28 @@ - (void)fulfillWithResult:(id)result error:(NSError*)error resolver:(RCTPromiseR

#pragma mark - Files ---------------->

RCT_EXPORT_METHOD(prepareFiles:(NSString*)path threadId:(NSString*)threadId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
RCT_EXPORT_METHOD(prepareFiles:(NSString*)strBase64 threadId:(NSString*)threadId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {

// NSData *fileData = [[NSData alloc] initWithBase64EncodedString:dataStr options:0];

[self.node prepareFiles:strBase64 threadId:threadId cb:[[Callback alloc] initWithCompletion:^ (NSData *data, NSError *error) {
if (error) {
reject(@(error.code).stringValue, error.localizedDescription, error);
} else {
resolve([data base64EncodedStringWithOptions:0]);
}
}]];
}

RCT_EXPORT_METHOD(prepareFilesSync:(NSString*)strBase64 threadId:(NSString*)threadId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSError *error;
NSData *result = [self.node prepareFiles:path threadId:threadId error:&error];
// NSData *fileData = [[NSData alloc] initWithBase64EncodedString:dataStr options:0];
NSData *result = [self.node prepareFilesSync:strBase64 threadId:threadId error:&error];
[self fulfillWithResult:[result base64EncodedStringWithOptions:0] error:error resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(prepareFilesAsync:(NSString*)path threadId:(NSString*)threadId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
[self.node prepareFilesAsync:path threadId:threadId cb:[[Callback alloc] initWithCompletion:^ (NSData *data, NSError *error) {
RCT_EXPORT_METHOD(prepareFilesByPath:(NSString*)path threadId:(NSString*)threadId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
[self.node prepareFilesByPath:path threadId:threadId cb:[[Callback alloc] initWithCompletion:^ (NSData *data, NSError *error) {
if (error) {
reject(@(error.code).stringValue, error.localizedDescription, error);
} else {
Expand All @@ -241,6 +255,12 @@ - (void)fulfillWithResult:(id)result error:(NSError*)error resolver:(RCTPromiseR
}]];
}

RCT_EXPORT_METHOD(prepareFilesByPathSync:(NSString*)path threadId:(NSString*)threadId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSError *error;
NSData *result = [self.node prepareFilesByPathSync:path threadId:threadId error:&error];
[self fulfillWithResult:[result base64EncodedStringWithOptions:0] error:error resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(addFiles:(NSString*)dirStr threadId:(NSString*)threadId caption:(NSString*)caption resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSError *error;
NSData *dir = [[NSData alloc] initWithBase64EncodedString:dirStr options:0];
Expand Down
36 changes: 28 additions & 8 deletions lib/Textile/API/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,43 @@ import { pb } from '../Models'

const { TextileNode } = NativeModules
/**
* Use a Thread's Mill to prepare a raw file for adding to a Thread.
* Use a Thread's Mill to prepare a file data (as bse64 string) for adding to a Thread.
* ```typescript
* API.files.prepare(path, threadId);
* API.files.prepareFiles(data, threadId);
* ```
*/
export async function prepare(path: string, threadId: string): Promise<pb.IMobilePreparedFiles> {
const result = await TextileNode.prepareFiles(path, threadId)
export async function prepareFiles(data: string, threadId: string): Promise<pb.IMobilePreparedFiles> {
const result = await TextileNode.prepareFiles(data, threadId)
return pb.MobilePreparedFiles.decode(Buffer.from(result, 'base64'))
}
/**
* prepare by async
* Use a Thread's Mill to synchronously prepare a file data (as bse64 string) for adding to a Thread.
* ```typescript
* API.files.prepareAsync(path, threadId);
* API.files.prepareFiles(data, threadId);
* ```
*/
export async function prepareAsync(path: string, threadId: string): Promise<pb.IMobilePreparedFiles> {
const result = await TextileNode.prepareFilesAsync(path, threadId)
export async function prepareFilesSync(data: string, threadId: string): Promise<pb.IMobilePreparedFiles> {
const result = await TextileNode.prepareFilesSync(data, threadId)
return pb.MobilePreparedFiles.decode(Buffer.from(result, 'base64'))
}
/**
* Use a Thread's Mill to prepare a file for adding to a Thread.
* ```typescript
* API.files.prepareFilesByPath(path, threadId);
* ```
*/
export async function prepareFilesByPath(path: string, threadId: string): Promise<pb.IMobilePreparedFiles> {
const result = await TextileNode.prepareFilesByPath(path, threadId)
return pb.MobilePreparedFiles.decode(Buffer.from(result, 'base64'))
}
/**
* Use a Thread's Mill to synchronously prepare a file for adding to a Thread.
* ```typescript
* API.files.prepareFilesByPathSync(path, threadId);
* ```
*/
export async function prepareFilesByPathSync(path: string, threadId: string): Promise<pb.IMobilePreparedFiles> {
const result = await TextileNode.prepareFilesByPathSync(path, threadId)
return pb.MobilePreparedFiles.decode(Buffer.from(result, 'base64'))
}
/**
Expand Down
18 changes: 18 additions & 0 deletions lib/Textile/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { pb } from './Models'

/**
* Converts a protobuf timestamp into a Javascript Date
*
* ```typescript
* utils.timestampToDate(timestamp);
* ```
*/
export function timestampToDate(timestamp?: pb.google.protobuf.ITimestamp) {
let millis: number
if (!timestamp) {
millis = 0
} else {
millis = timestamp.seconds as number * 1e3 + timestamp.nanos / 1e6
}
return new Date(millis)
}
3 changes: 2 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Events, { TextileEvents, BackgroundTask } from './Textile/events'
import Textile from './Textile'
import * as API from './Textile/API'
import * as util from './Textile/util'

export { Textile, API, Events, BackgroundTask, TextileEvents }
export { Textile, API, Events, BackgroundTask, TextileEvents, util }

export * from './Textile/Models'

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@textile/react-native-sdk",
"version": "1.1.0-rc41",
"version": "1.1.0-rc42",
"description": "## Getting started",
"nativePackage": true,
"main": "dist/index.js",
Expand Down Expand Up @@ -65,7 +65,7 @@
"typescript": "^3.1.1"
},
"dependencies": {
"@textile/go-mobile": "1.0.0-rc55",
"@textile/go-mobile": "0.1.10-rc1",
"buffer": "^5.2.1"
},
"directories": {
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,9 @@
version "1.1.0"
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"

"@textile/go-mobile@1.0.0-rc55":
version "1.0.0-rc55"
resolved "https://registry.yarnpkg.com/@textile/go-mobile/-/go-mobile-1.0.0-rc55.tgz#f3d420c0dd758419f0ca4580f19ddcf8a7633844"
"@textile/go-mobile@0.1.10-rc1":
version "0.1.10-rc1"
resolved "https://registry.yarnpkg.com/@textile/go-mobile/-/go-mobile-0.1.10-rc1.tgz#a901ccb53a19355dac70a0fd36b8b1954e64b34c"
dependencies:
protobufjs "^6.8.8"

Expand Down

0 comments on commit fa8c131

Please sign in to comment.