|
@@ -7,6 +7,7 @@ import BlockchainWalletService from 'App/Services/BlockchainWalletService'
|
|
|
import * as bip39 from 'bip39'
|
|
import * as bip39 from 'bip39'
|
|
|
import { HttpStatusCode } from 'axios'
|
|
import { HttpStatusCode } from 'axios'
|
|
|
import { HttpException } from '@adonisjs/http-server/build/src/Exceptions/HttpException'
|
|
import { HttpException } from '@adonisjs/http-server/build/src/Exceptions/HttpException'
|
|
|
|
|
+import FilesService from 'App/Services/FilesService'
|
|
|
|
|
|
|
|
export default class OcrRecordController {
|
|
export default class OcrRecordController {
|
|
|
private paginationService = new PaginationService(OcrRecord)
|
|
private paginationService = new PaginationService(OcrRecord)
|
|
@@ -31,9 +32,8 @@ export default class OcrRecordController {
|
|
|
await Promise.all(
|
|
await Promise.all(
|
|
|
res.map(async (record) => {
|
|
res.map(async (record) => {
|
|
|
if (record.img && record.img !== '-') {
|
|
if (record.img && record.img !== '-') {
|
|
|
- record.img = await Drive.getSignedUrl(
|
|
|
|
|
- new URL(record.img).pathname.replace(/^\//, '')
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ const url = new URL(record.img)
|
|
|
|
|
+ record.img = await Drive.getSignedUrl(url.pathname.replace(/^\//, ''))
|
|
|
} else {
|
|
} else {
|
|
|
record.img = ''
|
|
record.img = ''
|
|
|
}
|
|
}
|
|
@@ -259,4 +259,72 @@ export default class OcrRecordController {
|
|
|
// 返回去重后的助记词列表
|
|
// 返回去重后的助记词列表
|
|
|
return mnemonics
|
|
return mnemonics
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public async imgCleaning({ request, bouncer, response }: HttpContextContract) {
|
|
|
|
|
+ // 授权检查
|
|
|
|
|
+ await bouncer.authorize('admin')
|
|
|
|
|
+
|
|
|
|
|
+ // 验证请求数据
|
|
|
|
|
+ const { startDate, endDate } = await request.validate({
|
|
|
|
|
+ schema: schema.create({
|
|
|
|
|
+ startDate: schema.string(),
|
|
|
|
|
+ endDate: schema.string()
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ // 查询符合条件的记录
|
|
|
|
|
+ const records = await OcrRecord.query()
|
|
|
|
|
+ .whereBetween('createdAt', [startDate, endDate])
|
|
|
|
|
+ .where('favorite', 0)
|
|
|
|
|
+ .whereNot('img', '-')
|
|
|
|
|
+
|
|
|
|
|
+ console.log(`待清理图片数量: ${records.length}`)
|
|
|
|
|
+
|
|
|
|
|
+ if (records.length === 0) {
|
|
|
|
|
+ return response.status(200).ok({
|
|
|
|
|
+ success: true,
|
|
|
|
|
+ message: '没有符合条件的图片需要清理',
|
|
|
|
|
+ count: 0
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const recordImgMap = new Map(records.map((record) => [record.id, record.img]))
|
|
|
|
|
+
|
|
|
|
|
+ const filePaths = records
|
|
|
|
|
+ .map((record) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return {
|
|
|
|
|
+ id: record.id,
|
|
|
|
|
+ originalUrl: record.img,
|
|
|
|
|
+ path: new URL(record.img).pathname.replace(/^\//, '')
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error(`无效的图片 URL: ${record.img}`)
|
|
|
|
|
+ return null
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .filter(Boolean) as { id: number; originalUrl: string; path: string }[]
|
|
|
|
|
+
|
|
|
|
|
+ const pathsToDelete = filePaths.map((item) => item.path)
|
|
|
|
|
+ const imgResult = await FilesService.batchDelete(pathsToDelete)
|
|
|
|
|
+
|
|
|
|
|
+ const successPaths = imgResult.success
|
|
|
|
|
+ console.log(`清理成功路径数量: ${successPaths.length}`)
|
|
|
|
|
+
|
|
|
|
|
+ // 找出成功删除的文件对应的记录ID
|
|
|
|
|
+ const successIds = filePaths
|
|
|
|
|
+ .filter((item) => successPaths.includes(item.path))
|
|
|
|
|
+ .map((item) => item.id)
|
|
|
|
|
+
|
|
|
|
|
+ const deletedCount = await OcrRecord.query().whereIn('id', successIds).delete()
|
|
|
|
|
+ console.log(`删除成功记录数量: ${deletedCount}`)
|
|
|
|
|
+
|
|
|
|
|
+ const successUrls = successIds.map((id) => recordImgMap.get(id)).filter(Boolean)
|
|
|
|
|
+
|
|
|
|
|
+ return response.status(200).ok({
|
|
|
|
|
+ success: true,
|
|
|
|
|
+ message: `清理成功数量: ${successPaths.length}`,
|
|
|
|
|
+ count: successUrls.length
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|