diff --git a/entry/src/main/ets/common/utils/UsbUtils.ets b/entry/src/main/ets/common/utils/UsbUtils.ets new file mode 100644 index 00000000..34971cce --- /dev/null +++ b/entry/src/main/ets/common/utils/UsbUtils.ets @@ -0,0 +1,91 @@ +import common from '@ohos.app.ability.common' +import fs from '@ohos.file.fs'; +import zlib from '@ohos.zlib'; + +async function compressFileToCache(input: string, output: string, context: common.UIAbilityContext): Promise { + if (!fs.accessSync(input)) { + return Promise.reject("no such file or directory") + } + try { + const tempDir = context.cacheDir + '/' + output + if (fs.statSync(input).isDirectory()) { + if (!fs.accessSync(tempDir)) { + fs.mkdirSync(tempDir) + } + fs.listFileSync(input).forEach(name => { + console.log(name) + fs.copyFileSync(input + '/' + name, tempDir + '/' + name) + }) + await zlib.compressFile(tempDir, tempDir + '.zip', { + level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, + memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, + strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY + }) + fs.rmdirSync(context.cacheDir + '/' + output) + return Promise.resolve(tempDir + '.zip') + } else { + const tempDir = context.cacheDir + '/' + output + fs.copyFileSync(input, tempDir) + await zlib.compressFile(tempDir, tempDir + '.zip', { + level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION, + memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT, + strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY + }) + fs.unlinkSync(context.cacheDir + '/' + output) + return Promise.resolve(tempDir + '.zip') + } + } catch (e) { + console.log("UsbUtils", JSON.stringify(e)) + return Promise.reject(e) + } +} + +export class UsbUtils { + private context: common.UIAbilityContext + + constructor(context: common.UIAbilityContext) { + this.context = context + } + + getUsbDiskPath(): Promise { + return new Promise((resolve, reject) => { + let path = "" + try { + path = fs.listFileSync("/mnt/data/external")[0] + } catch (error) { + console.log("err", JSON.stringify(error)) + reject(error) + return + } + resolve('/mnt/data/external' + '/' + path) + }) + } + + async copyTrackToUsb() { + let output = "" + let temp = "" + return this.getUsbDiskPath().then(path => { + output = path + return compressFileToCache('/data/log/duolun/logs', "track", this.context) + }).then((trackPath) => { + temp = trackPath + return fs.copyFile(trackPath, output + '/' + "track.zip") + }).then(() => { + return fs.unlink(temp) + }) + } + + async copyLogToUsb() { + let output = "" + let temp = "" + return this.getUsbDiskPath().then(path => { + output = path + return compressFileToCache('/data/log/hilog', "log", this.context) + }).then((trackPath) => { + temp = trackPath + return fs.copyFile(trackPath, output + '/' + "log.zip") + }).then(() => { + return fs.unlink(temp) + }) + } +} \ No newline at end of file diff --git a/entry/src/main/module.json5 b/entry/src/main/module.json5 index cfed9289..529cf275 100644 --- a/entry/src/main/module.json5 +++ b/entry/src/main/module.json5 @@ -88,44 +88,18 @@ }, { "name": "ohos.permission.WRITE_IMAGEVIDEO", - "usedScene": { - "abilities": [ - "EntryAbility" - ], - "when": "always" - } }, { "name": "ohos.permission.READ_IMAGEVIDEO", - "usedScene": { - "abilities": [ - "EntryAbility" - ], - "when": "always" - } }, { "name": "ohos.permission.FILE_ACCESS_MANAGER", - "reason": "$string:module_desc", - "usedScene": { - "abilities": [ - "MainAbility" - ], - "when": "always" - } }, { "name": "ohos.permission.STORAGE_MANAGER" }, { "name": "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", - "reason": "$string:module_desc", - "usedScene": { - "abilities": [ - "MainAbility" - ], - "when": "always" - } }, ] }