Skip to content

Commit 840a3f0

Browse files
author
qianyu
committed
更新react-native-blob-util的harmany实现
1 parent 618685f commit 840a3f0

File tree

12 files changed

+91
-138
lines changed

12 files changed

+91
-138
lines changed

.github/ISSUE_TEMPLATE

Lines changed: 0 additions & 7 deletions
This file was deleted.

.github/workflows/npm_publish.yml

Lines changed: 0 additions & 36 deletions
This file was deleted.

android.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ export default {
5656
getSDCardDir,
5757
getSDCardApplicationDir,
5858
};
59+

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ static public void getSDCardDir(ReactApplicationContext ctx, Promise promise) {
425425
} else {
426426
promise.reject("ReactNativeBlobUtil.getSDCardDir", "External storage not mounted");
427427
}
428-
429428
}
430429

431430
static public void getSDCardApplicationDir(ReactApplicationContext ctx, Promise promise) {

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ public void onHostResume() {
139139

140140
@Override
141141
public void onHostPause() {
142-
143142
}
144143

145144
@Override
146145
public void onHostDestroy() {
147-
148146
}
149147
};
150148
RCTContext.addLifecycleEventListener(listener);
@@ -334,7 +332,6 @@ public void getContentIntent(String mime, Promise promise) {
334332
i.setType("*/*");
335333
promiseTable.put(GET_CONTENT_INTENT, promise);
336334
RCTContext.startActivityForResult(i, GET_CONTENT_INTENT, null);
337-
338335
}
339336

340337
public void addCompleteDownload(ReadableMap config, Promise promise) {
@@ -363,7 +360,6 @@ public void addCompleteDownload(ReadableMap config, Promise promise) {
363360
} catch (Exception ex) {
364361
promise.reject("EUNSPECIFIED", ex.getLocalizedMessage());
365362
}
366-
367363
}
368364

369365
public void getSDCardDir(Promise promise) {

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,6 @@ public static boolean writeToMediaFile(Uri fileUri, String data, boolean transfo
133133
try {
134134
Context appCtx = ctx.getApplicationContext();
135135
ContentResolver resolver = appCtx.getContentResolver();
136-
137-
// set pending doesn't work right now. We would have to requery for the item
138-
//ContentValues contentValues = new ContentValues();
139-
//contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
140-
//resolver.update(fileUri, contentValues, null, null);
141-
142-
// write data
143136
OutputStream stream = null;
144137
Uri uri = null;
145138

@@ -155,11 +148,8 @@ public static boolean writeToMediaFile(Uri fileUri, String data, boolean transfo
155148
promise.reject("ENOENT", "No such file ('" + normalizedData + "')");
156149
return false;
157150
}
158-
159-
160151
FileInputStream fin = new FileInputStream(src);
161152
FileOutputStream out = new FileOutputStream(descr.getFileDescriptor());
162-
163153
if (transformFile) {
164154
// in order to transform file, we must load the entire file onto memory
165155
int length = (int) src.length();
@@ -178,8 +168,6 @@ public static boolean writeToMediaFile(Uri fileUri, String data, boolean transfo
178168
out.write(buf, 0, read);
179169
}
180170
}
181-
182-
183171
fin.close();
184172
out.close();
185173
descr.close();
@@ -188,10 +176,6 @@ public static boolean writeToMediaFile(Uri fileUri, String data, boolean transfo
188176
promise.reject(new IOException("Failed to get output stream."));
189177
return false;
190178
}
191-
192-
//contentValues.clear();
193-
//contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
194-
//appCtx.getContentResolver().update(fileUri, contentValues, null, null);
195179
stream = resolver.openOutputStream(fileUri);
196180
if (stream == null) {
197181
promise.reject(new IOException("Failed to get output stream."));
@@ -207,12 +191,6 @@ public static boolean writeToMediaFile(Uri fileUri, String data, boolean transfo
207191
stream.close();
208192
}
209193
}
210-
211-
// remove pending
212-
//contentValues = new ContentValues();
213-
//contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0);
214-
//resolver.update(fileUri, contentValues, null, null);
215-
216194
} catch (IOException e) {
217195
promise.reject("ReactNativeBlobUtil.createMediaFile", "Cannot write to file, file might not exist");
218196
return false;
@@ -226,11 +204,9 @@ public static boolean writeToMediaFile(Uri fileUri, String data, boolean transfo
226204
public static void copyToInternal(Uri contenturi, String destpath, Promise promise) {
227205
Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
228206
ContentResolver resolver = appCtx.getContentResolver();
229-
230207
InputStream in = null;
231208
OutputStream out = null;
232209
File f = new File((destpath));
233-
234210
if (!f.exists()) {
235211
try {
236212
File parent = f.getParentFile();
@@ -247,7 +223,6 @@ public static void copyToInternal(Uri contenturi, String destpath, Promise promi
247223
promise.reject("ReactNativeBlobUtil.copyToInternal: Could not create file: " + ioException.getLocalizedMessage());
248224
}
249225
}
250-
251226
try {
252227
in = resolver.openInputStream(contenturi);
253228
out = new FileOutputStream(destpath);
@@ -276,7 +251,6 @@ public static void copyToInternal(Uri contenturi, String destpath, Promise promi
276251
}
277252
}
278253
}
279-
280254
promise.resolve("");
281255
}
282256

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ private void done(Response resp) {
678678
try (FileOutputStream fos = new FileOutputStream(file)) {
679679
fos.write(ReactNativeBlobUtilFileTransformer.sharedFileTransformer.onWriteFile(b));
680680
} catch (Exception e) {
681-
invoke_callback("Error from file transformer:" + e.getLocalizedMessage(), respmap.copy());
681+
invoke_callback("Error from file transformer:" + e.getLocalizedMessage(), null);
682682
return;
683683
}
684684
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath, respmap.copy());
@@ -709,7 +709,7 @@ private void done(Response resp) {
709709
}
710710
}
711711
} catch (IOException e) {
712-
invoke_callback("ReactNativeBlobUtil failed to encode response data to BASE64 string.", respmap.copy());
712+
invoke_callback("ReactNativeBlobUtil failed to encode response data to BASE64 string.", null);
713713
}
714714
break;
715715
case FileStorage:
@@ -741,15 +741,15 @@ private void done(Response resp) {
741741
} catch (IOException exception) {
742742
exception.printStackTrace();
743743
}
744-
invoke_callback("Unexpected FileStorage response file: " + responseBodyString, respmap.copy());
744+
invoke_callback("Unexpected FileStorage response file: " + responseBodyString, null);
745745
} else {
746-
invoke_callback("Unexpected FileStorage response with no file.", respmap.copy());
746+
invoke_callback("Unexpected FileStorage response with no file.", null);
747747
}
748748
return;
749749
}
750750

751751
if (ReactNativeBlobUtilFileResp != null && !ReactNativeBlobUtilFileResp.isDownloadComplete()) {
752-
invoke_callback("Download interrupted.", respmap.copy());
752+
invoke_callback("Download interrupted.", null, respmap.copy());
753753
} else {
754754
this.destPath = this.destPath.replace("?append=true", "");
755755
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath, respmap.copy());
@@ -760,7 +760,7 @@ private void done(Response resp) {
760760
try {
761761
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"), respmap.copy());
762762
} catch (IOException e) {
763-
invoke_callback("ReactNativeBlobUtil failed to encode response data to UTF8 string.", respmap.copy());
763+
invoke_callback("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
764764
}
765765
break;
766766
}

codegenSpecs/NativeBlobUtils.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ export interface Spec extends TurboModule {
2727
LegacyRingtoneDir: string,
2828
LegacySDCardDir: string,
2929
|};
30-
31-
+fetchBlobForm: (options: Object, taskId: string, method: string, url: string, headers: Object, form: Array<any>, callback: (value: Array<any>) => void) => void;
32-
+fetchBlob: (options: Object, taskId: string, method: string, url: string, headers: Object, body: string, callback: (value: Array<any>) => void) => void;
33-
+createFile: (path: string, data: string, encoding: string) => Promise<void>;
30+
//+fetchBlobForm: (options: Object, taskId: string, method: string, url: string, headers: Object, form: Array<any>, callback: (value: Array<any>) => void) => void;
31+
//+fetchBlob: (options: Object, taskId: string, method: string, url: string, headers: Object, body: string, callback: (value: Array<any>) => void) => void;
32+
+createFile: (path: string, data: string, encoding: string) => Promise<string>;
3433
+createFileASCII: (path: string, data: Array<any>) => Promise<void>;
35-
+pathForAppGroup: (groupName: string) => Promise<string>;
36-
+syncPathAppGroup: (groupName: string) => string;
34+
//+pathForAppGroup: (groupName: string) => Promise<string>;
35+
//+syncPathAppGroup: (groupName: string) => string;
3736
+exists: (path: string, callback: (value: Array<boolean>) => void) => void;
3837
+writeFile: (path: string, encoding: string, data: string, transformFile: boolean, append: boolean) => Promise<number>;
3938
+writeFileArray: (path: string, data: Array<any>, append: boolean) => Promise<number>;
@@ -52,18 +51,17 @@ export interface Spec extends TurboModule {
5251
+readFile: (path: string, encoding: string, transformFile: boolean) => Promise<Array<any>>;
5352
+hash: (path: string, algorithm: string) => Promise<string>;
5453
+readStream: (path: string, encoding: string, bufferSize: number, tick: number, streamId: string) => void;
55-
+getEnvironmentDirs: (callback: (value: Array<any>) => void) => void;
56-
+cancelRequest: (taskId: string, callback: (value: Array<any>) => void) => void;
57-
+enableProgressReport: (taskId: string, interval: number, count: number) => void;
58-
+enableUploadProgressReport: (taskId: string, interval: number, count: number) => void;
54+
//+getEnvironmentDirs: (callback: (value: Array<any>) => void) => void;
55+
//+cancelRequest: (taskId: string, callback: (value: Array<any>) => void) => void;
56+
//+enableProgressReport: (taskId: string, interval: number, count: number) => void;
57+
//+enableUploadProgressReport: (taskId: string, interval: number, count: number) => void;
5958
+slice: (src: string, dest: string, start: number, end: number) => Promise<string>;
6059
+presentOptionsMenu: (uri: string, scheme: string) => Promise<Array<any>>;
6160
+presentOpenInMenu: (uri: string, scheme: string) => Promise<Array<any>>;
6261
+presentPreview: (uri: string, scheme: string) => Promise<Array<any>>;
6362
+excludeFromBackupKey: (url: string) => Promise<Array<any>>;
6463
+df: (callback: (value: Array<any>) => void) => void;
65-
+emitExpiredEvent: (callback: (value: string) => void) => void; // The callback is not really used here
66-
// Android Only APIs
64+
//+emitExpiredEvent: (callback: (value: string) => void) => void; // The callback is not really used here
6765
+actionViewIntent: (path: string, mime: string, chooserTitle: string) => Promise<void>;
6866
+addCompleteDownload: (config: Object) => Promise<void>;
6967
+copyToInternal: (contentUri: string, destpath: string) => Promise<string>;
@@ -75,10 +73,9 @@ export interface Spec extends TurboModule {
7573
+getSDCardApplicationDir: () => Promise<string>;
7674
+scanFile: (pairs: Array<any>, callback: (value: Array<any>) => void) => void;
7775
+writeToMediaFile: (fileUri: string, path: string, transformFile: boolean) => Promise<string>;
78-
79-
// RCTEventEmitter
8076
+addListener: (eventName: string) => void;
8177
+removeListeners: (count: number) => void;
78+
8279
}
8380

8481
export default (TurboModuleRegistry.get<Spec>('ReactNativeBlobUtil'): ?Spec);

fetch.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import {ReactNativeBlobUtilConfig} from './types';
22
import URIUtil from './utils/uri';
33
import fs from './fs';
44
import getUUID from './utils/uuid';
5-
import {NativeEventEmitter} from 'react-native';
5+
import {NativeEventEmitter,RCTDeviceEventEmitter} from 'react-native';
66
import {FetchBlobResponse} from './class/ReactNativeBlobUtilBlobResponse';
77
import CanceledFetchError from './class/ReactNativeBlobUtilCanceledFetchError';
88
import ReactNativeBlobUtil from './codegenSpecs/NativeBlobUtils';
99

1010
const eventEmitter = new NativeEventEmitter(ReactNativeBlobUtil);
1111

12+
1213
// register message channel event handler.
1314
eventEmitter.addListener('ReactNativeBlobUtilMessage', (e) => {
1415
if (typeof e === 'string') e = JSON.parse(e);
@@ -245,7 +246,6 @@ export function fetch(...args: any): Promise {
245246
* @param responseInfo {Object.<>}
246247
*/
247248
req(options, taskId, method, url, headers || {}, body, (err, rawType, data, responseInfo) => {
248-
249249
// task done, remove event listeners
250250
subscription.remove();
251251
subscriptionUpload.remove();
@@ -260,8 +260,6 @@ export function fetch(...args: any): Promise {
260260
promise.cancel = () => {
261261
};
262262

263-
if(!responseInfo) responseInfo = {}; // should not be null / undefined
264-
265263
if (err)
266264
reject(new Error(err, respInfo));
267265
else {
@@ -271,10 +269,9 @@ export function fetch(...args: any): Promise {
271269
if (options.session)
272270
fs.session(options.session).add(data);
273271
}
272+
respInfo.rnfbEncode = rawType;
274273
if ('uninit' in respInfo && respInfo.uninit) // event didn't fire yet so we override it here
275274
respInfo = responseInfo;
276-
277-
respInfo.rnfbEncode = rawType;
278275
resolve(new FetchBlobResponse(taskId, respInfo, data));
279276
}
280277

@@ -344,7 +341,5 @@ export function fetch(...args: any): Promise {
344341
promiseReject(new CanceledFetchError('canceled'));
345342
};
346343
promise.taskId = taskId;
347-
348344
return promise;
349-
350345
}

fs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function readStream(
117117
bufferSize?: number,
118118
tick?: number = 10
119119
): Promise<ReactNativeBlobUtilReadStream> {
120+
ReactNativeBlobUtil.readStream(path, encoding, bufferSize, tick);
120121
if (typeof path !== 'string') {
121122
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
122123
}

0 commit comments

Comments
 (0)