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 worker from '@ohos.worker'; 17import fs from '@ohos.file.fs'; 18import { Logger, sleep } from '../common/Common'; 19 20const CONTENT = 'hello world'; 21const TAG: string = '[ConcurrentModule].[MyWorker]'; 22const FILE_NUM: number = 200; // 预制200 1m以下的小文件 23const FILE_NUMBER: number = 9; // 文件1-9命名时加上0 24const LIST_FILE_TWO: number = 2; // 显示拷贝成功后的第二个文件名 25const SLEEP_TIME: number = 100; // 睡眠时间 26 27let workerInstance = null; 28let fileFlag = false; 29 30export default class MyFile { 31 public baseDir: string = ''; 32 public filesCount: number = 0; 33 private flag: boolean = false; 34 public realFileNames: Array<string> = []; 35 36 constructor() { 37 this.baseDir = AppStorage.Get('sanBoxFileDir'); 38 } 39 40 readyFileToFileFs(): void { 41 let fileFsDir = this.baseDir + '/fileFs'; 42 try { 43 if (!fs.accessSync(fileFsDir)) { 44 fs.mkdirSync(fileFsDir); 45 } 46 Logger.info(TAG, 'readyFileToFileFs successful'); 47 } catch (e) { 48 Logger.error(TAG, `readyFileToFileFs has failed for: {message: ${e.message}, code: ${e.code}}`); 49 } 50 } 51 52 // worker file 53 readyFilesToWorker(): void { 54 let content = CONTENT + CONTENT + new Date() + '\n'; 55 let workerDir = this.baseDir + '/workerDir'; 56 57 try { 58 if (!fs.accessSync(workerDir)) { 59 fs.mkdirSync(workerDir); 60 } 61 Logger.info(TAG, 'readyFilesToWorker dpath = ' + workerDir); 62 for (let i = 0; i < FILE_NUM; i++) { 63 let myFile = ''; 64 if (i < FILE_NUMBER) { 65 myFile = workerDir + `/TestFile0${i + 1}.txt`; 66 } else { 67 myFile = workerDir + `/TestFile${i + 1}.txt`; 68 } 69 Logger.info(TAG, 'readyFilesToWorker myFile = ' + myFile); 70 let file = fs.openSync(myFile, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 71 fs.writeSync(file.fd, content); 72 fs.closeSync(file); 73 } 74 Logger.info(TAG, 'readyFilesToWorker successful'); 75 } catch (e) { 76 Logger.error(TAG, `readyFilesToWorker has failed for: {message: ${e.message}, code: ${e.code}}`); 77 } 78 } 79 80 async workToCopyFiles(files: Array<string>, filePath: string): Promise<void> { 81 try { 82 Logger.info(TAG, 'WorkCreator start to create worker'); 83 let destPath = filePath; 84 Logger.info(TAG, 'Workerets destPath ' + destPath); 85 if (!fs.accessSync(destPath)) { 86 fs.mkdirSync(destPath); 87 } 88 if (fs.accessSync(destPath)) { 89 fs.listFile(destPath).then((filenames) => { 90 Logger.info(TAG, 'listFile succeed'); 91 for (let i = 0; i < filenames.length; i++) { 92 Logger.info(TAG, 'Workerets fileName: ' + filenames[i]); 93 } 94 }).catch((err) => { 95 Logger.info(TAG, 'list file failed with error message: ' + err.message + ', error code: ' + err.code); 96 }); 97 } 98 if (files !== null) { 99 this.realFileNames.length = 0; 100 for (let i = 0; i < files.length; i++) { 101 if (files[i] === 'deletedTag') { 102 continue; 103 } 104 this.realFileNames.push(files[i]); 105 } 106 } 107 let count = this.realFileNames.length; 108 for (let j = 0; j < count; j++) { 109 Logger.info(TAG, 'workToCopyFiles this.realFileNames = ' + this.realFileNames[j]); 110 } 111 workerInstance = new worker.ThreadWorker('entry/ets/workers/WorkerCopy.ts'); 112 if (this.realFileNames !== null) { 113 let srcPath = this.baseDir + '/workerDir'; 114 workerInstance.postMessage({ 115 srcDir: srcPath, 116 destDir: destPath, 117 fileNames: this.realFileNames 118 }); 119 } 120 121 workerInstance.onexit = function (code): void { 122 Logger.info(TAG, `workerInstance::onexit has been exit ${code}`); 123 }; 124 workerInstance.onerror = function (err): void { 125 Logger.info(TAG, `workerInstance::onerror has errors: ${JSON.stringify(err)}`); 126 }; 127 workerInstance.onmessageerror = function (event): void { 128 Logger.info(TAG, `workerInstance::onmessageerror has errors: ${JSON.stringify(event)}`); 129 }; 130 workerInstance.onmessage = function (message): void { 131 let data = message.data; 132 Logger.info(TAG, `workerInstance::onmessage receive data: ${JSON.stringify(data)}`); 133 if (data.hasOwnProperty('count')) { 134 Logger.info(TAG, `workerInstance::onmessage receive data length = ${data.count}`); 135 this.filesCount = data.count; 136 fileFlag = data.strFlag; 137 this.flag = true; 138 let fileName1 = null; 139 let fileName2 = null; 140 for (let i = 0; i < data.listFileNames.length; i++) { 141 Logger.info(TAG, `Worker workerInstance::onmessage receive listFileNames: ${data.listFileNames[i]}`); 142 } 143 if (data.listFileNames[0] !== undefined && data.listFileNames[1] !== undefined && data.listFileNames[LIST_FILE_TWO] === undefined) { 144 fileName1 = data.listFileNames[0] + '、'; 145 fileName2 = data.listFileNames[1]; 146 } else if (data.listFileNames[0] !== undefined && data.listFileNames[1] === undefined) { 147 fileName1 = data.listFileNames[0]; 148 fileName2 = ''; 149 } else { 150 fileName1 = data.listFileNames[0] + '、'; 151 let copyFileLog = AppStorage.Get('copyFileLog5'); 152 fileName2 = data.listFileNames[1] + copyFileLog; 153 } 154 AppStorage.SetOrCreate('fileListName1', fileName1); 155 AppStorage.SetOrCreate('fileListName2', fileName2); 156 let copyFileLog3 = AppStorage.Get('copyFileLog3'); 157 let copyFileLog4 = AppStorage.Get('copyFileLog4'); 158 let copyFileLog = '2、' + fileName1 + fileName2 + copyFileLog3 + 'copy' + copyFileLog4; 159 if (fileName1 !== 'undefined、') { 160 AppStorage.SetOrCreate('copyFileShowLog', copyFileLog); 161 } else { 162 AppStorage.SetOrCreate('copyFileShowLog', $r('app.string.workerLogChooseFile')); 163 } 164 Logger.info(TAG, `Worker workerInstance::onmessage receive count: ${JSON.stringify(this.filesCount)}`); 165 } 166 if (this.fileNames !== 0) { 167 AppStorage.SetOrCreate('fileNumber', JSON.stringify(this.filesCount)); 168 } else { 169 AppStorage.SetOrCreate('fileNumber', '0'); 170 AppStorage.SetOrCreate('fileListName1', ''); 171 AppStorage.SetOrCreate('fileListName2', ''); 172 } 173 Logger.info(TAG, 'workerInstance::onmessage Finish to process data from WorkerCopy.ts'); 174 workerInstance.terminate(); 175 }; 176 while (!fileFlag) { 177 await sleep(SLEEP_TIME); 178 } 179 } catch (e) { 180 Logger.error(TAG, `Worker WorkCreator error package: message: ${e.message}, code: ${e.code}`); 181 } 182 } 183 184 deleteCopyFile(filePath: string): void { 185 Logger.info(TAG, 'deleteCopyFile destCopyFilePath = ' + filePath); 186 try { 187 if (fs.accessSync(filePath)) { 188 let isDirectory = fs.statSync(filePath).isDirectory(); 189 if (isDirectory) { 190 fs.rmdirSync(filePath); 191 fs.mkdirSync(filePath); 192 } 193 } 194 } catch (e) { 195 Logger.error(TAG, `delete workerCopyFile error package: message: ${e.message}, code: ${e.code}`); 196 } 197 } 198}