@nocobase/plugin-file-manager 0.5.0-alpha.38 → 0.7.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/esm/actions/upload.d.ts +3 -0
- package/esm/actions/upload.js +143 -0
- package/esm/actions/upload.js.map +1 -0
- package/esm/collections/attachments.d.ts +3 -0
- package/esm/collections/attachments.js +59 -0
- package/esm/collections/attachments.js.map +1 -0
- package/esm/collections/storages.d.ts +3 -0
- package/esm/collections/storages.js +60 -0
- package/esm/collections/storages.js.map +1 -0
- package/esm/constants.d.ts +6 -0
- package/esm/constants.js +7 -0
- package/esm/constants.js.map +1 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +3 -0
- package/esm/index.js.map +1 -0
- package/esm/rules/index.d.ts +1 -0
- package/esm/rules/index.js +3 -0
- package/esm/rules/index.js.map +1 -0
- package/esm/rules/mimetype.d.ts +1 -0
- package/esm/rules/mimetype.js +5 -0
- package/esm/rules/mimetype.js.map +1 -0
- package/esm/server.d.ts +7 -0
- package/esm/server.js +48 -0
- package/esm/server.js.map +1 -0
- package/esm/storages/ali-oss.d.ts +16 -0
- package/esm/storages/ali-oss.js +26 -0
- package/esm/storages/ali-oss.js.map +1 -0
- package/esm/storages/index.d.ts +8 -0
- package/esm/storages/index.js +12 -0
- package/esm/storages/index.js.map +1 -0
- package/esm/storages/local.d.ts +14 -0
- package/esm/storages/local.js +127 -0
- package/esm/storages/local.js.map +1 -0
- package/esm/storages/s3.d.ts +17 -0
- package/esm/storages/s3.js +53 -0
- package/esm/storages/s3.js.map +1 -0
- package/esm/utils.d.ts +2 -0
- package/esm/utils.js +16 -0
- package/esm/utils.js.map +1 -0
- package/lib/actions/upload.js +145 -228
- package/lib/actions/upload.js.map +1 -1
- package/lib/collections/attachments.d.ts +2 -2
- package/lib/collections/attachments.js +58 -56
- package/lib/collections/attachments.js.map +1 -1
- package/lib/collections/storages.d.ts +2 -2
- package/lib/collections/storages.js +59 -62
- package/lib/collections/storages.js.map +1 -1
- package/lib/constants.js +2 -17
- package/lib/constants.js.map +1 -1
- package/lib/index.js +14 -47
- package/lib/index.js.map +1 -1
- package/lib/rules/index.js +6 -30
- package/lib/rules/index.js.map +1 -1
- package/lib/rules/mimetype.js +5 -26
- package/lib/rules/mimetype.js.map +1 -1
- package/lib/server.d.ts +7 -3
- package/lib/server.js +46 -95
- package/lib/server.js.map +1 -1
- package/lib/storages/ali-oss.js +23 -44
- package/lib/storages/ali-oss.js.map +1 -1
- package/lib/storages/index.js +5 -31
- package/lib/storages/index.js.map +1 -1
- package/lib/storages/local.d.ts +2 -1
- package/lib/storages/local.js +110 -198
- package/lib/storages/local.js.map +1 -1
- package/lib/storages/s3.js +49 -84
- package/lib/storages/s3.js.map +1 -1
- package/lib/utils.js +14 -39
- package/lib/utils.js.map +1 -1
- package/package.json +17 -3
- package/tsconfig.build.json +9 -0
- package/lib/actions/upload.d.ts.map +0 -1
- package/lib/collections/attachments.d.ts.map +0 -1
- package/lib/collections/storages.d.ts.map +0 -1
- package/lib/constants.d.ts.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/rules/index.d.ts.map +0 -1
- package/lib/rules/mimetype.d.ts.map +0 -1
- package/lib/server.d.ts.map +0 -1
- package/lib/storages/ali-oss.d.ts.map +0 -1
- package/lib/storages/index.d.ts.map +0 -1
- package/lib/storages/local.d.ts.map +0 -1
- package/lib/storages/s3.d.ts.map +0 -1
- package/lib/utils.d.ts.map +0 -1
package/esm/server.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Plugin } from '@nocobase/server';
|
|
11
|
+
import { resolve } from 'path';
|
|
12
|
+
import { action as uploadAction, middleware as uploadMiddleware } from './actions/upload';
|
|
13
|
+
import { STORAGE_TYPE_LOCAL } from './constants';
|
|
14
|
+
import { getStorageConfig } from './storages';
|
|
15
|
+
export default class PluginFileManager extends Plugin {
|
|
16
|
+
storageType() {
|
|
17
|
+
return process.env.DEFAULT_STORAGE_TYPE;
|
|
18
|
+
}
|
|
19
|
+
install() {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
const defaultStorageConfig = getStorageConfig(this.storageType());
|
|
22
|
+
if (defaultStorageConfig) {
|
|
23
|
+
const Storage = this.db.getCollection('storages');
|
|
24
|
+
yield Storage.repository.create({
|
|
25
|
+
values: Object.assign(Object.assign({}, defaultStorageConfig.defaults()), { type: this.storageType(), default: true }),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
load() {
|
|
31
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
yield this.db.import({
|
|
33
|
+
directory: resolve(__dirname, 'collections'),
|
|
34
|
+
});
|
|
35
|
+
// 暂时中间件只能通过 use 加进来
|
|
36
|
+
this.app.resourcer.use(uploadMiddleware);
|
|
37
|
+
this.app.resourcer.registerActionHandler('upload', uploadAction);
|
|
38
|
+
if (process.env.NOCOBASE_ENV !== 'production') {
|
|
39
|
+
yield getStorageConfig(STORAGE_TYPE_LOCAL).middleware(this.app);
|
|
40
|
+
}
|
|
41
|
+
this.app.acl.allow('attachments', 'upload', 'loggedIn');
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
getName() {
|
|
45
|
+
return this.getPackageName(__dirname);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,UAAU,IAAI,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,CAAC,OAAO,OAAO,iBAAkB,SAAQ,MAAM;IACnD,WAAW;QACT,OAAO,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC;IAC1C,CAAC;IAEK,OAAO;;YACX,MAAM,oBAAoB,GAAG,gBAAgB,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;YAClE,IAAI,oBAAoB,EAAE;gBACxB,MAAM,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;gBAClD,MAAM,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC;oBAC9B,MAAM,kCACD,oBAAoB,CAAC,QAAQ,EAAE,KAClC,IAAI,EAAE,IAAI,CAAC,WAAW,EAAE,EACxB,OAAO,EAAE,IAAI,GACd;iBACF,CAAC,CAAC;aACJ;QACH,CAAC;KAAA;IAEK,IAAI;;YACR,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC;gBACnB,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,aAAa,CAAC;aAC7C,CAAC,CAAC;YAEH,oBAAoB;YACpB,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;YACzC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,qBAAqB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;YAEjE,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY,KAAK,YAAY,EAAE;gBAC7C,MAAM,gBAAgB,CAAC,kBAAkB,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;aACjE;YAED,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC;QAC1D,CAAC;KAAA;IAED,OAAO;QACL,OAAO,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;IACxC,CAAC;CACF","sourcesContent":["import { Plugin } from '@nocobase/server';\nimport { resolve } from 'path';\nimport { action as uploadAction, middleware as uploadMiddleware } from './actions/upload';\nimport { STORAGE_TYPE_LOCAL } from './constants';\nimport { getStorageConfig } from './storages';\n\nexport default class PluginFileManager extends Plugin {\n storageType() {\n return process.env.DEFAULT_STORAGE_TYPE;\n }\n\n async install() {\n const defaultStorageConfig = getStorageConfig(this.storageType());\n if (defaultStorageConfig) {\n const Storage = this.db.getCollection('storages');\n await Storage.repository.create({\n values: {\n ...defaultStorageConfig.defaults(),\n type: this.storageType(),\n default: true,\n },\n });\n }\n }\n\n async load() {\n await this.db.import({\n directory: resolve(__dirname, 'collections'),\n });\n\n // 暂时中间件只能通过 use 加进来\n this.app.resourcer.use(uploadMiddleware);\n this.app.resourcer.registerActionHandler('upload', uploadAction);\n\n if (process.env.NOCOBASE_ENV !== 'production') {\n await getStorageConfig(STORAGE_TYPE_LOCAL).middleware(this.app);\n }\n\n this.app.acl.allow('attachments', 'upload', 'loggedIn');\n }\n\n getName(): string {\n return this.getPackageName(__dirname);\n }\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
make(storage: any): any;
|
|
3
|
+
defaults(): {
|
|
4
|
+
title: string;
|
|
5
|
+
type: string;
|
|
6
|
+
name: string;
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
options: {
|
|
9
|
+
region: string;
|
|
10
|
+
accessKeyId: string;
|
|
11
|
+
accessKeySecret: string;
|
|
12
|
+
bucket: string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export default _default;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { STORAGE_TYPE_ALI_OSS } from '../constants';
|
|
2
|
+
import { cloudFilenameGetter } from '../utils';
|
|
3
|
+
export default {
|
|
4
|
+
make(storage) {
|
|
5
|
+
const createAliOssStorage = require('multer-aliyun-oss');
|
|
6
|
+
return new createAliOssStorage({
|
|
7
|
+
config: storage.options,
|
|
8
|
+
filename: cloudFilenameGetter(storage),
|
|
9
|
+
});
|
|
10
|
+
},
|
|
11
|
+
defaults() {
|
|
12
|
+
return {
|
|
13
|
+
title: '阿里云对象存储',
|
|
14
|
+
type: STORAGE_TYPE_ALI_OSS,
|
|
15
|
+
name: 'ali-oss-1',
|
|
16
|
+
baseUrl: process.env.ALI_OSS_STORAGE_BASE_URL,
|
|
17
|
+
options: {
|
|
18
|
+
region: process.env.ALI_OSS_REGION,
|
|
19
|
+
accessKeyId: process.env.ALI_OSS_ACCESS_KEY_ID,
|
|
20
|
+
accessKeySecret: process.env.ALI_OSS_ACCESS_KEY_SECRET,
|
|
21
|
+
bucket: process.env.ALI_OSS_BUCKET,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=ali-oss.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ali-oss.js","sourceRoot":"","sources":["../../src/storages/ali-oss.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAE/C,eAAe;IACb,IAAI,CAAC,OAAO;QACV,MAAM,mBAAmB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;QACzD,OAAO,IAAI,mBAAmB,CAAC;YAC7B,MAAM,EAAE,OAAO,CAAC,OAAO;YACvB,QAAQ,EAAE,mBAAmB,CAAC,OAAO,CAAC;SACvC,CAAC,CAAC;IACL,CAAC;IACD,QAAQ;QACN,OAAO;YACL,KAAK,EAAE,SAAS;YAChB,IAAI,EAAE,oBAAoB;YAC1B,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,wBAAwB;YAC7C,OAAO,EAAE;gBACP,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc;gBAClC,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,qBAAqB;gBAC9C,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,yBAAyB;gBACtD,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc;aACnC;SACF,CAAC;IACJ,CAAC;CACF,CAAC","sourcesContent":["import { STORAGE_TYPE_ALI_OSS } from '../constants';\nimport { cloudFilenameGetter } from '../utils';\n\nexport default {\n make(storage) {\n const createAliOssStorage = require('multer-aliyun-oss');\n return new createAliOssStorage({\n config: storage.options,\n filename: cloudFilenameGetter(storage),\n });\n },\n defaults() {\n return {\n title: '阿里云对象存储',\n type: STORAGE_TYPE_ALI_OSS,\n name: 'ali-oss-1',\n baseUrl: process.env.ALI_OSS_STORAGE_BASE_URL,\n options: {\n region: process.env.ALI_OSS_REGION,\n accessKeyId: process.env.ALI_OSS_ACCESS_KEY_ID,\n accessKeySecret: process.env.ALI_OSS_ACCESS_KEY_SECRET,\n bucket: process.env.ALI_OSS_BUCKET,\n },\n };\n },\n};\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import local from './local';
|
|
2
|
+
import oss from './ali-oss';
|
|
3
|
+
import s3 from './s3';
|
|
4
|
+
import { STORAGE_TYPE_LOCAL, STORAGE_TYPE_ALI_OSS, STORAGE_TYPE_S3 } from '../constants';
|
|
5
|
+
const map = new Map();
|
|
6
|
+
map.set(STORAGE_TYPE_LOCAL, local);
|
|
7
|
+
map.set(STORAGE_TYPE_ALI_OSS, oss);
|
|
8
|
+
map.set(STORAGE_TYPE_S3, s3);
|
|
9
|
+
export function getStorageConfig(key) {
|
|
10
|
+
return map.get(key);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/storages/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,GAAG,MAAM,WAAW,CAAC;AAC5B,OAAO,EAAE,MAAM,MAAM,CAAC;AAEtB,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAUzF,MAAM,GAAG,GAAG,IAAI,GAAG,EAAoB,CAAC;AACxC,GAAG,CAAC,GAAG,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;AACnC,GAAG,CAAC,GAAG,CAAC,oBAAoB,EAAE,GAAG,CAAC,CAAC;AACnC,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC;AAE7B,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC1C,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACtB,CAAC","sourcesContent":["import local from './local';\nimport oss from './ali-oss';\nimport s3 from './s3';\n\nimport { STORAGE_TYPE_LOCAL, STORAGE_TYPE_ALI_OSS, STORAGE_TYPE_S3 } from '../constants';\n\nexport interface IStorage {\n filenameKey?: string;\n middleware?: Function;\n getFileData?: Function;\n make: Function;\n defaults: Function;\n}\n\nconst map = new Map<string, IStorage>();\nmap.set(STORAGE_TYPE_LOCAL, local);\nmap.set(STORAGE_TYPE_ALI_OSS, oss);\nmap.set(STORAGE_TYPE_S3, s3);\n\nexport function getStorageConfig(key: string): IStorage {\n return map.get(key);\n}\n"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Application from '@nocobase/server';
|
|
2
|
+
import multer from 'multer';
|
|
3
|
+
declare function middleware(app: Application, options?: any): Promise<void>;
|
|
4
|
+
declare const _default: {
|
|
5
|
+
middleware: typeof middleware;
|
|
6
|
+
make(storage: any): multer.StorageEngine;
|
|
7
|
+
defaults(): {
|
|
8
|
+
title: string;
|
|
9
|
+
type: string;
|
|
10
|
+
name: string;
|
|
11
|
+
baseUrl: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export default _default;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import serve from 'koa-static';
|
|
11
|
+
import mkdirp from 'mkdirp';
|
|
12
|
+
import multer from 'multer';
|
|
13
|
+
import path from 'path';
|
|
14
|
+
import { URL } from 'url';
|
|
15
|
+
import { STORAGE_TYPE_LOCAL } from '../constants';
|
|
16
|
+
import { getFilename } from '../utils';
|
|
17
|
+
// use koa-mount match logic
|
|
18
|
+
function match(basePath, pathname) {
|
|
19
|
+
if (!pathname.startsWith(basePath)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const newPath = pathname.replace(basePath, '') || '/';
|
|
23
|
+
if (basePath.slice(-1) === '/') {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return newPath[0] === '/';
|
|
27
|
+
}
|
|
28
|
+
function update(app, storages) {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
const Storage = app.db.getCollection('storages');
|
|
31
|
+
const items = yield Storage.repository.find({
|
|
32
|
+
filter: {
|
|
33
|
+
type: STORAGE_TYPE_LOCAL,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
const primaryKey = Storage.model.primaryKeyAttribute;
|
|
37
|
+
storages.clear();
|
|
38
|
+
for (const storage of items) {
|
|
39
|
+
storages.set(storage[primaryKey], storage);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function createLocalServerUpdateHook(app, storages) {
|
|
44
|
+
return function (row) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
if (row.get('type') === STORAGE_TYPE_LOCAL) {
|
|
47
|
+
yield update(app, storages);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function getDocumentRoot(storage) {
|
|
53
|
+
const { documentRoot = 'uploads' } = storage.options || {};
|
|
54
|
+
// TODO(feature): 后面考虑以字符串模板的方式使用,可注入 req/action 相关变量,以便于区分文件夹
|
|
55
|
+
return path.resolve(path.isAbsolute(documentRoot) ? documentRoot : path.join(process.cwd(), documentRoot));
|
|
56
|
+
}
|
|
57
|
+
function middleware(app, options) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
const LOCALHOST = `http://localhost:${process.env.SERVER_PORT || '13002'}`;
|
|
60
|
+
const Storage = app.db.getCollection('storages');
|
|
61
|
+
const storages = new Map();
|
|
62
|
+
const localServerUpdateHook = createLocalServerUpdateHook(app, storages);
|
|
63
|
+
Storage.model.addHook('afterCreate', localServerUpdateHook);
|
|
64
|
+
Storage.model.addHook('afterUpdate', localServerUpdateHook);
|
|
65
|
+
Storage.model.addHook('afterDestroy', localServerUpdateHook);
|
|
66
|
+
app.on('beforeStart', () => __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
yield update(app, storages);
|
|
68
|
+
}));
|
|
69
|
+
app.use(function (ctx, next) {
|
|
70
|
+
var _a;
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
for (const storage of storages.values()) {
|
|
73
|
+
const baseUrl = storage.get('baseUrl');
|
|
74
|
+
let url;
|
|
75
|
+
try {
|
|
76
|
+
url = new URL(baseUrl);
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
url = {
|
|
80
|
+
pathname: baseUrl,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// 以下情况才认为当前进程所应该提供静态服务
|
|
84
|
+
// 否则都忽略,交给其他 server 来提供(如 nginx/cdn 等)
|
|
85
|
+
if (url.origin && ((_a = storage === null || storage === void 0 ? void 0 : storage.options) === null || _a === void 0 ? void 0 : _a.serve) === false) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const basePath = url.pathname.startsWith('/') ? url.pathname : `/${url.pathname}`;
|
|
89
|
+
if (!match(basePath, ctx.path)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
return serve(getDocumentRoot(storage), {
|
|
93
|
+
// for handle files after any api handlers
|
|
94
|
+
defer: true,
|
|
95
|
+
})(ctx, () => __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
if (ctx.path.startsWith(basePath)) {
|
|
97
|
+
ctx.path = ctx.path.replace(basePath, '');
|
|
98
|
+
}
|
|
99
|
+
yield next();
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
yield next();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
export default {
|
|
108
|
+
middleware,
|
|
109
|
+
make(storage) {
|
|
110
|
+
return multer.diskStorage({
|
|
111
|
+
destination: function (req, file, cb) {
|
|
112
|
+
const destPath = path.join(getDocumentRoot(storage), storage.path);
|
|
113
|
+
mkdirp(destPath, (err) => cb(err, destPath));
|
|
114
|
+
},
|
|
115
|
+
filename: getFilename,
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
defaults() {
|
|
119
|
+
return {
|
|
120
|
+
title: '本地存储',
|
|
121
|
+
type: STORAGE_TYPE_LOCAL,
|
|
122
|
+
name: `local`,
|
|
123
|
+
baseUrl: process.env.LOCAL_STORAGE_BASE_URL || `http://localhost:${process.env.SERVER_PORT || '13002'}/uploads`,
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=local.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local.js","sourceRoot":"","sources":["../../src/storages/local.ts"],"names":[],"mappings":";;;;;;;;;AACA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAC1B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAEvC,4BAA4B;AAC5B,SAAS,KAAK,CAAC,QAAgB,EAAE,QAAgB;IAC/C,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;QAClC,OAAO,KAAK,CAAC;KACd;IAED,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC;IACtD,IAAI,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;QAC9B,OAAO,IAAI,CAAC;KACb;IAED,OAAO,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC;AAC5B,CAAC;AAED,SAAe,MAAM,CAAC,GAAgB,EA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ocobase/server';\nimport serve from 'koa-static';\nimport mkdirp from 'mkdirp';\nimport multer from 'multer';\nimport path from 'path';\nimport { URL } from 'url';\nimport { STORAGE_TYPE_LOCAL } from '../constants';\nimport { getFilename } from '../utils';\n\n// use koa-mount match logic\nfunction match(basePath: string, pathname: string): boolean {\n if (!pathname.startsWith(basePath)) {\n return false;\n }\n\n const newPath = pathname.replace(basePath, '') || '/';\n if (basePath.slice(-1) === '/') {\n return true;\n }\n\n return newPath[0] === '/';\n}\n\nasync function update(app: Application, storages) {\n const Storage = app.db.getCollection('storages');\n\n const items = await Storage.repository.find({\n filter: {\n type: STORAGE_TYPE_LOCAL,\n },\n });\n\n const primaryKey = Storage.model.primaryKeyAttribute;\n\n storages.clear();\n for (const storage of items) {\n storages.set(storage[primaryKey], storage);\n }\n}\n\nfunction createLocalServerUpdateHook(app, storages) {\n return async function (row) {\n if (row.get('type') === STORAGE_TYPE_LOCAL) {\n await update(app, storages);\n }\n };\n}\n\nfunction getDocumentRoot(storage): string {\n const { documentRoot = 'uploads' } = storage.options || {};\n // TODO(feature): 后面考虑以字符串模板的方式使用,可注入 req/action 相关变量,以便于区分文件夹\n return path.resolve(path.isAbsolute(documentRoot) ? documentRoot : path.join(process.cwd(), documentRoot));\n}\n\nasync function middleware(app: Application, options?) {\n const LOCALHOST = `http://localhost:${process.env.SERVER_PORT || '13002'}`;\n\n const Storage = app.db.getCollection('storages');\n const storages = new Map<string, any>();\n\n const localServerUpdateHook = createLocalServerUpdateHook(app, storages);\n Storage.model.addHook('afterCreate', localServerUpdateHook);\n Storage.model.addHook('afterUpdate', localServerUpdateHook);\n Storage.model.addHook('afterDestroy', localServerUpdateHook);\n\n app.on('beforeStart', async () => {\n await update(app, storages);\n });\n\n app.use(async function (ctx, next) {\n for (const storage of storages.values()) {\n const baseUrl = storage.get('baseUrl');\n\n let url;\n try {\n url = new URL(baseUrl);\n } catch (e) {\n url = {\n pathname: baseUrl,\n };\n }\n\n // 以下情况才认为当前进程所应该提供静态服务\n // 否则都忽略,交给其他 server 来提供(如 nginx/cdn 等)\n if (url.origin && storage?.options?.serve === false) {\n continue;\n }\n\n const basePath = url.pathname.startsWith('/') ? url.pathname : `/${url.pathname}`;\n\n if (!match(basePath, ctx.path)) {\n continue;\n }\n\n return serve(getDocumentRoot(storage), {\n // for handle files after any api handlers\n defer: true,\n })(ctx, async () => {\n if (ctx.path.startsWith(basePath)) {\n ctx.path = ctx.path.replace(basePath, '');\n }\n await next();\n });\n }\n\n await next();\n });\n}\n\nexport default {\n middleware,\n make(storage) {\n return multer.diskStorage({\n destination: function (req, file, cb) {\n const destPath = path.join(getDocumentRoot(storage), storage.path);\n mkdirp(destPath, (err: Error | null) => cb(err, destPath));\n },\n filename: getFilename,\n });\n },\n defaults() {\n return {\n title: '本地存储',\n type: STORAGE_TYPE_LOCAL,\n name: `local`,\n baseUrl: process.env.LOCAL_STORAGE_BASE_URL || `http://localhost:${process.env.SERVER_PORT || '13002'}/uploads`,\n };\n },\n};\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
filenameKey: string;
|
|
3
|
+
make(storage: any): any;
|
|
4
|
+
defaults(): {
|
|
5
|
+
title: string;
|
|
6
|
+
name: string;
|
|
7
|
+
type: string;
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
options: {
|
|
10
|
+
region: string;
|
|
11
|
+
accessKeyId: string;
|
|
12
|
+
secretAccessKey: string;
|
|
13
|
+
bucket: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export default _default;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { STORAGE_TYPE_S3 } from '../constants';
|
|
13
|
+
import { cloudFilenameGetter } from '../utils';
|
|
14
|
+
export default {
|
|
15
|
+
filenameKey: 'key',
|
|
16
|
+
make(storage) {
|
|
17
|
+
const S3Client = require('aws-sdk/clients/s3');
|
|
18
|
+
const multerS3 = require('multer-s3');
|
|
19
|
+
const _a = storage.options, { accessKeyId, secretAccessKey, bucket, acl = 'public-read' } = _a, options = __rest(_a, ["accessKeyId", "secretAccessKey", "bucket", "acl"]);
|
|
20
|
+
const s3 = new S3Client(Object.assign(Object.assign({}, options), { credentials: {
|
|
21
|
+
accessKeyId,
|
|
22
|
+
secretAccessKey,
|
|
23
|
+
} }));
|
|
24
|
+
return multerS3({
|
|
25
|
+
s3,
|
|
26
|
+
bucket,
|
|
27
|
+
acl,
|
|
28
|
+
contentType(req, file, cb) {
|
|
29
|
+
if (file.mimetype) {
|
|
30
|
+
cb(null, file.mimetype);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
multerS3.AUTO_CONTENT_TYPE(req, file, cb);
|
|
34
|
+
},
|
|
35
|
+
key: cloudFilenameGetter(storage),
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
defaults() {
|
|
39
|
+
return {
|
|
40
|
+
title: 'AWS S3',
|
|
41
|
+
name: 'aws-s3',
|
|
42
|
+
type: STORAGE_TYPE_S3,
|
|
43
|
+
baseUrl: process.env.AWS_S3_STORAGE_BASE_URL,
|
|
44
|
+
options: {
|
|
45
|
+
region: process.env.AWS_S3_REGION,
|
|
46
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
47
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
48
|
+
bucket: process.env.AWS_S3_BUCKET,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=s3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3.js","sourceRoot":"","sources":["../../src/storages/s3.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAE/C,eAAe;IACb,WAAW,EAAE,KAAK;IAClB,IAAI,CAAC,OAAO;QACV,MAAM,QAAQ,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;QAC/C,MAAM,QAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;QACtC,MAAM,KAA4E,OAAO,CAAC,OAAO,EAA3F,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,EAAE,GAAG,GAAG,aAAa,OAAgC,EAA3B,OAAO,cAAvE,mDAAyE,CAAkB,CAAC;QAClG,MAAM,EAAE,GAAG,IAAI,QAAQ,iCAClB,OAAO,KACV,WAAW,EAAE;gBACX,WAAW;gBACX,eAAe;aAChB,IACD,CAAC;QAEH,OAAO,QAAQ,CAAC;YACd,EAAE;YACF,MAAM;YACN,GAAG;YACH,WAAW,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;gBACvB,IAAI,IAAI,CAAC,QAAQ,EAAE;oBACjB,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;oBACxB,OAAO;iBACR;gBAED,QAAQ,CAAC,iBAAiB,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;YAC5C,CAAC;YACD,GAAG,EAAE,mBAAmB,CAAC,OAAO,CAAC;SAClC,CAAC,CAAC;IACL,CAAC;IACD,QAAQ;QACN,OAAO;YACL,KAAK,EAAE,QAAQ;YACf,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,eAAe;YACrB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,uBAAuB;YAC5C,OAAO,EAAE;gBACP,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,aAAa;gBACjC,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB;gBAC1C,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,qBAAqB;gBAClD,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,aAAa;aAClC;SACF,CAAC;IACJ,CAAC;CACF,CAAC","sourcesContent":["import { STORAGE_TYPE_S3 } from '../constants';\nimport { cloudFilenameGetter } from '../utils';\n\nexport default {\n filenameKey: 'key',\n make(storage) {\n const S3Client = require('aws-sdk/clients/s3');\n const multerS3 = require('multer-s3');\n const { accessKeyId, secretAccessKey, bucket, acl = 'public-read', ...options } = storage.options;\n const s3 = new S3Client({\n ...options,\n credentials: {\n accessKeyId,\n secretAccessKey,\n },\n });\n\n return multerS3({\n s3,\n bucket,\n acl,\n contentType(req, file, cb) {\n if (file.mimetype) {\n cb(null, file.mimetype);\n return;\n }\n\n multerS3.AUTO_CONTENT_TYPE(req, file, cb);\n },\n key: cloudFilenameGetter(storage),\n });\n },\n defaults() {\n return {\n title: 'AWS S3',\n name: 'aws-s3',\n type: STORAGE_TYPE_S3,\n baseUrl: process.env.AWS_S3_STORAGE_BASE_URL,\n options: {\n region: process.env.AWS_S3_REGION,\n accessKeyId: process.env.AWS_ACCESS_KEY_ID,\n secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,\n bucket: process.env.AWS_S3_BUCKET,\n },\n };\n },\n};\n"]}
|
package/esm/utils.d.ts
ADDED
package/esm/utils.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
export function getFilename(req, file, cb) {
|
|
4
|
+
crypto.pseudoRandomBytes(16, function (err, raw) {
|
|
5
|
+
cb(err, err ? undefined : `${raw.toString('hex')}${path.extname(file.originalname)}`);
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
export const cloudFilenameGetter = (storage) => (req, file, cb) => {
|
|
9
|
+
getFilename(req, file, (err, filename) => {
|
|
10
|
+
if (err) {
|
|
11
|
+
return cb(err);
|
|
12
|
+
}
|
|
13
|
+
cb(null, `${storage.path ? `${storage.path}/` : ''}${filename}`);
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=utils.js.map
|
package/esm/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,UAAU,WAAW,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;IACvC,MAAM,CAAC,iBAAiB,CAAC,EAAE,EAAE,UAAU,GAAG,EAAE,GAAG;QAC7C,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC;IACxF,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE;IAChE,WAAW,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE;QACvC,IAAI,GAAG,EAAE;YACP,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC;SAChB;QACD,EAAE,CAAC,IAAI,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,QAAQ,EAAE,CAAC,CAAC;IACnE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC","sourcesContent":["import crypto from 'crypto';\nimport path from 'path';\n\nexport function getFilename(req, file, cb) {\n crypto.pseudoRandomBytes(16, function (err, raw) {\n cb(err, err ? undefined : `${raw.toString('hex')}${path.extname(file.originalname)}`);\n });\n}\n\nexport const cloudFilenameGetter = (storage) => (req, file, cb) => {\n getFilename(req, file, (err, filename) => {\n if (err) {\n return cb(err);\n }\n cb(null, `${storage.path ? `${storage.path}/` : ''}${filename}`);\n });\n};\n"]}
|