diff --git a/.env.example b/.env.example index 27e6d2153..df428078f 100644 --- a/.env.example +++ b/.env.example @@ -18,5 +18,11 @@ HTTP_PORT=23000 VERDACCIO_PORT=4873 -LOCAL_STORAGE_BASE_URL=http://localhost:23000/uploads USE_STATIC_SERVER=true +LOCAL_STORAGE_BASE_URL=http://localhost:23000/uploads +STORAGE_BASE_URL=http://localhost:23000/uploads + +ALIYUN_OSS_REGION=oss-cn-beijing +ALIYUN_OSS_ACCESS_KEY_ID= +ALIYUN_OSS_ACCESS_KEY_SECRET= +ALIYUN_OSS_BUCKET= diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index bb04bc3f1..521adbc69 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -28,7 +28,7 @@ const api = Api.create({ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', }, - // logging: false, + logging: false, define: {}, sync, }, diff --git a/packages/app/src/api/migrate.ts b/packages/app/src/api/migrate.ts index 0a6be4e3b..2aece5ae7 100644 --- a/packages/app/src/api/migrate.ts +++ b/packages/app/src/api/migrate.ts @@ -188,11 +188,23 @@ api.registerPlugin('plugin-file-manager', [path.resolve(__dirname, '../../../plu }); } const Storage = database.getModel('storages'); + // await Storage.create({ + // title: '本地存储', + // name: `local`, + // type: 'local', + // baseUrl: process.env.LOCAL_STORAGE_BASE_URL, + // default: true + // }); await Storage.create({ - title: '本地存储', - name: `local`, - type: 'local', - baseUrl: process.env.LOCAL_STORAGE_BASE_URL, + name: `ali-oss`, + type: 'ali-oss', + baseUrl: process.env.STORAGE_BASE_URL, + options: { + region: process.env.ALIYUN_OSS_REGION,// 'oss-cn-beijing', + accessKeyId: process.env.ALIYUN_OSS_ACCESS_KEY_ID,// 'LTAI4GEGDJsdGantisvSaz47', + accessKeySecret: process.env.ALIYUN_OSS_ACCESS_KEY_SECRET,//'cDFaOUwigps7ohRmsfBFXGDxNm8uIq', + bucket: process.env.ALIYUN_OSS_BUCKET, //'nocobase' + }, default: true }); await database.getModel('collections').import(require('./collections/example').default); diff --git a/packages/app/src/components/views/Field/index.tsx b/packages/app/src/components/views/Field/index.tsx index e6a7bf5ac..9e7e854e4 100644 --- a/packages/app/src/components/views/Field/index.tsx +++ b/packages/app/src/components/views/Field/index.tsx @@ -258,7 +258,7 @@ export function AttachmentFieldItem(props: any) { e.preventDefault(); } }} className={'attachment-field-item'} target={'_blank'} href={url}> - {title} + {title} { + if (err) return cb(err) + this.client.putStream(filename, file.stream).then( + result => cb(null, { + filename: result.name, + url : result.url + }) + ).catch(cb); + }); + } + + _removeFile(req, file, cb) { + if (!this.client) { + console.error('oss client undefined'); + return cb({message: 'oss client undefined'}); + } + this.client.delete(file.filename).then( + result => cb(null, result) + ).catch(cb); + } +} + +export default (storage) => new AliOssStorage({config: storage.options}); diff --git a/packages/plugin-file-manager/src/storages/index.ts b/packages/plugin-file-manager/src/storages/index.ts index f71923e4b..fb02afe01 100644 --- a/packages/plugin-file-manager/src/storages/index.ts +++ b/packages/plugin-file-manager/src/storages/index.ts @@ -1,9 +1,11 @@ import local from './local'; +import oss from './ali-oss'; import { STORAGE_TYPE_LOCAL, STORAGE_TYPE_ALI_OSS } from '../constants'; const map = new Map(); map.set(STORAGE_TYPE_LOCAL, local); +map.set(STORAGE_TYPE_ALI_OSS, oss); export default map; diff --git a/packages/plugin-file-manager/src/storages/local.ts b/packages/plugin-file-manager/src/storages/local.ts index cb7efa7a3..89c5725dd 100644 --- a/packages/plugin-file-manager/src/storages/local.ts +++ b/packages/plugin-file-manager/src/storages/local.ts @@ -1,4 +1,3 @@ -import crypto from 'crypto'; import path from 'path'; import { URL } from 'url'; import mkdirp from 'mkdirp'; @@ -6,6 +5,7 @@ import multer from 'multer'; import serve from 'koa-static'; import mount from 'koa-mount'; import { STORAGE_TYPE_LOCAL } from '../constants'; +import { getFilename } from '../utils'; export function getDocumentRoot(storage): string { const { documentRoot = 'uploads' } = storage.options || {}; @@ -71,9 +71,5 @@ export default (storage) => multer.diskStorage({ cb(null, destPath); }).catch(cb); }, - filename: function (req, file, cb) { - crypto.randomBytes(16, (err, raw) => { - cb(err, err ? undefined : `${raw.toString('hex')}${path.extname(file.originalname)}`) - }); - } + filename: getFilename }); diff --git a/packages/plugin-file-manager/src/utils.ts b/packages/plugin-file-manager/src/utils.ts new file mode 100644 index 000000000..c5a1677c8 --- /dev/null +++ b/packages/plugin-file-manager/src/utils.ts @@ -0,0 +1,8 @@ +import crypto from 'crypto'; +import path from 'path'; + +export function getFilename (req, file, cb) { + crypto.pseudoRandomBytes(16, function (err, raw) { + cb(err, err ? undefined : `${raw.toString('hex')}${path.extname(file.originalname)}`) + }) +}