23
23
*/
24
24
25
25
import { TurboModule , RNOHError } from 'rnoh/ts' ;
26
- import fs from '@ohos.file.fs' ;
26
+ import fs , { ReadTextOptions , WriteOptions } from '@ohos.file.fs' ;
27
+ import hash from '@ohos.file.hash' ;
27
28
import { BusinessError } from '@ohos.base' ;
28
29
import common from '@ohos.app.ability.common' ;
29
30
import util from '@ohos.util' ;
30
31
import buffer from '@ohos.buffer' ;
32
+ import HashMap from '@ohos.util.HashMap' ;
31
33
32
34
let context = getContext ( this ) as common . ApplicationContext ; // ApplicationContext
33
35
36
+ interface StatResult {
37
+ ctime : number , // The creation date of the file
38
+ mtime : number , // The last modified date of the file
39
+ size : number , // Size in bytes
40
+ mode : number , // UNIX file mode
41
+ originalFilepath : string , // ANDROID: In case of content uri this is the pointed file path, otherwise is the same as path
42
+ type : number // Is the file just a file? Is the file a directory?
43
+ }
44
+
34
45
export class FsTurboModule extends TurboModule {
35
- // 常量路径
46
+ // 常量
36
47
getConstants ( ) : object {
37
48
return {
38
49
// 沙箱路径
39
50
FileSandBoxPath : context . filesDir ,
40
51
// 缓存路径
41
52
FileCachePath : context . cacheDir ,
53
+ // 文件
54
+ RNFSFileTypeRegular : 0 ,
55
+ // 文件夹
56
+ RNFSFileTypeDirectory : 1 ,
42
57
}
43
58
} ;
44
59
@@ -175,4 +190,150 @@ export class FsTurboModule extends TurboModule {
175
190
} ) ;
176
191
} )
177
192
}
193
+
194
+ // 文件hash
195
+ hash ( path : string , algorithm : string ) : Promise < string > {
196
+ return new Promise ( ( resolve , reject ) => {
197
+ let algorithms : HashMap < string , string > = new HashMap ( ) ;
198
+ algorithms . set ( 'md5' , 'md5' ) ;
199
+ algorithms . set ( 'sha1' , 'sha1' ) ;
200
+ algorithms . set ( 'sha256' , 'sha256' ) ;
201
+ // algorithm不存在
202
+ if ( ! algorithms . hasKey ( algorithm ) ) {
203
+ reject ( 'Invalid hash algorithm' ) ;
204
+ return ;
205
+ }
206
+ // 判断是否是文件夹
207
+ let isDirectory = fs . statSync ( path ) . isDirectory ( ) ;
208
+ if ( isDirectory ) {
209
+ reject ( 'file IsDirectory' ) ;
210
+ return ;
211
+ }
212
+ // 判断文件是否在
213
+ let res = fs . accessSync ( path ) ;
214
+ if ( ! res ) {
215
+ reject ( 'file not exists' ) ;
216
+ return ;
217
+ }
218
+ hash . hash ( path , algorithm , ( err : BusinessError , result : string ) => {
219
+ if ( err ) {
220
+ reject ( "calculate file hash failed with error message: " + err . message + ", error code: " + err . code ) ;
221
+ } else {
222
+ resolve ( result ) ;
223
+ }
224
+ } )
225
+ } )
226
+ }
227
+
228
+ // 移动文件
229
+ moveFile ( filepath : string , destPath : string ) : Promise < void > {
230
+ return new Promise ( ( resolve , reject ) => {
231
+ fs . moveFile ( filepath , destPath , 0 , ( err : BusinessError ) => {
232
+ if ( err ) {
233
+ reject ( 'move file failed with error message: ' + err . message + ', error code: ' + err . code ) ;
234
+ } else {
235
+ resolve ( ) ;
236
+ }
237
+ } )
238
+ } )
239
+ }
240
+
241
+ // 文件内容部分读
242
+ read ( path : string , length : number , position : number ) : Promise < string > {
243
+ return new Promise ( ( resolve , reject ) => {
244
+ let readTextOption : ReadTextOptions = {
245
+ offset : position ,
246
+ length : length ,
247
+ encoding : 'utf-8'
248
+ } ;
249
+ fs . readText ( path , readTextOption , ( err : BusinessError , str : string ) => {
250
+ if ( err ) {
251
+ reject ( 'readText failed with error message: ' + err . message + ', error code: ' + err . code ) ;
252
+ } else {
253
+ let result = buffer . from ( str , 'utf8' ) . toString ( 'base64' ) ;
254
+ resolve ( result ) ;
255
+ }
256
+ } ) ;
257
+ } )
258
+ }
259
+
260
+ // 文件内容从某位置写
261
+ write ( filepath : string , contents : string , position : number ) : Promise < void > {
262
+ return new Promise ( ( resolve , reject ) => {
263
+ let result = buffer . from ( contents , 'base64' ) . toString ( 'utf8' ) ;
264
+ let file = fs . openSync ( filepath , fs . OpenMode . READ_WRITE | fs . OpenMode . CREATE ) ;
265
+ let writeOption : WriteOptions = {
266
+ offset : position
267
+ } ;
268
+ fs . write ( file . fd , result , writeOption , ( err : BusinessError , writeLen : number ) => {
269
+ if ( err ) {
270
+ reject ( 'write data to file failed with error message:' + err . message + ', error code: ' + err . code ) ;
271
+ } else {
272
+ resolve ( ) ;
273
+ }
274
+ fs . closeSync ( file ) ;
275
+ } ) ;
276
+ } )
277
+ }
278
+
279
+ touch ( filePath : string , mtime ?: number , ctime ?: number ) : Promise < boolean > {
280
+ return new Promise ( ( resolve , reject ) => {
281
+ // 判断是否是文件夹
282
+ let isDirectory = fs . statSync ( filePath ) . isDirectory ( ) ;
283
+ if ( isDirectory ) {
284
+ reject ( 'file IsDirectory' ) ;
285
+ return ;
286
+ }
287
+ // 判断文件是否在
288
+ let res = fs . accessSync ( filePath ) ;
289
+ if ( ! res ) {
290
+ reject ( 'No such file or directory' ) ;
291
+ return ;
292
+ }
293
+ if ( mtime ) {
294
+ try {
295
+ fs . utimes ( filePath , mtime ) ;
296
+ resolve ( true )
297
+ } catch ( err ) {
298
+ resolve ( err . message )
299
+ }
300
+ } else {
301
+ resolve ( false )
302
+ }
303
+ } )
304
+ }
305
+
306
+ // 获取文件详细属性信息
307
+ stat ( filepath : string ) : Promise < StatResult > {
308
+ return new Promise ( ( resolve , reject ) => {
309
+ let statResult : StatResult = {
310
+ ctime : - 1 ,
311
+ mtime : - 1 ,
312
+ size : - 1 ,
313
+ mode : - 1 ,
314
+ originalFilepath : '' ,
315
+ type : - 1
316
+ } ;
317
+ // 判断文件是否在
318
+ let res = fs . accessSync ( filepath ) ;
319
+ if ( ! res ) {
320
+ reject ( 'file not exists' ) ;
321
+ return ;
322
+ }
323
+ fs . stat ( filepath , ( err : BusinessError , stat : fs . Stat ) => {
324
+ if ( err ) {
325
+ console . error ( "error message: " + err . message + ", error code: " + err . code ) ;
326
+ } else {
327
+ statResult . ctime = stat . ctime ;
328
+ statResult . mtime = stat . mtime ;
329
+ statResult . size = stat . size ;
330
+ statResult . mode = stat . mode ;
331
+ statResult . originalFilepath = filepath ;
332
+ statResult . type = stat . isDirectory ( ) ? 1 : 0 ;
333
+ console . log ( 'file statResult: ' + JSON . stringify ( statResult ) ) ;
334
+ resolve ( statResult ) ;
335
+ }
336
+ } ) ;
337
+ } )
338
+ }
178
339
}
0 commit comments