1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
public copyRawFileZip(fileName: string, sandBoxPath: string): Promise<boolean> { return new Promise(async (resolve, reject) => { try { this.context.resourceManager.getRawFd(fileName, async (err, data) => { if (err != null) { console.error(`FMResourceManager.ts@copyRawFileZip >> getRawFd error : ${JSON.stringify({ fileName:fileName, sandboxPath:sandBoxPath,err:err })} ` ); resolve(false); return; } console.log(`FMResourceManager.ts@copyRawFileZip >> getRawFd : `, { fileName:fileName, sandboxPath:sandBoxPath });
await checkDir(sandBoxPath); await this.copyFile(fileName,sandBoxPath,data); const isCompled = await unZipFile(sandBoxPath+'/'+fileName, sandBoxPath);
this.context.resourceManager.closeRawFd(fileName, (err, data) => { if (err != null) { console.error(`FMResourceManager.ts@copyRawFileZip >> closeRawFd ->:${JSON.stringify(err)}`); } }); return resolve(isCompled ? true : false); }); } catch (error) { let code = (error as BusinessError).code; let message = (error as BusinessError).message; console.error(`FMResourceManager.ts@copyRawFileZip >> catch ->:${JSON.stringify({code:code,message:message})}`, ); return(false); } }); }
public async copyRawFile(fileName: string,sandboxPath: string) : Promise<string> { console.log(`FMResourceManager.ts@copyRawFile >> 沙箱路径: ${JSON.stringify({fileName,sandboxPath})}`); try { let data = await this.context.resourceManager.getRawFdSync(fileName); let tempPath = sandboxPath+"/temp"; let tempFileName = tempPath +"/" + fileName;
console.log(`FMResourceManager.ts@copyRawFile >> 复制临时文件 ${JSON.stringify({tempPath})}`); await this.copyFile(fileName,tempPath,data); console.log(`FMResourceManager.ts@copyRawFile >> 计算文件 Md5 ${JSON.stringify({tempFileName})}`); let rawFdFileMd5 = await calFileMd5(tempFileName); if(rawFdFileMd5 == null){ return; }
let filenames = await fileIo.listFileSync(sandboxPath);
for (let i = 0; i < filenames.length; i++) { let path = sandboxPath +"/"+filenames[i];
if(filenames[i] === 'temp')continue; let fileMd5 = await calFileMd5(path); if(fileMd5 == null) continue; console.log(`FMResourceManager.ts@copyRawFile >> 计算目标目录下文件md5->${JSON.stringify({path,fileMd5})}`); if(fileMd5 === rawFdFileMd5){ console.log(`FMResourceManager.ts@copyRawFile >> 找到md5相同的文件,返回该文件名->${JSON.stringify({path,fileMd5,rawFdFileMd5})}`); fileIo.unlink(tempFileName); return filenames[i]; } } console.log(`FMResourceManager.ts@copyRawFile >> 没有重复文件,拷贝文件到指定目录 ->${JSON.stringify({tempFileName,dest:sandboxPath+"/"+fileName})}`); await fileIo.copyFile(tempFileName, sandboxPath+"/"+fileName); console.log(`FMResourceManager.ts@copyRawFile >> 删除临时文件->${JSON.stringify({tempFileName})}`); await fileIo.unlink(tempFileName); return fileName; } catch (error) { let code = (error as BusinessError).code; let message = (error as BusinessError).message; console.error(`FMResourceManager.ts@copyRawFile catch error :->${JSON.stringify({code:code,message:message})}`); return; } }
export async function calFileMd5(fileUrl: string): Promise<string | null> { try{ return await hash.hash(fileUrl, "md5"); }catch (e){ FMLog.i("file.ts@calFileMd5 >> calFileMd5 : ",e); } return null;
}
public async copyFile(fileName: string, sandboxPath: string,data:any){ try{ await checkDir(sandboxPath); let destFilePath = sandboxPath+'/'+fileName; console.log(`FMResourceManager.ts@copyFile >> copyFile `,{destFilePath});
let dest = fileIo.openSync(destFilePath,fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
let bufsize = 4096; let buf = new ArrayBuffer(bufsize); let off = 0,len = 0,readedLen = 0;
while (len = fileIo.readSync(data.fd, buf, { offset: data.offset + off, length: bufsize })) { readedLen += len fileIo.writeSync(dest.fd, buf, { offset: off, length: len }) off = off + len if ((data.length - readedLen) < bufsize) { bufsize = data.length - readedLen } } fileIo.close(dest.fd); }catch (e) { console.error(`FMResourceManager.ts@copyFile >> copyFile catch error->: ${JSON.stringify(e)} `); } }
|