2025-03-28 14:06:56 +08:00
|
|
|
import common from '@ohos.app.ability.common'
|
2025-02-19 10:11:05 +08:00
|
|
|
import fs from '@ohos.file.fs'
|
2025-03-28 14:06:56 +08:00
|
|
|
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl'
|
|
|
|
|
import promptAction from '@ohos.promptAction'
|
|
|
|
|
import { FileTag } from '../config'
|
|
|
|
|
import { BusinessError } from '@ohos.base'
|
2025-02-19 10:11:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
export default class FileUtils {
|
2025-04-09 10:46:50 +08:00
|
|
|
public destFile: string = ''
|
2025-02-19 10:11:05 +08:00
|
|
|
public filePathFdObj: Record<string, fs.File> = {}
|
2025-03-26 13:43:47 +08:00
|
|
|
private context: common.UIAbilityContext
|
|
|
|
|
private absolutePath = '/mnt/hmdfs/100/account/device_view/local/files/duolun'
|
2025-02-19 10:11:05 +08:00
|
|
|
|
|
|
|
|
constructor(context: common.UIAbilityContext) {
|
|
|
|
|
this.context = context
|
2025-03-28 14:06:56 +08:00
|
|
|
this.requestPermission()
|
2025-02-19 10:11:05 +08:00
|
|
|
fs.mkdir(this.absolutePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async initFolder(folderPath: string): Promise<string> {
|
2025-03-28 14:06:56 +08:00
|
|
|
const folderList = folderPath.split('/').filter(folderName => folderName !== '')
|
2025-02-19 10:11:05 +08:00
|
|
|
let path = this.absolutePath
|
2025-03-28 14:06:56 +08:00
|
|
|
for (const folderName of folderList) {
|
|
|
|
|
path += `/${folderName}`
|
2025-02-19 10:11:05 +08:00
|
|
|
try {
|
2025-03-28 14:06:56 +08:00
|
|
|
if (!fs.accessSync(path)) {
|
2025-02-19 10:11:05 +08:00
|
|
|
fs.mkdirSync(path)
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2025-03-28 14:06:56 +08:00
|
|
|
this.handleError('初始化文件夹失败', e, folderPath)
|
2025-02-19 10:11:05 +08:00
|
|
|
}
|
2025-03-28 14:06:56 +08:00
|
|
|
}
|
2025-02-19 10:11:05 +08:00
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 14:06:56 +08:00
|
|
|
async addFile(filePath: string, content: string): Promise<boolean> {
|
2025-02-19 10:11:05 +08:00
|
|
|
try {
|
|
|
|
|
if (fs.accessSync(filePath)) {
|
2025-03-28 14:06:56 +08:00
|
|
|
fs.unlinkSync(filePath)
|
2025-02-19 10:11:05 +08:00
|
|
|
}
|
2025-03-28 14:06:56 +08:00
|
|
|
const file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
|
2025-02-19 10:11:05 +08:00
|
|
|
fs.writeSync(file.fd, content)
|
|
|
|
|
fs.closeSync(file)
|
2025-03-28 14:06:56 +08:00
|
|
|
console.log(FileTag, '写入文件成功')
|
2025-02-19 10:11:05 +08:00
|
|
|
return true
|
|
|
|
|
} catch (e) {
|
2025-03-28 14:06:56 +08:00
|
|
|
this.handleError('写入失败', e, filePath)
|
|
|
|
|
return false
|
2025-02-19 10:11:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 14:06:56 +08:00
|
|
|
async editFile(filePath: string, content: string): Promise<number | undefined> {
|
2025-02-19 10:11:05 +08:00
|
|
|
try {
|
2025-03-28 14:06:56 +08:00
|
|
|
const file = this.filePathFdObj[filePath] ||
|
|
|
|
|
fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.APPEND | fs.OpenMode.CREATE)
|
|
|
|
|
fs.writeSync(file.fd, content + '\n')
|
|
|
|
|
this.filePathFdObj[filePath] = file
|
|
|
|
|
return file.fd
|
2025-02-19 10:11:05 +08:00
|
|
|
} catch (e) {
|
2025-03-28 14:06:56 +08:00
|
|
|
this.handleError('editFile文件失败', e, filePath)
|
2025-04-09 10:46:50 +08:00
|
|
|
return undefined
|
2025-02-19 10:11:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async readFile(filePath: string): Promise<string> {
|
|
|
|
|
try {
|
2025-03-28 14:06:56 +08:00
|
|
|
return await fs.readText(filePath)
|
2025-02-19 10:11:05 +08:00
|
|
|
} catch (e) {
|
2025-03-28 14:06:56 +08:00
|
|
|
this.handleError('readFile文件失败', e, filePath)
|
2025-02-19 10:11:05 +08:00
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-26 13:43:47 +08:00
|
|
|
|
2025-03-28 14:06:56 +08:00
|
|
|
async closeFile(filePath: string): Promise<void> {
|
|
|
|
|
const file = this.filePathFdObj[filePath]
|
|
|
|
|
if (file) {
|
|
|
|
|
fs.closeSync(file)
|
|
|
|
|
console.log(FileTag, `${filePath}文件关闭成功`)
|
2025-03-26 13:43:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 14:06:56 +08:00
|
|
|
async getDeviceList(folderPath?: string): Promise<string[]> {
|
2025-03-26 13:43:47 +08:00
|
|
|
return this.getFilePathList(`${this.absolutePath}/${folderPath}`, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getFilePathList(filePath: string, isSdCard: boolean): Promise<string[]> {
|
|
|
|
|
try {
|
2025-03-28 14:06:56 +08:00
|
|
|
const filenames = await fs.listFile(filePath)
|
|
|
|
|
console.log(FileTag, `目录:${filePath}的子文件:${filenames.join(', ')}`)
|
|
|
|
|
return filenames
|
2025-03-26 13:43:47 +08:00
|
|
|
} catch (e) {
|
2025-03-28 14:06:56 +08:00
|
|
|
this.handleError('获取文件列表失败', e, filePath)
|
|
|
|
|
return []
|
2025-03-26 13:43:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 14:06:56 +08:00
|
|
|
async deleteF(path: string, type: 1 | 2 | 3): Promise<boolean> {
|
|
|
|
|
try {
|
|
|
|
|
if (type === 1) {
|
|
|
|
|
const fileList = await this.getFilePathList(`${this.absolutePath}/${path}`, false)
|
|
|
|
|
fileList.forEach(fileName => fs.unlinkSync(`${this.absolutePath}/${path}/${fileName}`))
|
|
|
|
|
fs.rmdirSync(`${this.absolutePath}/${path}`)
|
|
|
|
|
} else if (type === 2) {
|
|
|
|
|
fs.unlinkSync(`${this.absolutePath}/${path}`)
|
|
|
|
|
} else {
|
|
|
|
|
fs.unlinkSync(path)
|
|
|
|
|
}
|
2025-03-26 13:43:47 +08:00
|
|
|
return true
|
2025-03-28 14:06:56 +08:00
|
|
|
} catch (e) {
|
|
|
|
|
this.handleError('删除文件失败', e, path)
|
|
|
|
|
return false
|
2025-03-26 13:43:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAbsolutePath(): string {
|
|
|
|
|
return this.absolutePath
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 14:06:56 +08:00
|
|
|
getFileContent(filePath: string): string {
|
2025-03-26 13:43:47 +08:00
|
|
|
const path = `${this.absolutePath}/${filePath}`
|
2025-03-28 14:06:56 +08:00
|
|
|
return fs.readTextSync(path)
|
2025-03-26 13:43:47 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-28 14:06:56 +08:00
|
|
|
private async requestPermission(): Promise<void> {
|
|
|
|
|
const permissions: Array<Permissions> = [
|
2025-03-26 13:43:47 +08:00
|
|
|
'ohos.permission.READ_MEDIA',
|
|
|
|
|
'ohos.permission.WRITE_MEDIA'
|
2025-03-28 14:06:56 +08:00
|
|
|
]
|
|
|
|
|
const atManager = abilityAccessCtrl.createAtManager()
|
|
|
|
|
atManager.requestPermissionsFromUser(this.context, permissions, (code, result) => {
|
|
|
|
|
if (result.authResults[0] === -1) {
|
2025-03-26 13:43:47 +08:00
|
|
|
promptAction.showToast({
|
2025-03-28 14:06:56 +08:00
|
|
|
message: '请先授权',
|
2025-03-26 13:43:47 +08:00
|
|
|
duration: 3000,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-03-28 14:06:56 +08:00
|
|
|
|
|
|
|
|
private handleError(message: string, error: BusinessError, filePath: string): void {
|
|
|
|
|
console.error(FileTag, `${message} ${filePath} ${JSON.stringify(error)}`)
|
|
|
|
|
promptAction.showToast({
|
|
|
|
|
message: `${message} ${filePath} ${JSON.stringify(error)}`,
|
|
|
|
|
duration: 4000,
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-02-19 10:11:05 +08:00
|
|
|
}
|