From 379248e5c54c5407254275f5c7738a686678dd5e Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 20 Nov 2023 15:53:09 +0800 Subject: [PATCH] fix: local storage base url (#3063) * fix: local storage base url * fix: test error --- .../src/server/__tests__/action.test.ts | 2 +- .../migrations/20231120142523-fix-storage.ts | 43 +++++++++++++++++++ .../src/server/storages/local.ts | 6 +-- 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 packages/plugins/@nocobase/plugin-file-manager/src/server/migrations/20231120142523-fix-storage.ts diff --git a/packages/plugins/@nocobase/plugin-file-manager/src/server/__tests__/action.test.ts b/packages/plugins/@nocobase/plugin-file-manager/src/server/__tests__/action.test.ts index 9d256774a..c3506d182 100644 --- a/packages/plugins/@nocobase/plugin-file-manager/src/server/__tests__/action.test.ts +++ b/packages/plugins/@nocobase/plugin-file-manager/src/server/__tests__/action.test.ts @@ -5,7 +5,7 @@ import { FILE_FIELD_NAME, STORAGE_TYPE_LOCAL } from '../constants'; const { LOCAL_STORAGE_BASE_URL, LOCAL_STORAGE_DEST = 'storage/uploads', APP_PORT = '13000' } = process.env; -const DEFAULT_LOCAL_BASE_URL = LOCAL_STORAGE_BASE_URL || `http://localhost:${APP_PORT}/storage/uploads`; +const DEFAULT_LOCAL_BASE_URL = LOCAL_STORAGE_BASE_URL || `/storage/uploads`; describe('action', () => { let app; diff --git a/packages/plugins/@nocobase/plugin-file-manager/src/server/migrations/20231120142523-fix-storage.ts b/packages/plugins/@nocobase/plugin-file-manager/src/server/migrations/20231120142523-fix-storage.ts new file mode 100644 index 000000000..0d497da8c --- /dev/null +++ b/packages/plugins/@nocobase/plugin-file-manager/src/server/migrations/20231120142523-fix-storage.ts @@ -0,0 +1,43 @@ +import { Repository } from '@nocobase/database'; +import { Migration } from '@nocobase/server'; + +export default class extends Migration { + async up() { + const result = await this.app.version.satisfies('<0.15.0-alpha.5'); + + if (!result) { + return; + } + + const r = this.db.getRepository('storages'); + const items = await r.find({ + filter: { + type: 'local', + }, + }); + for (const item of items) { + const baseUrl = item.get('baseUrl'); + if (baseUrl === '/storage/uploads') { + continue; + } + if (!baseUrl.includes('/storage/uploads')) { + continue; + } + item.set('baseUrl', '/storage/uploads'); + const options = item.get('options'); + options.documentRoot = 'storage/uploads'; + item.changed('options', true); + item.set('options', options); + const [, pathname] = baseUrl.split('/storage/uploads/'); + if (pathname && item.get('path')) { + item.set('path', pathname.replace(/\/$/, '') + '/' + item.get('path')); + } else if (pathname) { + item.set('path', pathname.replace(/\/$/, '')); + } + await item.save(); + } + const c = this.db.getCollection('attachments'); + const table = c.getTableNameWithSchemaAsString(); + await this.db.sequelize.query(`update ${table} set url = replace(url, 'http://localhost:13000', '')`); + } +} diff --git a/packages/plugins/@nocobase/plugin-file-manager/src/server/storages/local.ts b/packages/plugins/@nocobase/plugin-file-manager/src/server/storages/local.ts index 65c34e259..2a4a0e113 100644 --- a/packages/plugins/@nocobase/plugin-file-manager/src/server/storages/local.ts +++ b/packages/plugins/@nocobase/plugin-file-manager/src/server/storages/local.ts @@ -23,15 +23,13 @@ export default { }); }, defaults() { - const { LOCAL_STORAGE_DEST, LOCAL_STORAGE_BASE_URL, APP_PORT } = process.env; - const documentRoot = LOCAL_STORAGE_DEST || 'storage/uploads'; return { title: 'Local storage', type: STORAGE_TYPE_LOCAL, name: `local`, - baseUrl: LOCAL_STORAGE_BASE_URL || `http://localhost:${APP_PORT || '13000'}/${documentRoot}`, + baseUrl: '/storage/uploads', options: { - documentRoot, + documentRoot: 'storage/uploads', }, }; },