feat: add backend code to generate file preview url for next (#6281)

* feat: adjust preview field of file collection

* fix: add banckend to generate preview

* fix: undefined error
This commit is contained in:
Jiannx 2025-02-25 09:33:11 +08:00 committed by GitHub
parent b2aa497da5
commit 669c7f6335
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 0 deletions

View File

@ -41,6 +41,9 @@ attachmentFileTypes.add({
return matchMimetype(file, 'image/*');
},
getThumbnailURL(file) {
if (file.preview) {
return file.preview;
}
if (file.url) {
return `${file.url}${file.thumbnailRule || ''}`;
}

View File

@ -8,6 +8,9 @@
*/
import { Model } from '@nocobase/database';
import { STORAGE_TYPE_ALI_OSS, STORAGE_TYPE_LOCAL, STORAGE_TYPE_S3, STORAGE_TYPE_TX_COS } from '../constants';
const currentStorage = [STORAGE_TYPE_LOCAL, STORAGE_TYPE_ALI_OSS, STORAGE_TYPE_S3, STORAGE_TYPE_TX_COS];
export class FileModel extends Model {
public toJSON() {
@ -15,6 +18,10 @@ export class FileModel extends Model {
const fileStorages = this.constructor['database']?.['_fileStorages'];
if (json.storageId && fileStorages && fileStorages.has(json.storageId)) {
const storage = fileStorages.get(json.storageId);
// 当前文件管理器内的存储类型拼接生成预览链接,其他文件存储自行处理
if (currentStorage.includes(storage?.type) && storage?.options?.thumbnailRule) {
json['preview'] = `${json['url']}${storage?.options?.thumbnailRule || ''}`;
}
if (storage?.options?.thumbnailRule) {
json['thumbnailRule'] = storage?.options?.thumbnailRule;
}