@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/LICENSE
CHANGED
|
@@ -1,21 +1,201 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,143 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
+
var t = {};
|
|
12
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
+
t[p] = s[p];
|
|
14
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
+
t[p[i]] = s[p[i]];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
import path from 'path';
|
|
22
|
+
import multer from '@koa/multer';
|
|
23
|
+
import { getStorageConfig } from '../storages';
|
|
24
|
+
import * as Rules from '../rules';
|
|
25
|
+
import { FILE_FIELD_NAME, LIMIT_FILES, LIMIT_MAX_FILE_SIZE } from '../constants';
|
|
26
|
+
import { BelongsToManyRepository, BelongsToRepository } from '@nocobase/database';
|
|
27
|
+
function getRules(ctx) {
|
|
28
|
+
const { resourceField } = ctx;
|
|
29
|
+
if (!resourceField) {
|
|
30
|
+
return ctx.storage.rules;
|
|
31
|
+
}
|
|
32
|
+
const { rules = {} } = resourceField.options.attachment || {};
|
|
33
|
+
return Object.assign({}, ctx.storage.rules, rules);
|
|
34
|
+
}
|
|
35
|
+
// TODO(optimize): 需要优化错误处理,计算失败后需要抛出对应错误,以便程序处理
|
|
36
|
+
function getFileFilter(ctx) {
|
|
37
|
+
return (req, file, cb) => {
|
|
38
|
+
// size 交给 limits 处理
|
|
39
|
+
const _a = getRules(ctx), { size } = _a, rules = __rest(_a, ["size"]);
|
|
40
|
+
const ruleKeys = Object.keys(rules);
|
|
41
|
+
const result = !ruleKeys.length ||
|
|
42
|
+
!ruleKeys.some((key) => typeof Rules[key] !== 'function' || !Rules[key](file, rules[key], ctx));
|
|
43
|
+
cb(null, result);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function middleware(ctx, next) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
const { resourceName, actionName, associatedName } = ctx.action.params;
|
|
49
|
+
if (actionName !== 'upload') {
|
|
50
|
+
return next();
|
|
51
|
+
}
|
|
52
|
+
// NOTE:
|
|
53
|
+
// 1. 存储引擎选择依赖于字段定义
|
|
54
|
+
// 2. 字段定义中需包含引擎的外键值
|
|
55
|
+
// 3. 无字段时按 storages 表的默认项
|
|
56
|
+
// 4. 插件初始化后应提示用户添加至少一个存储引擎并设为默认
|
|
57
|
+
const Storage = ctx.db.getCollection('storages');
|
|
58
|
+
let storage;
|
|
59
|
+
if (resourceName === 'attachments') {
|
|
60
|
+
// 如果没有包含关联,则直接按默认文件上传至默认存储引擎
|
|
61
|
+
storage = yield Storage.repository.findOne({ filter: { default: true } });
|
|
62
|
+
}
|
|
63
|
+
else if (associatedName) {
|
|
64
|
+
const AssociatedCollection = ctx.db.getCollection(associatedName);
|
|
65
|
+
const resourceField = AssociatedCollection.getField(resourceName);
|
|
66
|
+
ctx.resourceField = resourceField;
|
|
67
|
+
const { attachment = {} } = resourceField.options;
|
|
68
|
+
storage = yield Storage.repository.findOne({
|
|
69
|
+
filter: attachment.storage ? { name: attachment.storage } : { default: true },
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (!storage) {
|
|
73
|
+
console.error('[file-manager] no default or linked storage provided');
|
|
74
|
+
return ctx.throw(500);
|
|
75
|
+
}
|
|
76
|
+
// 传递已取得的存储引擎,避免重查
|
|
77
|
+
ctx.storage = storage;
|
|
78
|
+
const storageConfig = getStorageConfig(storage.type);
|
|
79
|
+
if (!storageConfig) {
|
|
80
|
+
console.error(`[file-manager] storage type "${storage.type}" is not defined`);
|
|
81
|
+
return ctx.throw(500);
|
|
82
|
+
}
|
|
83
|
+
const multerOptions = {
|
|
84
|
+
fileFilter: getFileFilter(ctx),
|
|
85
|
+
limits: {
|
|
86
|
+
fileSize: Math.min(getRules(ctx).size || LIMIT_MAX_FILE_SIZE, LIMIT_MAX_FILE_SIZE),
|
|
87
|
+
// 每次只允许提交一个文件
|
|
88
|
+
files: LIMIT_FILES,
|
|
89
|
+
},
|
|
90
|
+
storage: storageConfig.make(storage),
|
|
91
|
+
};
|
|
92
|
+
const upload = multer(multerOptions).single(FILE_FIELD_NAME);
|
|
93
|
+
return upload(ctx, next);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export function action(ctx, next) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
const { [FILE_FIELD_NAME]: file, storage } = ctx;
|
|
99
|
+
if (!file) {
|
|
100
|
+
return ctx.throw(400, 'file validation failed');
|
|
101
|
+
}
|
|
102
|
+
const storageConfig = getStorageConfig(storage.type);
|
|
103
|
+
const { [storageConfig.filenameKey || 'filename']: name } = file;
|
|
104
|
+
// make compatible filename across cloud service (with path)
|
|
105
|
+
const filename = path.basename(name);
|
|
106
|
+
const extname = path.extname(filename);
|
|
107
|
+
const urlPath = storage.path ? storage.path.replace(/^([^\/])/, '/$1') : '';
|
|
108
|
+
const data = Object.assign({ title: file.originalname.replace(extname, ''), filename,
|
|
109
|
+
extname,
|
|
110
|
+
// TODO(feature): 暂时两者相同,后面 storage.path 模版化以后,这里只是 file 实际的 path
|
|
111
|
+
path: storage.path, size: file.size,
|
|
112
|
+
// 直接缓存起来
|
|
113
|
+
url: `${storage.baseUrl}${urlPath}/${filename}`, mimetype: file.mimetype,
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
meta: ctx.request.body }, (storageConfig.getFileData ? storageConfig.getFileData(file) : {}));
|
|
116
|
+
const attachment = yield ctx.db.sequelize.transaction((transaction) => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
// TODO(optimize): 应使用关联 accessors 获取
|
|
118
|
+
const result = yield storage.createAttachment(data, { transaction });
|
|
119
|
+
const { associatedName, associatedIndex, resourceName } = ctx.action.params;
|
|
120
|
+
const AssociatedCollection = ctx.db.getCollection(associatedName);
|
|
121
|
+
if (AssociatedCollection && associatedIndex && resourceName) {
|
|
122
|
+
const Repo = AssociatedCollection.repository.relation(resourceName).of(associatedIndex);
|
|
123
|
+
const Attachment = ctx.db.getCollection('attachments').model;
|
|
124
|
+
const opts = {
|
|
125
|
+
tk: result[Attachment.primaryKeyAttribute],
|
|
126
|
+
transaction,
|
|
127
|
+
};
|
|
128
|
+
if (Repo instanceof BelongsToManyRepository) {
|
|
129
|
+
yield Repo.add(opts);
|
|
130
|
+
}
|
|
131
|
+
else if (Repo instanceof BelongsToRepository) {
|
|
132
|
+
yield Repo.set(opts);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}));
|
|
137
|
+
// 将存储引擎的信息附在已创建的记录里,节省一次查询
|
|
138
|
+
// attachment.setDataValue('storage', storage);
|
|
139
|
+
ctx.body = attachment;
|
|
140
|
+
yield next();
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=upload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload.js","sourceRoot":"","sources":["../../src/actions/upload.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,KAAK,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACjF,OAAO,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAElF,SAAS,QAAQ,CAAC,GAAY;IAC5B,MAAM,EAAE,aAAa,EAAE,GAAG,GAAG,CAAC;IAC9B,IAAI,CAAC,aAAa,EAAE;QAClB,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC;KAC1B;IACD,MAAM,EAAE,KAAK,GAAG,EAAE,EAAE,GAAG,aAAa,CAAC,OAAO,CAAC,UAAU,IAAI,EAAE,CAAC;IAC9D,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AACrD,CAAC;AAED,gDAAgD;AAChD,SAAS,aAAa,CAAC,GAAY;IACjC,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE;QACvB,oBAAoB;QACpB,MAAM,KAAqB,QAAQ,CAAC,GAAG,CAAC,EAAlC,EAAE,IAAI,OAA4B,EAAvB,KAAK,cAAhB,QAAkB,CAAgB,CAAC;QACzC,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACpC,MAAM,MA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AO,CAAC,gBAAgB,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;YAErE,MAAM,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;YAC5E,MAAM,oBAAoB,GAAG,GAAG,CAAC,EAAE,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;YAElE,IAAI,oBAAoB,IAAI,eAAe,IAAI,YAAY,EAAE;gBAC3D,MAAM,IAAI,GAAG,oBAAoB,CAAC,UAAU,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC;gBACxF,MAAM,UAAU,GAAG,GAAG,CAAC,EAAE,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC,KAAK,CAAC;gBAC7D,MAAM,IAAI,GAAG;oBACX,EAAE,EAAE,MAAM,CAAC,UAAU,CAAC,mBAAmB,CAAC;oBAC1C,WAAW;iBACZ,CAAC;gBAEF,IAAI,IAAI,YAAY,uBAAuB,EAAE;oBAC3C,MAAM,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;iBACtB;qBAAM,IAAI,IAAI,YAAY,mBAAmB,EAAE;oBAC9C,MAAM,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;iBACtB;aACF;YAED,OAAO,MAAM,CAAC;QAChB,CAAC,CAAA,CAAC,CAAC;QAEH,2BAA2B;QAC3B,+CAA+C;QAC/C,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC;QAEtB,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA","sourcesContent":["import path from 'path';\nimport multer from '@koa/multer';\nimport { Context, Next } from '@nocobase/actions';\nimport { getStorageConfig } from '../storages';\nimport * as Rules from '../rules';\nimport { FILE_FIELD_NAME, LIMIT_FILES, LIMIT_MAX_FILE_SIZE } from '../constants';\nimport { BelongsToManyRepository, BelongsToRepository } from '@nocobase/database';\n\nfunction getRules(ctx: Context) {\n const { resourceField } = ctx;\n if (!resourceField) {\n return ctx.storage.rules;\n }\n const { rules = {} } = resourceField.options.attachment || {};\n return Object.assign({}, ctx.storage.rules, rules);\n}\n\n// TODO(optimize): 需要优化错误处理,计算失败后需要抛出对应错误,以便程序处理\nfunction getFileFilter(ctx: Context) {\n return (req, file, cb) => {\n // size 交给 limits 处理\n const { size, ...rules } = getRules(ctx);\n const ruleKeys = Object.keys(rules);\n const result =\n !ruleKeys.length ||\n !ruleKeys.some((key) => typeof Rules[key] !== 'function' || !Rules[key](file, rules[key], ctx));\n cb(null, result);\n };\n}\n\nexport async function middleware(ctx: Context, next: Next) {\n const { resourceName, actionName, associatedName } = ctx.action.params;\n if (actionName !== 'upload') {\n return next();\n }\n\n // NOTE:\n // 1. 存储引擎选择依赖于字段定义\n // 2. 字段定义中需包含引擎的外键值\n // 3. 无字段时按 storages 表的默认项\n // 4. 插件初始化后应提示用户添加至少一个存储引擎并设为默认\n\n const Storage = ctx.db.getCollection('storages');\n let storage;\n\n if (resourceName === 'attachments') {\n // 如果没有包含关联,则直接按默认文件上传至默认存储引擎\n storage = await Storage.repository.findOne({ filter: { default: true } });\n } else if (associatedName) {\n const AssociatedCollection = ctx.db.getCollection(associatedName);\n const resourceField = AssociatedCollection.getField(resourceName);\n ctx.resourceField = resourceField;\n const { attachment = {} } = resourceField.options;\n storage = await Storage.repository.findOne({\n filter: attachment.storage ? { name: attachment.storage } : { default: true },\n });\n }\n\n if (!storage) {\n console.error('[file-manager] no default or linked storage provided');\n return ctx.throw(500);\n }\n // 传递已取得的存储引擎,避免重查\n ctx.storage = storage;\n\n const storageConfig = getStorageConfig(storage.type);\n if (!storageConfig) {\n console.error(`[file-manager] storage type \"${storage.type}\" is not defined`);\n return ctx.throw(500);\n }\n const multerOptions = {\n fileFilter: getFileFilter(ctx),\n limits: {\n fileSize: Math.min(getRules(ctx).size || LIMIT_MAX_FILE_SIZE, LIMIT_MAX_FILE_SIZE),\n // 每次只允许提交一个文件\n files: LIMIT_FILES,\n },\n storage: storageConfig.make(storage),\n };\n const upload = multer(multerOptions).single(FILE_FIELD_NAME);\n return upload(ctx, next);\n}\n\nexport async function action(ctx: Context, next: Next) {\n const { [FILE_FIELD_NAME]: file, storage } = ctx;\n if (!file) {\n return ctx.throw(400, 'file validation failed');\n }\n\n const storageConfig = getStorageConfig(storage.type);\n const { [storageConfig.filenameKey || 'filename']: name } = file;\n // make compatible filename across cloud service (with path)\n const filename = path.basename(name);\n const extname = path.extname(filename);\n const urlPath = storage.path ? storage.path.replace(/^([^\\/])/, '/$1') : '';\n\n const data = {\n title: file.originalname.replace(extname, ''),\n filename,\n extname,\n // TODO(feature): 暂时两者相同,后面 storage.path 模版化以后,这里只是 file 实际的 path\n path: storage.path,\n size: file.size,\n // 直接缓存起来\n url: `${storage.baseUrl}${urlPath}/${filename}`,\n mimetype: file.mimetype,\n // @ts-ignore\n meta: ctx.request.body,\n ...(storageConfig.getFileData ? storageConfig.getFileData(file) : {}),\n };\n\n const attachment = await ctx.db.sequelize.transaction(async (transaction) => {\n // TODO(optimize): 应使用关联 accessors 获取\n const result = await storage.createAttachment(data, { transaction });\n\n const { associatedName, associatedIndex, resourceName } = ctx.action.params;\n const AssociatedCollection = ctx.db.getCollection(associatedName);\n\n if (AssociatedCollection && associatedIndex && resourceName) {\n const Repo = AssociatedCollection.repository.relation(resourceName).of(associatedIndex);\n const Attachment = ctx.db.getCollection('attachments').model;\n const opts = {\n tk: result[Attachment.primaryKeyAttribute],\n transaction,\n };\n\n if (Repo instanceof BelongsToManyRepository) {\n await Repo.add(opts);\n } else if (Repo instanceof BelongsToRepository) {\n await Repo.set(opts);\n }\n }\n\n return result;\n });\n\n // 将存储引擎的信息附在已创建的记录里,节省一次查询\n // attachment.setDataValue('storage', storage);\n ctx.body = attachment;\n\n await next();\n}\n"]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
name: 'attachments',
|
|
3
|
+
title: '文件管理器',
|
|
4
|
+
fields: [
|
|
5
|
+
{
|
|
6
|
+
comment: '用户文件名(不含扩展名)',
|
|
7
|
+
type: 'string',
|
|
8
|
+
name: 'title',
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
comment: '系统文件名(含扩展名)',
|
|
12
|
+
type: 'string',
|
|
13
|
+
name: 'filename',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
comment: '扩展名(含“.”)',
|
|
17
|
+
type: 'string',
|
|
18
|
+
name: 'extname',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
comment: '文件体积(字节)',
|
|
22
|
+
type: 'integer',
|
|
23
|
+
name: 'size',
|
|
24
|
+
},
|
|
25
|
+
// TODO: 使用暂不明确,以后再考虑
|
|
26
|
+
// {
|
|
27
|
+
// comment: '文件类型(mimetype 前半段,通常用于预览)',
|
|
28
|
+
// type: 'string',
|
|
29
|
+
// name: 'type',
|
|
30
|
+
// },
|
|
31
|
+
{
|
|
32
|
+
type: 'string',
|
|
33
|
+
name: 'mimetype',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
comment: '存储引擎',
|
|
37
|
+
type: 'belongsTo',
|
|
38
|
+
name: 'storage',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
comment: '相对路径(含“/”前缀)',
|
|
42
|
+
type: 'string',
|
|
43
|
+
name: 'path',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
comment: '其他文件信息(如图片的宽高)',
|
|
47
|
+
type: 'jsonb',
|
|
48
|
+
name: 'meta',
|
|
49
|
+
defaultValue: {},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
comment: '网络访问地址',
|
|
53
|
+
type: 'string',
|
|
54
|
+
name: 'url',
|
|
55
|
+
// formula: '{{ storage.baseUrl }}{{ path }}/{{ filename }}'
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=attachments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments.js","sourceRoot":"","sources":["../../src/collections/attachments.ts"],"names":[],"mappings":"AAEA,eAAe;IACb,IAAI,EAAE,aAAa;IACnB,KAAK,EAAE,OAAO;IACd,MAAM,EAAE;QACN;YACE,OAAO,EAAE,cAAc;YACvB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,OAAO;SACd;QACD;YACE,OAAO,EAAE,aAAa;YACtB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,UAAU;SACjB;QACD;YACE,OAAO,EAAE,WAAW;YACpB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,SAAS;SAChB;QACD;YACE,OAAO,EAAE,UAAU;YACnB,IAAI,EAAE,SAAS;YACf,IAAI,EAAE,MAAM;SACb;QACD,qBAAqB;QACrB,IAAI;QACJ,0CAA0C;QAC1C,oBAAoB;QACpB,kBAAkB;QAClB,KAAK;QACL;YACE,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,UAAU;SACjB;QACD;YACE,OAAO,EAAE,MAAM;YACf,IAAI,EAAE,WAAW;YACjB,IAAI,EAAE,SAAS;SAChB;QACD;YACE,OAAO,EAAE,cAAc;YACvB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,MAAM;SACb;QACD;YACE,OAAO,EAAE,gBAAgB;YACzB,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,MAAM;YACZ,YAAY,EAAE,EAAE;SACjB;QACD;YACE,OAAO,EAAE,QAAQ;YACjB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,KAAK;YACX,4DAA4D;SAC7D;KACF;CACmB,CAAC","sourcesContent":["import { CollectionOptions } from '@nocobase/database';\n\nexport default {\n name: 'attachments',\n title: '文件管理器',\n fields: [\n {\n comment: '用户文件名(不含扩展名)',\n type: 'string',\n name: 'title',\n },\n {\n comment: '系统文件名(含扩展名)',\n type: 'string',\n name: 'filename',\n },\n {\n comment: '扩展名(含“.”)',\n type: 'string',\n name: 'extname',\n },\n {\n comment: '文件体积(字节)',\n type: 'integer',\n name: 'size',\n },\n // TODO: 使用暂不明确,以后再考虑\n // {\n // comment: '文件类型(mimetype 前半段,通常用于预览)',\n // type: 'string',\n // name: 'type',\n // },\n {\n type: 'string',\n name: 'mimetype',\n },\n {\n comment: '存储引擎',\n type: 'belongsTo',\n name: 'storage',\n },\n {\n comment: '相对路径(含“/”前缀)',\n type: 'string',\n name: 'path',\n },\n {\n comment: '其他文件信息(如图片的宽高)',\n type: 'jsonb',\n name: 'meta',\n defaultValue: {},\n },\n {\n comment: '网络访问地址',\n type: 'string',\n name: 'url',\n // formula: '{{ storage.baseUrl }}{{ path }}/{{ filename }}'\n },\n ],\n} as CollectionOptions;\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
name: 'storages',
|
|
3
|
+
title: '存储引擎',
|
|
4
|
+
fields: [
|
|
5
|
+
{
|
|
6
|
+
title: '存储引擎名称',
|
|
7
|
+
comment: '存储引擎名称',
|
|
8
|
+
type: 'string',
|
|
9
|
+
name: 'title',
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
title: '英文标识',
|
|
13
|
+
// comment: '英文标识,用于代码层面配置',
|
|
14
|
+
type: 'string',
|
|
15
|
+
name: 'name',
|
|
16
|
+
unique: true,
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
comment: '类型标识,如 local/ali-oss 等',
|
|
20
|
+
type: 'string',
|
|
21
|
+
name: 'type',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
comment: '配置项',
|
|
25
|
+
type: 'jsonb',
|
|
26
|
+
name: 'options',
|
|
27
|
+
defaultValue: {},
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
comment: '文件规则',
|
|
31
|
+
type: 'jsonb',
|
|
32
|
+
name: 'rules',
|
|
33
|
+
defaultValue: {},
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
comment: '存储相对路径模板',
|
|
37
|
+
type: 'string',
|
|
38
|
+
name: 'path',
|
|
39
|
+
defaultValue: '',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
comment: '访问地址前缀',
|
|
43
|
+
type: 'string',
|
|
44
|
+
name: 'baseUrl',
|
|
45
|
+
defaultValue: '',
|
|
46
|
+
},
|
|
47
|
+
// TODO(feature): 需要使用一个实现了可设置默认值的字段
|
|
48
|
+
{
|
|
49
|
+
comment: '默认引擎',
|
|
50
|
+
type: 'radio',
|
|
51
|
+
name: 'default',
|
|
52
|
+
defaultValue: false,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
type: 'hasMany',
|
|
56
|
+
name: 'attachments',
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=storages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storages.js","sourceRoot":"","sources":["../../src/collections/storages.ts"],"names":[],"mappings":"AAEA,eAAe;IACb,IAAI,EAAE,UAAU;IAChB,KAAK,EAAE,MAAM;IACb,MAAM,EAAE;QACN;YACE,KAAK,EAAE,QAAQ;YACf,OAAO,EAAE,QAAQ;YACjB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,OAAO;SACd;QACD;YACE,KAAK,EAAE,MAAM;YACb,4BAA4B;YAC5B,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,MAAM;YACZ,MAAM,EAAE,IAAI;SACb;QACD;YACE,OAAO,EAAE,wBAAwB;YACjC,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,MAAM;SACb;QACD;YACE,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,SAAS;YACf,YAAY,EAAE,EAAE;SACjB;QACD;YACE,OAAO,EAAE,MAAM;YACf,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,OAAO;YACb,YAAY,EAAE,EAAE;SACjB;QACD;YACE,OAAO,EAAE,UAAU;YACnB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,MAAM;YACZ,YAAY,EAAE,EAAE;SACjB;QACD;YACE,OAAO,EAAE,QAAQ;YACjB,IAAI,EAAE,QAAQ;YACd,IAAI,EAAE,SAAS;YACf,YAAY,EAAE,EAAE;SACjB;QACD,oCAAoC;QACpC;YACE,OAAO,EAAE,MAAM;YACf,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,SAAS;YACf,YAAY,EAAE,KAAK;SACpB;QACD;YACE,IAAI,EAAE,SAAS;YACf,IAAI,EAAE,aAAa;SACpB;KACF;CACmB,CAAC","sourcesContent":["import { CollectionOptions } from '@nocobase/database';\n\nexport default {\n name: 'storages',\n title: '存储引擎',\n fields: [\n {\n title: '存储引擎名称',\n comment: '存储引擎名称',\n type: 'string',\n name: 'title',\n },\n {\n title: '英文标识',\n // comment: '英文标识,用于代码层面配置',\n type: 'string',\n name: 'name',\n unique: true,\n },\n {\n comment: '类型标识,如 local/ali-oss 等',\n type: 'string',\n name: 'type',\n },\n {\n comment: '配置项',\n type: 'jsonb',\n name: 'options',\n defaultValue: {},\n },\n {\n comment: '文件规则',\n type: 'jsonb',\n name: 'rules',\n defaultValue: {},\n },\n {\n comment: '存储相对路径模板',\n type: 'string',\n name: 'path',\n defaultValue: '',\n },\n {\n comment: '访问地址前缀',\n type: 'string',\n name: 'baseUrl',\n defaultValue: '',\n },\n // TODO(feature): 需要使用一个实现了可设置默认值的字段\n {\n comment: '默认引擎',\n type: 'radio',\n name: 'default',\n defaultValue: false,\n },\n {\n type: 'hasMany',\n name: 'attachments',\n },\n ],\n} as CollectionOptions;\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const FILE_FIELD_NAME = "file";
|
|
2
|
+
export declare const LIMIT_FILES = 1;
|
|
3
|
+
export declare const LIMIT_MAX_FILE_SIZE: number;
|
|
4
|
+
export declare const STORAGE_TYPE_LOCAL = "local";
|
|
5
|
+
export declare const STORAGE_TYPE_ALI_OSS = "ali-oss";
|
|
6
|
+
export declare const STORAGE_TYPE_S3 = "s3";
|
package/esm/constants.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const FILE_FIELD_NAME = 'file';
|
|
2
|
+
export const LIMIT_FILES = 1;
|
|
3
|
+
export const LIMIT_MAX_FILE_SIZE = 1024 * 1024 * 1024;
|
|
4
|
+
export const STORAGE_TYPE_LOCAL = 'local';
|
|
5
|
+
export const STORAGE_TYPE_ALI_OSS = 'ali-oss';
|
|
6
|
+
export const STORAGE_TYPE_S3 = 's3';
|
|
7
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC;AACtC,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC;AAC7B,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAEtD,MAAM,CAAC,MAAM,kBAAkB,GAAG,OAAO,CAAC;AAC1C,MAAM,CAAC,MAAM,oBAAoB,GAAG,SAAS,CAAC;AAC9C,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,CAAC","sourcesContent":["export const FILE_FIELD_NAME = 'file';\nexport const LIMIT_FILES = 1;\nexport const LIMIT_MAX_FILE_SIZE = 1024 * 1024 * 1024;\n\nexport const STORAGE_TYPE_LOCAL = 'local';\nexport const STORAGE_TYPE_ALI_OSS = 'ali-oss';\nexport const STORAGE_TYPE_S3 = 's3';\n"]}
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
package/esm/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC","sourcesContent":["export * from './constants';\nexport { default } from './server';\n\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as mimetype } from './mimetype';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rules/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEjD,oCAAoC","sourcesContent":["export { default as mimetype } from './mimetype';\n\n// TODO(feature): 提供注册新规则的方法,规则可动态添加\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function (file: any, options: string | string[], ctx: any): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mimetype.js","sourceRoot":"","sources":["../../src/rules/mimetype.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAE/B,MAAM,CAAC,OAAO,WAAW,IAAI,EAAE,UAA6B,GAAG,EAAE,GAAG;IAClE,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;AAClE,CAAC","sourcesContent":["import match from 'mime-match';\n\nexport default function (file, options: string | string[] = '*', ctx): boolean {\n return options.toString().split(',').some(match(file.mimetype));\n}\n"]}
|