Commit ba258b3d by liimin

```

feat(utils): 新增获取文件元数据功能并优化最新文件查找逻辑

新增 `__getFileMetadata` 方法用于获取文件的名称和修改时间,并在
`__getLatestFile` 中使用该方法来比较文件修改时间。同时移除了测试用的
API 劫持代码,并增加了对异步处理异常的捕获,提升代码健壮性。此外,
统一了日志提示信息格式,便于问题定位与调试。
```
1 parent 511cc3da
......@@ -61,26 +61,51 @@ async function __getLatestFilePath(directoryPath) {
return latestFile ? directoryPath + '/' + latestFile.name : ''
}
/**
* 获取文件的元数据
*
* @param {object} fileEntry - 文件条目
* @returns {Promise<object>} - 文件元数据
*/
function __getFileMetadata(fileEntry) {
return new Promise(resolve => {
fileEntry.getMetadata(
metadata => {
resolve({
name: fileEntry.name,
modificationTime: metadata.modificationTime.getTime()
})
},
e => {
uni.$logger.local.warn(`〖接待记录Record〗 == 获取文件元数据失败: ${fileEntry.name} ${e.message}`)
resolve({
name: fileEntry.name,
modificationTime: 0
})
}
)
})
}
/**
* 获取指定目录中修改时间最晚的文件信息
*
* @param {string} directoryPath - 目录路径
* @param {string} prefix - 文件名前缀(可选)
* @returns {Promise<object|null>} - 文件对象 {name, modificationTime} 或 null
*/
async function __getLatestFile(directoryPath, prefix = '') {
return new Promise((resolve, reject) => {
// [测试注入] 模拟系统异常:劫持 API 强制失败
const originalResolve = plus.io.resolveLocalFileSystemURL
plus.io.resolveLocalFileSystemURL = function (path, success, fail) {
uni.$logger.local.info(`〖接待记录Record〗 == [测试模拟] 拦截目录读取: ${path}`)
setTimeout(() => {
fail && fail({ code: 12, message: 'Simulated Path Not Found' })
// 恢复 API,避免影响全局
plus.io.resolveLocalFileSystemURL = originalResolve
}, 0)
}
// const originalResolve = plus.io.resolveLocalFileSystemURL
// plus.io.resolveLocalFileSystemURL = function (path, success, fail) {
// uni.$logger.local.info(`〖接待记录Record〗 == [测试模拟] 拦截目录读取: ${path}`)
// setTimeout(() => {
// fail && fail({ code: 12, message: 'Simulated Path Not Found' })
// // 恢复 API,避免影响全局
// plus.io.resolveLocalFileSystemURL = originalResolve
// }, 0)
// }
plus.io.resolveLocalFileSystemURL(
directoryPath,
......@@ -88,11 +113,16 @@ async function __getLatestFile(directoryPath, prefix = '') {
const dirR = e.createReader()
dirR.readEntries(
async entries => {
const files = entries.filter(item => (prefix ? item.name.startsWith(prefix) : true))
const filePromises = files.map(item => __getFileMetadata(item))
const results = await Promise.all(filePromises)
const latest = results.reduce((acc, current) => (current.modificationTime > acc.modificationTime ? current : acc), { modificationTime: 0 })
resolve(latest.name ? latest : null)
try {
const files = entries.filter(item => (prefix ? item.name.startsWith(prefix) : true))
const filePromises = files.map(item => __getFileMetadata(item))
const results = await Promise.all(filePromises)
const latest = results.reduce((acc, current) => (current.modificationTime > acc.modificationTime ? current : acc), { modificationTime: 0 })
resolve(latest.name ? latest : null)
} catch (error) {
uni.$logger.local.error(`〖接待记录Record〗 == 处理目录项异常: ${error.message}`)
resolve(null)
}
},
err => {
uni.$logger.local.warn(`〖接待记录Record〗 == 读取目录项失败: ${directoryPath}, err: ${JSON.stringify(err)} ${err.message}`)
......@@ -108,11 +138,10 @@ async function __getLatestFile(directoryPath, prefix = '') {
)
})
}
async function sleep(time){
async function sleep(time) {
return new Promise(resolve => setTimeout(resolve, time))
}
export default {
getLatestAudioFilePath,
sleep
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!