• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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
16import { bufferToString, Logger } from '../common/Common';
17import fs from '@ohos.file.fs';
18import fileUri from '@ohos.file.fileuri';
19import wantConstant from '@ohos.ability.wantConstant';
20import type Want from '@ohos.application.Want';
21
22let fileContent = '';
23const BUFFER_SIZE = 4096; // 读写文件缓冲区大小
24const FILE_NUM = 3; // 沙箱目录预制文件个数
25const DIR_FILE_NUM = 10; // 沙箱目录文件夹中预制文件个数
26const TAG: string = '[File].[SandboxShare]';
27
28export default class FileFs {
29  public fileSize: number = 0;
30  private baseDir: string = '';
31  private dmClass: Object = null;
32  public log: string[] = [];
33  public fileInfo = {
34    path: [],
35    size: 0
36  };
37
38  constructor() {
39    let content1: string = AppStorage.Get('fileContent1');
40    let content2: string = AppStorage.Get('fileContent2');
41    let content3: string = AppStorage.Get('fileContent3');
42    let content4: string = AppStorage.Get('fileContent4');
43    fileContent = content1 + '\r\n\n' + content2 + '\r\n\n' + content3 + '\r\n\n' + content4;
44  }
45
46  readyFilesToTestDir(filesDir: string): void {
47    let content = fileContent;
48    this.baseDir = filesDir + '/TestDir';
49
50    try {
51      let flag = TAG;
52      if (!fs.accessSync(this.baseDir)) {
53        fs.mkdirSync(this.baseDir);
54      }
55      let dpath = this.baseDir;
56
57      Logger.info(TAG, 'readyFileToWatcher dpath = ' + dpath);
58      for (let i = 0; i < DIR_FILE_NUM; i++) {
59        let myFile = dpath + `/testFile_${i}.txt`;
60        Logger.info(TAG, 'readyFileToWatcher myFile = ' + myFile);
61        let file = fs.openSync(myFile, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
62        fs.writeSync(file.fd, content);
63        fs.closeSync(file);
64      }
65      Logger.info(TAG, 'readyFileToWatcher successful');
66    } catch (e) {
67      Logger.error(TAG, `readyFileToWatcher has failed for: {message: ${e.message}, code: ${e.code}}`);
68    }
69  }
70
71  readyFilesToCurDir(filesDir: string): void {
72    let content = fileContent;
73    this.baseDir = filesDir;
74
75    try {
76      let flag = TAG;
77      if (!fs.accessSync(this.baseDir)) {
78        fs.mkdirSync(this.baseDir);
79      }
80      let dpath = this.baseDir;
81
82      Logger.info(TAG, 'readyFileToWatcher dpath = ' + dpath);
83      for (let i = 0; i < FILE_NUM; i++) {
84        let myFile = dpath + `/myFile_${i}.txt`;
85        Logger.info(TAG, 'readyFileToWatcher myFile = ' + myFile);
86        let file = fs.openSync(myFile, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
87        fs.writeSync(file.fd, content);
88        fs.closeSync(file);
89      }
90      Logger.info(TAG, 'readyFileToWatcher successful');
91    } catch (e) {
92      Logger.error(TAG, `readyFileToWatcher has failed for: {message: ${e.message}, code: ${e.code}}`);
93    }
94  }
95
96  getFileContent(path: string): string {
97    let resultPut = '';
98    try {
99      Logger.info(TAG, 'modifyFileToWatcher getFileContent filePath = ' + path);
100      let file = fs.openSync(path, fs.OpenMode.READ_WRITE);
101      let buf = new ArrayBuffer(BUFFER_SIZE);
102      let num = fs.readSync(file.fd, buf);
103      Logger.info(TAG, 'modifyFileToWatcher getFileContent read num = ' + num);
104      resultPut = bufferToString(buf);
105      Logger.info(TAG, 'modifyFileToWatcher getFileContent read resultPut = ' + resultPut);
106      fs.closeSync(file);
107      return resultPut;
108    } catch (e) {
109      Logger.error(TAG, `modifyFileToWatcher getFileContent has failed for: {message: ${e.message}, code: ${e.code}}`);
110      return resultPut;
111    }
112  }
113
114  implicitStartAbility(context, fileName: string, fileSize: number, fileFd: number, filePath: string): void {
115    let buf = new ArrayBuffer(BUFFER_SIZE);
116    let num = fs.readSync(fileFd, buf);
117    Logger.info(TAG, 'modifyFileToWatcher getFileContent read num = ' + num);
118    let content = bufferToString(buf);
119
120    let myUri: string = fileUri.getUriFromPath(filePath);
121    Logger.info(TAG, 'implicitStartAbility myUri is: ' + myUri);
122    let mode = wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION;
123    let uris: string[] = [myUri];
124    let want: Want = {
125      action: 'ohos.want.action.sendData',
126      parameters: {
127        'keyFd': {
128          'type': 'txt',
129          'value': fileFd,
130          'name': fileName,
131          'size': fileSize,
132          'content': content
133        },
134        'stream': uris
135      },
136      flags: mode,
137      type: 'application/txt',
138      uri: myUri,
139    };
140
141    context.startAbility(want).then(() => {
142      Logger.info(TAG, 'startAbility success');
143    }).catch((err) => {
144      Logger.info(TAG, 'startAbility err:' + err);
145    });
146  }
147}
148
149
150