104 lines
2.9 KiB
Plaintext
104 lines
2.9 KiB
Plaintext
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<string> {
|
|
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<string> {
|
|
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)
|
|
})
|
|
.finally(() => {
|
|
})
|
|
}
|
|
|
|
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)
|
|
})
|
|
.finally(() => {
|
|
})
|
|
}
|
|
} |