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