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 */ 15import worker from '@ohos.worker'; 16import type { ThreadWorkerGlobalScope } from '@ohos.worker'; 17import Logger from '../common/Logger'; 18import fs from '@ohos.file.fs'; 19 20const workerPort: ThreadWorkerGlobalScope = worker.workerPort; 21const TAG: string = '[ConcurrentModule].[WorkerCopy]'; 22 23workerPort.onmessage = function (message): void { 24 let data = message.data; 25 let srcPath = null; 26 let destPath = null; 27 let fileNames = data.fileNames; 28 for (let i = 0; i < fileNames.length; i++) { 29 srcPath = data.srcDir + '/' + fileNames[i]; 30 Logger.info(TAG, ' srcPath ' + srcPath); 31 destPath = data.destDir + '/' + fileNames[i]; 32 Logger.info(TAG, ' destPath ' + destPath); 33 try { 34 fs.copyFileSync(srcPath, destPath); 35 let countTest = fs.listFileSync(data.destDir).length; 36 Logger.info(TAG, `Worker workerInstance::onmessage receive countTest: ${countTest}`); 37 } catch (e) { 38 Logger.error(TAG, 'WorkerCopy::copyFile has failed for: ' + JSON.stringify(e)); 39 } 40 } 41 let listFileNames = []; 42 listFileNames.length = 0; 43 try { 44 let count = fs.listFileSync(data.destDir).length; 45 let listFiles = fs.listFileSync(data.destDir); 46 Logger.info(TAG, 'listFile succeed'); 47 for (let i = 0; i < listFiles.length; i++) { 48 listFileNames[i] = listFiles[i]; 49 Logger.info(TAG, `Worker workerInstance::onmessage receive listFileNames: ${listFileNames[i]}`); 50 } 51 workerPort.postMessage({ 52 count: count, 53 strFlag: true, 54 listFileNames: listFileNames 55 }); 56 Logger.info(TAG, 'WorkerCopy::onmessage thread post message successfully'); 57 } catch (e) { 58 Logger.error(TAG, 'WorkerCopy::onmessage has failed for: ' + JSON.stringify(e)); 59 } 60}; 61 62workerPort.onmessageerror = async function (message): Promise<void> { 63 Logger.error(TAG, 'WorkerCopy::onmessageerror : ' + JSON.stringify(message)); 64}; 65 66workerPort.onerror = function (e): void { 67 Logger.error(TAG, 'WorkerCopy::onerror : ' + JSON.stringify(e)); 68};