Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

新增existsAssets,readDir,downloadFile接口 #5

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion FS.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var RNFS = {
},

// Android-only
existsAssets(filepath: string) {
existsAssets(filepath: string):Promise<boolean> {
if (!RNFSManager.existsAssets) {
throw new Error('existsAssets is not available on this platform');
}
Expand Down
Binary file modified harmony/fs.har
Binary file not shown.
2 changes: 1 addition & 1 deletion harmony/fs/oh-package.json5
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-native-oh-tpl/react-native-fs",
"version": "2.20.0-0.1.3",
"version": "2.20.0-0.1.4",
"description": "Please describe the basic information.",
"main": "index.ets",
"types": "module",
Expand Down
195 changes: 184 additions & 11 deletions harmony/fs/src/main/ets/FsTurboModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
* SOFTWARE.
*/

import { TurboModule, RNOHError } from '@rnoh/react-native-openharmony/ts';
import fs, { ReadTextOptions, WriteOptions } from '@ohos.file.fs';
import { TurboModule, RNOHError, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
import { TM } from "@rnoh/react-native-openharmony/generated/ts"
import fs, { ListFileOptions, ReadOptions, ReadTextOptions, WriteOptions } from '@ohos.file.fs';
import hash from '@ohos.file.hash';
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';
import resourceManager from '@ohos.resourceManager'
import util from '@ohos.util';
import loadRequest from '@ohos.request';
import buffer from '@ohos.buffer';
import HashMap from '@ohos.util.HashMap';
import Logger from './Logger';
import { Context } from '@ohos.abilityAccessCtrl';
import common from '@ohos.app.ability.common';

const TAG: string = "[RNOH] Fs"

Expand All @@ -43,11 +47,177 @@ interface StatResult {
type: number // Is the file just a file? Is the file a directory?
}

export class FsTurboModule extends TurboModule {
type Headers = { [name: string]: string }
type Fields = { [name: string]: string }

type DownloadFileOptions = {
jobId: number
fromUrl: string // URL to download file from
toFile: string // Local filesystem path to save the file to
headers?: Headers // An object of headers to be passed to the server
background?: boolean // Continue the download in the background after the app terminates (iOS only)
progressInterval?: number
progressDivider?: number
readTimeout?: number
hasBeginCallback?: (res: DownloadBeginCallbackResult) => void
hasProgressCallback?: (res: DownloadProgressCallbackResult) => void
hasResumableCallback?: () => void // only supported on iOS yet
connectionTimeout?: number // only supported on Android yet
backgroundTimeout?: number // Maximum time (in milliseconds) to download an entire resource (iOS only, useful for timing out background downloads)
}

type DownloadBeginCallbackResult = {
jobId: number // The download job ID, required if one wishes to cancel the download. See `stopDownload`.
statusCode: number // The HTTP status code
contentLength: number // The total size in bytes of the download resource
headers: Headers // The HTTP response headers from the server
}

type DownloadProgressCallbackResult = {
jobId: number // The download job ID, required if one wishes to cancel the download. See `stopDownload`.
contentLength: number // The total size in bytes of the download resource
bytesWritten: number // The number of bytes written to the file so far
}

type DownloadResult = {
jobId: number // The download job ID, required if one wishes to cancel the download. See `stopDownload`.
statusCode: number // The HTTP status code
bytesWritten: number // The number of bytes written to the file
}

type ReadDirItem = {
ctime?: number;
mtime?: number;
name: string;
path: string;
size: number;
type: number;
};

export class FsTurboModule extends TurboModule implements TM.ReactNativeFs.Spec {
private context: Context; // ApplicationContext
private resourceManager: resourceManager.ResourceManager;

constructor(ctx: TurboModuleContext) {
super(ctx)
this.context = this.ctx.uiAbilityContext;
this.resourceManager = this.context.resourceManager;
}

existsAssets(filepath: string): Promise<boolean> {
return new Promise((resolve, reject) => {
try {
this.resourceManager.getRawFileList(filepath, (error: BusinessError, value: Array<string>) => {
if (error != null) {
resolve(false);
} else {
resolve(true);
}
});
} catch (error) {
resolve(false);
}
});
}

readDir(dirpath: string): Promise<Object[]> {
return new Promise((resolve, reject) => {
let listFileOption: ListFileOptions = {
recursion: false,
listNum: 0
};
fs.listFile(dirpath, listFileOption, (err: BusinessError, filenames: Array<string>) => {
if (err) {
reject("list file failed with error message: " + err.message + ", error code: " + err.code);
} else {
let readDirResult: ReadDirItem[] = [];
for (let i = 0; i < filenames.length; i++) {
let filename = filenames[i];
let filePath = dirpath + filename;
let file = fs.statSync(filePath);
readDirResult.push({
ctime: file.ctime,
mtime: file.mtime,
name: filename,
path: filePath,
size: file.size,
type: file.isDirectory() ? 1 : 0,
});
}
resolve(readDirResult);
}
});
});
}

downloadFile(options: Object): Promise<DownloadResult> {
return new Promise((resolve, reject) => {
let downloadFileOptions: DownloadFileOptions = options as DownloadFileOptions;
let downloadConfig: loadRequest.DownloadConfig = {
url: downloadFileOptions.fromUrl,
header: downloadFileOptions.headers,
enableMetered: true,
enableRoaming: true,
description: "",
filePath: downloadFileOptions.toFile,
title: '',
background: true
};

loadRequest.downloadFile((this.context as common.BaseContext), downloadConfig)
.then((downloadTask: loadRequest.DownloadTask) => {
if (downloadTask) {
let loadTask: loadRequest.DownloadTask | null = downloadTask;

if (downloadFileOptions.hasBeginCallback) {
let downloadBeginCallbackResult: DownloadBeginCallbackResult = {
jobId: downloadFileOptions.jobId,
statusCode: 0,
contentLength: 0,
headers: downloadFileOptions.headers
}
this.ctx.rnInstance.emitDeviceEvent('DownloadBegin', downloadBeginCallbackResult)
}
if (downloadFileOptions.hasProgressCallback) {
loadTask.on('progress', (receivedSize, totalSize) => {
if (totalSize > 0) {
let downloadProgressCallbackResult: DownloadProgressCallbackResult = {
jobId: downloadFileOptions.jobId,
contentLength: totalSize,
bytesWritten: receivedSize
}
this.ctx.rnInstance.emitDeviceEvent('DownloadProgress', downloadProgressCallbackResult)
}
});
}

loadTask.on('complete', () => {
let downloadResult: DownloadResult = {
jobId: downloadFileOptions.jobId,
statusCode: 200,
bytesWritten: 0
}
resolve(downloadResult);
})
loadTask.on('pause', () => {
})

loadTask.on('remove', () => {
})
loadTask.on('fail', (err) => {
reject(JSON.stringify(err));
})
} else {
reject("downloadTask dismiss");
}
}).catch((err: BusinessError) => {
reject(JSON.stringify(err));
})
});
}

context = getContext(this) as common.ApplicationContext; // ApplicationContext
// 常量
getConstants(): object {
getConstants(): Object {
return {
// 沙箱路径
FileSandBoxPath: this.context.filesDir,
Expand All @@ -63,14 +233,18 @@ export class FsTurboModule extends TurboModule {
// 读取文件内容
readFile(path: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readText(path, (err: BusinessError, content: string) => {
let file = fs.openSync(path, fs.OpenMode.READ_WRITE);
let arrayBuffer = new ArrayBuffer(4096);
fs.read(file.fd, arrayBuffer, (err: BusinessError, readLen: number) => {
if (err) {
reject('Failed to read the file');
reject("read failed with error message: " + err.message + ", error code: " + err.code);
} else {
let result = buffer.from(content, 'utf8').toString('base64');
let result = buffer.from(arrayBuffer, 0, readLen).toString('base64');
resolve(result);
}
fs.closeSync(file);
});

})
};

Expand Down Expand Up @@ -145,8 +319,7 @@ export class FsTurboModule extends TurboModule {
// 资源文件内容读取
readFileAssets(path: string): Promise<string> {
return new Promise((resolve, reject) => {
this.context.resourceManager.getRawFileContent(
path, (err: BusinessError, value) => {
this.resourceManager.getRawFileContent(path, (err: BusinessError, value: Uint8Array) => {
if (err) {
reject(err.message);
} else {
Expand Down
1 change: 0 additions & 1 deletion harmony/fs/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@
*/

export * from "./src/main/ets/FsPackage"
export * from "./src/main/ets/FsTurboModule"
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-native-oh-tpl/react-native-fs",
"version": "2.20.0-0.1.3",
"version": "2.20.0-0.1.4",
"description": "Native filesystem access for react-native",
"main": "FS.common.js",
"typings": "index.d.ts",
Expand All @@ -9,7 +9,12 @@
"flow": "flow; test $? -eq 0 -o $? -eq 2"
},
"harmony": {
"alias": "react-native-fs"
"alias": "react-native-fs",
"codegenConfig": {
"specPaths": [
"./src"
]
}
},
"repository": {
"type": "git",
Expand Down
25 changes: 15 additions & 10 deletions src/NativeReactNativeFs..js → src/NativeReactNativeFs..ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ type StatResult = {
type: number // Is the file just a file? // Is the file a directory?
};

type DownloadResult = {
jobId: number; // The download job ID, required if one wishes to cancel the download. See `stopDownload`.
statusCode: number; // The HTTP status code
bytesWritten: number; // The number of bytes written to the file
};

export interface Spec extends TurboModule {
getConstants: () => {
// System paths. 沙箱路径
FileSandBoxPath: string;
// 缓存路径
FileCachePath: string;
// 文件
RNFSFileTypeRegular:number;
// 文件夹
RNFSFileTypeDirectory:number;
};
getConstants(): Object;

readFile(path: string): Promise<string>;

exists(path: string): Promise<boolean>;
Expand Down Expand Up @@ -54,6 +52,13 @@ export interface Spec extends TurboModule {
stat(filepath: string): Promise<StatResult>;

touch(filepath: string, mtime?: number, ctime?: number): Promise<boolean>;

downloadFile(bridgeOptions: Object): Promise<DownloadResult>;

readDir(dirpath: string): Promise<Object[]>;

existsAssets(filepath: string):Promise<boolean>;

}

export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativeFs');