Explorar el Código

feat(FilesService): 添加阿里云 OSS 缩略图生成功能- 在 FilesService 中实现 generateThumbnailUrl 方法,用于生成阿里云 OSS 文件的缩略图 URL
- 在 OcrRecord 模型中添加 thumbnail 字段,用于存储缩略图 URL
- 在 OcrRecordController 中集成缩略图生成逻辑,为每个 OCR 记录生成并存储缩略图
- 引入 @types/ali-oss 依赖,用于类型检查和代码提示

wui hace 9 meses
padre
commit
328a5c65b9

+ 6 - 1
app/Controllers/Http/OcrRecordController.ts

@@ -8,6 +8,7 @@ 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'
 import FilesService from 'App/Services/FilesService'
+import * as console from 'node:console'
 
 
 export default class OcrRecordController {
 export default class OcrRecordController {
     private paginationService = new PaginationService(OcrRecord)
     private paginationService = new PaginationService(OcrRecord)
@@ -27,15 +28,19 @@ export default class OcrRecordController {
                 record.content = ''
                 record.content = ''
                 record.record = ''
                 record.record = ''
                 record.img = ''
                 record.img = ''
+                record.thumbnail = ''
             })
             })
         } else {
         } else {
             await Promise.all(
             await Promise.all(
                 res.map(async (record) => {
                 res.map(async (record) => {
                     if (record.img && record.img !== '-') {
                     if (record.img && record.img !== '-') {
                         const url = new URL(record.img)
                         const url = new URL(record.img)
-                        record.img = await Drive.getSignedUrl(url.pathname.replace(/^\//, ''))
+                        const filePath = url.pathname.replace(/^\//, '')
+                        record.img = await Drive.getSignedUrl(filePath)
+                        record.thumbnail = await FilesService.generateThumbnailUrl(filePath)
                     } else {
                     } else {
                         record.img = ''
                         record.img = ''
+                        record.thumbnail = ''
                     }
                     }
                 })
                 })
             )
             )

+ 6 - 1
app/Models/OcrRecord.ts

@@ -1,6 +1,7 @@
 import AppBaseModel from 'App/Models/AppBaseModel'
 import AppBaseModel from 'App/Models/AppBaseModel'
-import { column } from '@ioc:Adonis/Lucid/Orm'
+import { column, computed } from '@ioc:Adonis/Lucid/Orm'
 import { DateTime } from 'luxon'
 import { DateTime } from 'luxon'
+import Series from 'App/Models/Series'
 
 
 export default class OcrRecord extends AppBaseModel {
 export default class OcrRecord extends AppBaseModel {
     @column({ isPrimary: true })
     @column({ isPrimary: true })
@@ -32,4 +33,8 @@ export default class OcrRecord extends AppBaseModel {
 
 
     @column.dateTime({ autoCreate: true, autoUpdate: true })
     @column.dateTime({ autoCreate: true, autoUpdate: true })
     public updatedAt: DateTime
     public updatedAt: DateTime
+
+    @computed()
+    public thumbnail?: string
+
 }
 }

+ 34 - 0
app/Services/FilesService.ts

@@ -1,7 +1,41 @@
 import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
 import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
 import Drive from '@ioc:Adonis/Core/Drive'
 import Drive from '@ioc:Adonis/Core/Drive'
+import OSS from 'ali-oss'
+import Env from '@ioc:Adonis/Core/Env'
+import * as console from 'node:console'
 
 
 class FilesService {
 class FilesService {
+    private client: OSS
+
+    constructor() {
+        this.client = new OSS({
+            region: Env.get('OSS_REGION'),
+            accessKeyId: Env.get('OSS_KEY'),
+            accessKeySecret: Env.get('OSS_SECRET'),
+            bucket: Env.get('OSS_BUCKET')
+        })
+    }
+
+    public async generateThumbnailUrl(
+        filePath: string,
+        width: number = 300,
+        expires: number = 600
+    ): Promise<string> {
+        try {
+            // const exists = await Drive.exists(filePath)
+            // if (!exists) {
+            //     throw new Error('文件不存在')
+            // }
+
+            return this.client.signatureUrl(filePath, {
+                expires,
+                process: `image/resize,w_${width}`
+            })
+        } catch (error) {
+            throw new Error(`生成缩略图URL失败: ${error.message}`)
+        }
+    }
+
     public async batchDelete(filePaths: string[]) {
     public async batchDelete(filePaths: string[]) {
         if (!Array.isArray(filePaths) || filePaths.length === 0) {
         if (!Array.isArray(filePaths) || filePaths.length === 0) {
             throw new Error('请提供要删除的文件路径数组')
             throw new Error('请提供要删除的文件路径数组')

+ 1 - 0
package.json

@@ -44,6 +44,7 @@
         "@adonisjs/assembler": "^5.9.6",
         "@adonisjs/assembler": "^5.9.6",
         "@japa/preset-adonis": "^1.2.0",
         "@japa/preset-adonis": "^1.2.0",
         "@japa/runner": "2.5.1",
         "@japa/runner": "2.5.1",
+        "@types/ali-oss": "^6.16.11",
         "@types/proxy-addr": "^2.0.3",
         "@types/proxy-addr": "^2.0.3",
         "@types/source-map-support": "^0.5.10",
         "@types/source-map-support": "^0.5.10",
         "adonis-preset-ts": "^2.1.0",
         "adonis-preset-ts": "^2.1.0",

+ 5 - 0
yarn.lock

@@ -2219,6 +2219,11 @@
   resolved "https://registry.yarnpkg.com/@tokenizer/token/-/token-0.3.0.tgz#fe98a93fe789247e998c75e74e9c7c63217aa276"
   resolved "https://registry.yarnpkg.com/@tokenizer/token/-/token-0.3.0.tgz#fe98a93fe789247e998c75e74e9c7c63217aa276"
   integrity sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==
   integrity sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==
 
 
+"@types/ali-oss@^6.16.11":
+  version "6.16.11"
+  resolved "https://registry.yarnpkg.com/@types/ali-oss/-/ali-oss-6.16.11.tgz#a70fc1ef54abb54809405c8b5d77dac06011557c"
+  integrity sha512-/AyemPZy93ZXGzEokMsoPFgjH37snpzH4X/fwans/n63HLaCleriCG3PyrkHCPkgHEc9vj9Uo6paqsBN3vJ3OA==
+
 "@types/bytes@^3.1.1":
 "@types/bytes@^3.1.1":
   version "3.1.4"
   version "3.1.4"
   resolved "https://registry.yarnpkg.com/@types/bytes/-/bytes-3.1.4.tgz#8563f38ea6096df3f409c6500e8ac171790a7c1f"
   resolved "https://registry.yarnpkg.com/@types/bytes/-/bytes-3.1.4.tgz#8563f38ea6096df3f409c6500e8ac171790a7c1f"