• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2020 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16export interface FileResponse {
17  /**
18   * File URI.
19   * @since 3
20   */
21  uri: string;
22
23  /**
24   * File size, in bytes.
25   * @since 3
26   */
27  length: number;
28
29  /**
30   * Timestamp when the file is stored, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT.
31   * @since 3
32   */
33  lastModifiedTime: number;
34
35  /**
36   * File type. Available values are as follows:
37   * dir: directory
38   * file: file
39   * @since 3
40   */
41  type: "dir" | "file";
42
43  /**
44   * File list.
45   * @since 3
46   */
47  subFiles?: Array<FileResponse>;
48}
49
50/**
51 * @Syscap SysCap.ACE.UIEngine
52 */
53export default class File {
54  /**
55   * Moves a specified file to a given location.
56   * @param options Options.
57   */
58  static move(options: {
59    /**
60     * URI of the file to move.
61     * @since 3
62     */
63    srcUri: string;
64
65    /**
66     * URI of the location to which the file is to move.
67     * @since 3
68     */
69    dstUri: string;
70
71    /**
72     * Called when the source file is moved to the specified location successfully.
73     * This function returns the URI of the file moved to the target location.
74     * @since 3
75     */
76    success?: (uri: string) => void;
77
78    /**
79     * Called when moving fails.
80     * @since 3
81     */
82    fail?: (data: any, code: number) => void;
83
84    /**
85     * Called when the execution is completed.
86     * @since 3
87     */
88    complete?: () => void;
89  }): void;
90
91  /**
92   * Copies a file and save the copy to a specified location.
93   * @param options Options.
94   */
95  static copy(options: {
96    /**
97     * URI of the file to copy.
98     * @since 3
99     */
100    srcUri: string;
101
102    /**
103     * URI of the location to which the copy is to save.
104     * The directory of application resources and URI of the tmp type are not supported.
105     * @since 3
106     */
107    dstUri: string;
108
109    /**
110     * Called when the source file is copied and saved to the specified location successfully.
111     * This function returns the URI of the file moved to the target location.
112     * @since 3
113     */
114    success?: (uri: string) => void;
115
116    /**
117     * Called when the copy or save operation fails.
118     * @since 3
119     */
120    fail?: (data: any, code: number) => void;
121
122    /**
123     * Called when the execution is completed.
124     * @since 3
125     */
126    complete?: () => void;
127  }): void;
128
129  /**
130   * Obtains the list of files in a specified directory.
131   * @param options Options.
132   */
133  static list(options: {
134    /**
135     * URI of the directory.
136     * @since 3
137     */
138    uri: string;
139
140    /**
141     * Called when the list is obtained successfully.
142     * @since 3
143     */
144    success?: (data: { fileList: Array<FileResponse> }) => void;
145
146    /**
147     * Called when the list fails to be obtained.
148     * @since 3
149     */
150    fail?: (data: any, code: number) => void;
151
152    /**
153     * Called when the execution is completed.
154     * @since 3
155     */
156    complete?: () => void;
157  }): void;
158
159  /**
160   * Obtains information about a specified local file.
161   * @param options
162   */
163  static get(options: {
164    /**
165     * File URI.
166     * @since 3
167     */
168    uri: string;
169
170    /**
171     * Whether to recursively obtain the file list under a subdirectory.
172     * The default value is false.
173     * @since 3
174     */
175    recursive?: boolean;
176
177    /**
178     * Called when file information is obtained successfully.
179     * @since 3
180     */
181    success?: (file: FileResponse) => void;
182
183    /**
184     * Called when file information fails to be obtained.
185     * @since 3
186     */
187    fail?: (data: any, code: number) => void;
188
189    /**
190     * Called when the execution is completed.
191     * @since 3
192     */
193    complete?: () => void;
194  }): void;
195
196  /**
197   * Deletes a local file.
198   * @param options
199   */
200  static delete(options: {
201    /**
202     * URI of the file to be deleted, which cannot be an application resource path.
203     * @since 3
204     */
205    uri: string;
206
207    /**
208     * Called when local files are deleted successfully.
209     * @since 3
210     */
211    success?: () => void;
212
213    /**
214     * Called when the deletion fails.
215     * @since 3
216     */
217    fail?: (data: any, code: number) => void;
218
219    /**
220     * Called when the execution is completed.
221     * @since 3
222     */
223    complete?: () => void;
224  }): void;
225
226  /**
227   * Writes texts into a specified file.
228   * @param options
229   */
230  static writeText(options: {
231    /**
232     * URI of a local file. If it does not exist, a file will be created.
233     * @since 3
234     */
235    uri: string;
236
237    /**
238     * Character string to write into the local file.
239     * @since 3
240     */
241    text: string;
242
243    /**
244     * Encoding format. The default format is UTF-8.
245     * @since 3
246     */
247    encoding?: string;
248
249    /**
250     * Whether to enable the append mode. The default value is false.
251     * @since 3
252     */
253    append?: boolean;
254
255    /**
256     * Called when texts are written into a file successfully.
257     * @since 3
258     */
259    success?: () => void;
260
261    /**
262     * Called when texts fail to be written into a file.
263     * @since 3
264     */
265    fail?: (data: any, code: number) => void;
266
267    /**
268     * Called when the execution is completed.
269     * @since 3
270     */
271    complete?: () => void;
272  }): void;
273
274  /**
275   * Reads texts from a specified file.
276   * @param options
277   */
278  static readText(options: {
279    /**
280     * URI of a local file.
281     * @since 3
282     */
283    uri: string;
284
285    /**
286     * Encoding format. The default format is UTF-8.
287     * @since 3
288     */
289    encoding?: string;
290
291    /**
292     * Called when texts are read successfully.
293     * @since 3
294     */
295    success?: (data: { text: string }) => void;
296
297    /**
298     * Called when texts fail to be read.
299     * @since 3
300     */
301    fail?: (data: any, code: number) => void;
302
303    /**
304     * Called when the execution is completed.
305     * @since 3
306     */
307    complete?: () => void;
308  }): void;
309
310  /**
311   * Writes buffer data into a specified file.
312   * @param options Options.
313   */
314  static writeArrayBuffer(options: {
315    /**
316     * URI of a local file. If it does not exist, a file will be created.
317     * @since 3
318     */
319    uri: string;
320
321    /**
322     * Buffer from which the data is derived.
323     * @since 3
324     */
325    buffer: Uint8Array;
326
327    /**
328     * Offset to the position where the writing starts. The default value is 0.
329     * @since 3
330     */
331    position?: number;
332
333    /**
334     * Whether to enable the append mode. The default value is false. If the value is true, the position parameter will become invalid.
335     * @since 3
336     */
337    append?: boolean;
338
339    /**
340     * Called when data from a buffer is written into a file successfully.
341     * @since 3
342     */
343    success?: () => void;
344
345    /**
346     * Called when data from a buffer fails to be written into a file.
347     * @since 3
348     */
349    fail?: (data: any, code: number) => void;
350
351    /**
352     * Called when the execution is completed.
353     * @since 3
354     */
355    complete?: () => void;
356  }): void;
357
358  /**
359   * Reads buffer data from a specified file.
360   * @param options Options.
361   */
362  static readArrayBuffer(options: {
363    /**
364     * URI of a local file.
365     * @since 3
366     */
367    uri: string;
368
369    /**
370     * Position where the reading starts.
371     * The default value is the start position of the file.
372     * @since 3
373     */
374    position?: number;
375
376    /**
377     * Length of the content to read.
378     * If this parameter is not set, the reading proceeds until the end of the file.
379     * @since 3
380     */
381    length?: number;
382
383    /**
384     * Called when the buffer data is read successfully.
385     * @since 3
386     */
387    success?: (data: { buffer: Uint8Array }) => void;
388
389    /**
390     * Called when the buffer data fails to be read.
391     * @since 3
392     */
393    fail?: (data: any, code: number) => void;
394
395    /**
396     * Called when the execution is completed.
397     * @since 3
398     */
399    complete?: () => void;
400  }): void;
401
402  /**
403   * Checks whether a specified file or directory exists.
404   * @param options Options.
405   */
406  static access(options: {
407    /**
408     * URI of the directory or file.
409     * @since 3
410     */
411    uri: string;
412
413    /**
414     * Called when the check result is obtained successfully.
415     * @since 3
416     */
417    success?: () => void;
418
419    /**
420     * Called when the check fails.
421     * @since 3
422     */
423    fail?: (data: any, code: number) => void;
424
425    /**
426     * Called when the execution is completed.
427     * @since 3
428     */
429    complete?: () => void;
430  }): void;
431
432  /**
433   * Creates a specified directory.
434   * @param options Options.
435   */
436  static mkdir(options: {
437    /**
438     * URI of the directory.
439     * @since 3
440     */
441    uri: string;
442
443    /**
444     * Whether to recursively create upper-level directories of the specified directory.
445     * The default value is false.
446     * @since 3
447     */
448    recursive?: boolean;
449
450    /**
451     * Called when the directory is created successfully.
452     * @since 3
453     */
454    success?: () => void;
455
456    /**
457     * Called when the creation fails.
458     * @since 3
459     */
460    fail?: (data: any, code: number) => void;
461
462    /**
463     * Called when the execution is completed.
464     */
465    complete?: () => void;
466  }): void;
467
468  /**
469   * Deletes a specified directory.
470   * @param options
471   */
472  static rmdir(options: {
473    /**
474     * URI of the directory.
475     */
476    uri: string;
477
478    /**
479     * Whether to recursively delete sub files and subdirectories of the specified directory.
480     * The default value is false.
481     */
482    recursive?: boolean;
483
484    /**
485     * Called when the directory is deleted successfully.
486     */
487    success?: () => void;
488
489    /**
490     * Called when the deletion fails.
491     */
492    fail?: (data: any, code: number) => void;
493
494    /**
495     * Called when the execution is completed.
496     */
497    complete?: () => void;
498  }): void;
499}
500