@zucker-framework/storage 1.0.0
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 -0
- package/NOTICE +22 -0
- package/THIRD_PARTY_NOTICES +4 -0
- package/dist/index.d.mts +433 -0
- package/dist/index.d.ts +433 -0
- package/dist/index.js +1081 -0
- package/dist/index.mjs +1035 -0
- package/package.json +44 -0
- package/scripts/r2-storage-canary.mjs +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Zucker Framework
|
|
2
|
+
Copyright 2026 Lind and Zucker Framework contributors
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0. See LICENSE.
|
|
5
|
+
|
|
6
|
+
The framework includes TypeScript adaptations and enhancements of concepts and
|
|
7
|
+
implementations from hsweb-framework and jetlinks-community, both distributed
|
|
8
|
+
under the Apache License, Version 2.0. Individual packages use different subsets
|
|
9
|
+
of the framework. The TypeScript adaptations and Node.js integrations were
|
|
10
|
+
modified for Zucker Framework; they are not the original upstream distribution.
|
|
11
|
+
|
|
12
|
+
hsweb-framework
|
|
13
|
+
https://github.com/hs-web/hsweb-framework
|
|
14
|
+
Copyright 2020 http://hsweb.me
|
|
15
|
+
|
|
16
|
+
jetlinks-community
|
|
17
|
+
https://github.com/jetlinks/jetlinks-community
|
|
18
|
+
|
|
19
|
+
See THIRD_PARTY_NOTICES for license texts and attribution for bundled dependencies
|
|
20
|
+
and reference implementations relevant to this package. External dependencies
|
|
21
|
+
remain subject to their own licenses. This NOTICE is informational and does not
|
|
22
|
+
modify any license or grant rights to upstream trademarks.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { ReadStream } from 'fs';
|
|
2
|
+
import { OnModuleDestroy, Type, DynamicModule, InjectionToken, OptionalFactoryDependency } from '@nestjs/common';
|
|
3
|
+
|
|
4
|
+
interface UploadOptions {
|
|
5
|
+
contentType?: string;
|
|
6
|
+
metadata?: Record<string, string>;
|
|
7
|
+
acl?: 'private' | 'public-read';
|
|
8
|
+
}
|
|
9
|
+
interface UploadResult {
|
|
10
|
+
key: string;
|
|
11
|
+
/**
|
|
12
|
+
* Stable public URL when the provider is configured for public delivery.
|
|
13
|
+
* Private providers may return an empty string; callers must persist `key`
|
|
14
|
+
* and request a short-lived URL with `getDownloadUrl` when needed.
|
|
15
|
+
*/
|
|
16
|
+
url: string;
|
|
17
|
+
size: number;
|
|
18
|
+
}
|
|
19
|
+
interface FileInfo {
|
|
20
|
+
key: string;
|
|
21
|
+
size: number;
|
|
22
|
+
lastModified: Date;
|
|
23
|
+
contentType?: string;
|
|
24
|
+
/**
|
|
25
|
+
* 文件 MD5 哈希 — 对应 jetlinks FileInfo.md5
|
|
26
|
+
* 用于文件去重和完整性校验
|
|
27
|
+
*/
|
|
28
|
+
md5?: string;
|
|
29
|
+
/**
|
|
30
|
+
* 文件 SHA-256 哈希 — 对应 jetlinks FileInfo.sha256
|
|
31
|
+
*/
|
|
32
|
+
sha256?: string;
|
|
33
|
+
}
|
|
34
|
+
type StorageProviderType = 'local' | 's3' | 'webdav';
|
|
35
|
+
/**
|
|
36
|
+
* 文件上传校验配置
|
|
37
|
+
* 支持后缀名白名单/黑名单和 MIME 类型白名单/黑名单
|
|
38
|
+
*/
|
|
39
|
+
interface FileUploadValidation {
|
|
40
|
+
/** 允许的文件后缀(不含 .),设置后仅这些后缀的文件可上传 */
|
|
41
|
+
allowExtensions?: string[];
|
|
42
|
+
/** 拒绝的文件后缀(不含 .) */
|
|
43
|
+
denyExtensions?: string[];
|
|
44
|
+
/** 允许的 MIME 类型 */
|
|
45
|
+
allowMimeTypes?: string[];
|
|
46
|
+
/** 拒绝的 MIME 类型 */
|
|
47
|
+
denyMimeTypes?: string[];
|
|
48
|
+
/** 最大文件大小(字节),0 表示不限制 */
|
|
49
|
+
maxFileSize?: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Default dangerous file extensions that should be blocked unless explicitly allowed.
|
|
53
|
+
*/
|
|
54
|
+
declare const DANGEROUS_EXTENSIONS: readonly string[];
|
|
55
|
+
/**
|
|
56
|
+
* 文件名安全清洗
|
|
57
|
+
*
|
|
58
|
+
* 文件上传安全规则:
|
|
59
|
+
* 1. Unicode NFKC 规范化 — 将不同编码形式的字符统一
|
|
60
|
+
* (如全角字符 `\` → `\`),防止绕过路径检查
|
|
61
|
+
* 2. 将反斜杠统一为正斜杠后仅提取文件名部分
|
|
62
|
+
* 3. 仅允许字母数字和 ._- 组成的文件名用于原始名保留
|
|
63
|
+
*
|
|
64
|
+
* @param fileName 原始文件名
|
|
65
|
+
* @returns 清洗后的安全文件名
|
|
66
|
+
*/
|
|
67
|
+
declare function sanitizeFileName(fileName: string): string;
|
|
68
|
+
/**
|
|
69
|
+
* 检查文件名是否为安全的原始文件名(仅字母数字 ._-)
|
|
70
|
+
*/
|
|
71
|
+
declare function isSafeOriginalFileName(name: string): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* 判断文件是否被拒绝上传(基于后缀名与 MIME 类型校验)
|
|
74
|
+
* 根据文件名、扩展名和大小检查上传限制。
|
|
75
|
+
*
|
|
76
|
+
* @param fileName 文件名
|
|
77
|
+
* @param mimeType MIME 类型(可选)
|
|
78
|
+
* @param validation 校验配置
|
|
79
|
+
* @param fileSize 文件大小(字节,可选),用于最大文件大小校验
|
|
80
|
+
*/
|
|
81
|
+
declare function isFileDenied(fileName: string, mimeType: string | undefined, validation: FileUploadValidation, fileSize?: number): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* 生成基于日期的存储路径
|
|
84
|
+
* 按 YYYYMMDD 归档,避免单目录文件过多
|
|
85
|
+
*/
|
|
86
|
+
declare function generateDateBasedKey(originalName: string, options?: {
|
|
87
|
+
useOriginalFileName?: boolean;
|
|
88
|
+
prefix?: string;
|
|
89
|
+
}): string;
|
|
90
|
+
/**
|
|
91
|
+
* Chunk upload progress — tracks multipart/chunked upload state.
|
|
92
|
+
*/
|
|
93
|
+
/**
|
|
94
|
+
* POSIX 文件权限配置
|
|
95
|
+
*
|
|
96
|
+
* 使用 Node.js 的 fs.chmod 模式数字设置文件权限。
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* // 所有者可读写,同组用户可读
|
|
101
|
+
* const permissions: PosixFilePermissions = { mode: 0o640 };
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
interface PosixFilePermissions {
|
|
105
|
+
/**
|
|
106
|
+
* POSIX 权限模式(八进制),如 0o644, 0o755
|
|
107
|
+
* 使用标准 POSIX 权限位
|
|
108
|
+
*/
|
|
109
|
+
mode: number;
|
|
110
|
+
}
|
|
111
|
+
interface ChunkUploadState {
|
|
112
|
+
/** Upload session ID */
|
|
113
|
+
uploadId: string;
|
|
114
|
+
/** Target storage key */
|
|
115
|
+
key: string;
|
|
116
|
+
/** Total number of chunks expected */
|
|
117
|
+
totalChunks: number;
|
|
118
|
+
/** Chunks received so far */
|
|
119
|
+
receivedChunks: number[];
|
|
120
|
+
/** Total file size in bytes (if known) */
|
|
121
|
+
totalSize?: number;
|
|
122
|
+
/** Whether the upload is complete */
|
|
123
|
+
complete: boolean;
|
|
124
|
+
}
|
|
125
|
+
interface FileManager {
|
|
126
|
+
readonly providerType: StorageProviderType;
|
|
127
|
+
isConfigured(): boolean;
|
|
128
|
+
upload(buffer: Buffer, key: string, options?: UploadOptions): Promise<UploadResult>;
|
|
129
|
+
uploadStream(stream: ReadStream, key: string, options?: UploadOptions): Promise<UploadResult>;
|
|
130
|
+
download(key: string): Promise<{
|
|
131
|
+
stream: ReadStream;
|
|
132
|
+
size: number;
|
|
133
|
+
}>;
|
|
134
|
+
delete(key: string): Promise<void>;
|
|
135
|
+
exists(key: string): Promise<boolean>;
|
|
136
|
+
getPublicUrl(key: string): Promise<string>;
|
|
137
|
+
getDownloadUrl(key: string, expiresIn?: number): Promise<string>;
|
|
138
|
+
list(prefix: string): Promise<FileInfo[]>;
|
|
139
|
+
/**
|
|
140
|
+
* 获取单个文件的元信息
|
|
141
|
+
* 返回 undefined 表示文件不存在
|
|
142
|
+
*/
|
|
143
|
+
getFileInfo?(key: string): Promise<FileInfo | undefined>;
|
|
144
|
+
/**
|
|
145
|
+
* 从指定偏移量开始读取文件 — 对应 jetlinks FileManager.read(id, position)
|
|
146
|
+
*
|
|
147
|
+
* 支持断点续传和范围读取场景。
|
|
148
|
+
* @param key 存储键
|
|
149
|
+
* @param position 起始偏移量(字节)
|
|
150
|
+
*/
|
|
151
|
+
downloadRange?(key: string, position: number, length?: number): Promise<{
|
|
152
|
+
stream: ReadStream;
|
|
153
|
+
size: number;
|
|
154
|
+
}>;
|
|
155
|
+
/**
|
|
156
|
+
* 复制文件
|
|
157
|
+
*/
|
|
158
|
+
copy?(sourceKey: string, destKey: string): Promise<void>;
|
|
159
|
+
/**
|
|
160
|
+
* 移动/重命名文件
|
|
161
|
+
*/
|
|
162
|
+
move?(sourceKey: string, destKey: string): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Initialize a chunked/multipart upload session.
|
|
165
|
+
*
|
|
166
|
+
* @param key Target storage key
|
|
167
|
+
* @param totalChunks Total number of chunks expected
|
|
168
|
+
* @param totalSize Total file size in bytes (optional)
|
|
169
|
+
* @returns Upload session ID
|
|
170
|
+
*/
|
|
171
|
+
initChunkedUpload?(key: string, totalChunks: number, totalSize?: number): Promise<string>;
|
|
172
|
+
/**
|
|
173
|
+
* Upload a single chunk in a multipart upload.
|
|
174
|
+
*
|
|
175
|
+
* @param uploadId Upload session ID from initChunkedUpload
|
|
176
|
+
* @param chunkIndex Zero-based chunk index
|
|
177
|
+
* @param buffer Chunk data
|
|
178
|
+
*/
|
|
179
|
+
uploadChunk?(uploadId: string, chunkIndex: number, buffer: Buffer): Promise<void>;
|
|
180
|
+
/**
|
|
181
|
+
* Complete a chunked upload, assembling all chunks into the final file.
|
|
182
|
+
*
|
|
183
|
+
* @param uploadId Upload session ID
|
|
184
|
+
* @param options Optional upload options (contentType, metadata, acl)
|
|
185
|
+
* @returns Upload result with final URL
|
|
186
|
+
*/
|
|
187
|
+
completeChunkedUpload?(uploadId: string, options?: UploadOptions): Promise<UploadResult>;
|
|
188
|
+
/**
|
|
189
|
+
* Abort a chunked upload, cleaning up any temporary chunks.
|
|
190
|
+
*
|
|
191
|
+
* @param uploadId Upload session ID
|
|
192
|
+
*/
|
|
193
|
+
abortChunkedUpload?(uploadId: string): Promise<void>;
|
|
194
|
+
/**
|
|
195
|
+
* 便捷文件保存
|
|
196
|
+
*
|
|
197
|
+
* 自动根据文件类型生成日期归档路径并保存,简化调用方使用。
|
|
198
|
+
* 内部调用 generateDateBasedKey + upload。
|
|
199
|
+
*
|
|
200
|
+
* @param buffer 文件内容
|
|
201
|
+
* @param fileType 文件类型/后缀(如 "png", ".jpg")
|
|
202
|
+
* @param options 上传选项
|
|
203
|
+
* @returns 上传结果
|
|
204
|
+
*/
|
|
205
|
+
saveFile?(buffer: Buffer, fileType: string, options?: UploadOptions): Promise<UploadResult>;
|
|
206
|
+
/**
|
|
207
|
+
* 应用 POSIX 文件权限
|
|
208
|
+
*
|
|
209
|
+
* 在文件写入后设置 POSIX 权限(仅对本地存储有效,S3/WebDAV 忽略)。
|
|
210
|
+
* 这里使用 Node.js fs.chmodSync() 实现。
|
|
211
|
+
*
|
|
212
|
+
* @param key 存储键
|
|
213
|
+
* @param permissions POSIX 权限配置
|
|
214
|
+
*/
|
|
215
|
+
applyFilePermission?(key: string, permissions: PosixFilePermissions): Promise<void>;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
declare class LocalFileStorage implements FileManager {
|
|
219
|
+
private readonly logger;
|
|
220
|
+
readonly providerType: "local";
|
|
221
|
+
private readonly basePath;
|
|
222
|
+
private readonly baseUrl;
|
|
223
|
+
/** Active chunked upload sessions */
|
|
224
|
+
private readonly chunkedUploads;
|
|
225
|
+
constructor(options: {
|
|
226
|
+
basePath: string;
|
|
227
|
+
baseUrl?: string;
|
|
228
|
+
});
|
|
229
|
+
isConfigured(): boolean;
|
|
230
|
+
upload(buffer: Buffer, key: string, _options?: UploadOptions): Promise<UploadResult>;
|
|
231
|
+
uploadStream(stream: ReadStream, key: string, options?: UploadOptions): Promise<UploadResult>;
|
|
232
|
+
download(key: string): Promise<{
|
|
233
|
+
stream: ReadStream;
|
|
234
|
+
size: number;
|
|
235
|
+
}>;
|
|
236
|
+
delete(key: string): Promise<void>;
|
|
237
|
+
exists(key: string): Promise<boolean>;
|
|
238
|
+
getPublicUrl(key: string): Promise<string>;
|
|
239
|
+
getDownloadUrl(key: string): Promise<string>;
|
|
240
|
+
list(prefix: string): Promise<FileInfo[]>;
|
|
241
|
+
downloadRange(key: string, position: number, length?: number): Promise<{
|
|
242
|
+
stream: ReadStream;
|
|
243
|
+
size: number;
|
|
244
|
+
}>;
|
|
245
|
+
getFileInfo(key: string): Promise<FileInfo | undefined>;
|
|
246
|
+
copy(sourceKey: string, destKey: string): Promise<void>;
|
|
247
|
+
move(sourceKey: string, destKey: string): Promise<void>;
|
|
248
|
+
initChunkedUpload(key: string, totalChunks: number, totalSize?: number): Promise<string>;
|
|
249
|
+
uploadChunk(uploadId: string, chunkIndex: number, buffer: Buffer): Promise<void>;
|
|
250
|
+
completeChunkedUpload(uploadId: string, _options?: UploadOptions): Promise<UploadResult>;
|
|
251
|
+
abortChunkedUpload(uploadId: string): Promise<void>;
|
|
252
|
+
/**
|
|
253
|
+
* 便捷文件保存
|
|
254
|
+
* 自动根据文件类型生成日期归档路径并保存。
|
|
255
|
+
*/
|
|
256
|
+
saveFile(buffer: Buffer, fileType: string, options?: UploadOptions): Promise<UploadResult>;
|
|
257
|
+
/**
|
|
258
|
+
* 应用 POSIX 文件权限
|
|
259
|
+
* 使用 fs.chmodSync 设置文件权限,非 POSIX 系统静默忽略错误。
|
|
260
|
+
*/
|
|
261
|
+
applyFilePermission(key: string, permissions: PosixFilePermissions): Promise<void>;
|
|
262
|
+
/**
|
|
263
|
+
* Resolve a storage key to an absolute file path.
|
|
264
|
+
* Validates against path traversal attacks (e.g. "../../../etc/passwd").
|
|
265
|
+
*/
|
|
266
|
+
private resolvePath;
|
|
267
|
+
private buildUrl;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
type S3ObjectAclMode = 'object-acl' | 'none';
|
|
271
|
+
type S3UploadUrlMode = 'signed' | 'public-only';
|
|
272
|
+
interface S3StorageOptions {
|
|
273
|
+
accessKeyId: string;
|
|
274
|
+
secretAccessKey: string;
|
|
275
|
+
region?: string;
|
|
276
|
+
endpoint?: string;
|
|
277
|
+
bucket: string;
|
|
278
|
+
publicUrl?: string;
|
|
279
|
+
forcePathStyle?: boolean;
|
|
280
|
+
objectAclMode?: S3ObjectAclMode;
|
|
281
|
+
uploadUrlMode?: S3UploadUrlMode;
|
|
282
|
+
signedUrlExpiresIn?: number;
|
|
283
|
+
}
|
|
284
|
+
declare class S3Storage implements FileManager, OnModuleDestroy {
|
|
285
|
+
private readonly logger;
|
|
286
|
+
private readonly client;
|
|
287
|
+
private readonly bucket;
|
|
288
|
+
private readonly publicUrl;
|
|
289
|
+
private readonly objectAclMode;
|
|
290
|
+
private readonly uploadUrlMode;
|
|
291
|
+
private readonly signedUrlExpiresIn;
|
|
292
|
+
readonly providerType: "s3";
|
|
293
|
+
constructor(options: S3StorageOptions);
|
|
294
|
+
onModuleDestroy(): void;
|
|
295
|
+
isConfigured(): boolean;
|
|
296
|
+
upload(buffer: Buffer, key: string, options?: UploadOptions): Promise<UploadResult>;
|
|
297
|
+
uploadStream(stream: ReadStream, key: string, options?: UploadOptions): Promise<UploadResult>;
|
|
298
|
+
download(key: string): Promise<{
|
|
299
|
+
stream: ReadStream;
|
|
300
|
+
size: number;
|
|
301
|
+
}>;
|
|
302
|
+
delete(key: string): Promise<void>;
|
|
303
|
+
exists(key: string): Promise<boolean>;
|
|
304
|
+
getPublicUrl(key: string): Promise<string>;
|
|
305
|
+
getDownloadUrl(key: string, expiresIn?: number): Promise<string>;
|
|
306
|
+
list(prefix: string): Promise<FileInfo[]>;
|
|
307
|
+
getFileInfo(key: string): Promise<FileInfo | undefined>;
|
|
308
|
+
copy(sourceKey: string, destKey: string): Promise<void>;
|
|
309
|
+
move(sourceKey: string, destKey: string): Promise<void>;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
interface WebDavStorageOptions {
|
|
313
|
+
/** WebDAV 服务地址,如 https://nextcloud.example.com/remote.php/dav/files/user */
|
|
314
|
+
url: string;
|
|
315
|
+
username: string;
|
|
316
|
+
password: string;
|
|
317
|
+
/** 公开访问 URL 前缀(可选) */
|
|
318
|
+
publicUrl?: string;
|
|
319
|
+
/** 基础路径前缀(可选,默认 '/') */
|
|
320
|
+
basePath?: string;
|
|
321
|
+
}
|
|
322
|
+
declare class WebDavStorage implements FileManager {
|
|
323
|
+
private readonly logger;
|
|
324
|
+
private clientPromise?;
|
|
325
|
+
private readonly publicUrl;
|
|
326
|
+
private readonly basePath;
|
|
327
|
+
private readonly directoryPromises;
|
|
328
|
+
readonly providerType: "webdav";
|
|
329
|
+
private readonly options;
|
|
330
|
+
constructor(options: WebDavStorageOptions);
|
|
331
|
+
private initClient;
|
|
332
|
+
private getClient;
|
|
333
|
+
isConfigured(): boolean;
|
|
334
|
+
upload(buffer: Buffer, key: string, options?: UploadOptions): Promise<UploadResult>;
|
|
335
|
+
uploadStream(stream: ReadStream, key: string, options?: UploadOptions): Promise<UploadResult>;
|
|
336
|
+
download(key: string): Promise<{
|
|
337
|
+
stream: ReadStream;
|
|
338
|
+
size: number;
|
|
339
|
+
}>;
|
|
340
|
+
delete(key: string): Promise<void>;
|
|
341
|
+
exists(key: string): Promise<boolean>;
|
|
342
|
+
getPublicUrl(key: string): Promise<string>;
|
|
343
|
+
getDownloadUrl(key: string): Promise<string>;
|
|
344
|
+
list(prefix: string): Promise<FileInfo[]>;
|
|
345
|
+
getFileInfo(key: string): Promise<FileInfo | undefined>;
|
|
346
|
+
copy(sourceKey: string, destKey: string): Promise<void>;
|
|
347
|
+
move(sourceKey: string, destKey: string): Promise<void>;
|
|
348
|
+
/**
|
|
349
|
+
* 递归创建目录
|
|
350
|
+
*/
|
|
351
|
+
private ensureDirectory;
|
|
352
|
+
private createDirectoryIfMissing;
|
|
353
|
+
private resolvePath;
|
|
354
|
+
private stripBasePath;
|
|
355
|
+
private buildUrl;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* 文件生命周期管理器。
|
|
360
|
+
*
|
|
361
|
+
* 支持文件过期自动清理、临时文件管理、存储配额检查。
|
|
362
|
+
*/
|
|
363
|
+
interface FileLifecyclePolicy {
|
|
364
|
+
/** 文件最大保留时间(毫秒),0 表示永久 */
|
|
365
|
+
maxAge?: number;
|
|
366
|
+
/** 文件最大数量,超出后按 FIFO 清理 */
|
|
367
|
+
maxCount?: number;
|
|
368
|
+
/** 文件最大总大小(字节),超出后按 FIFO 清理 */
|
|
369
|
+
maxTotalSize?: number;
|
|
370
|
+
/** 文件路径前缀匹配(用于作用域限定) */
|
|
371
|
+
pathPrefix?: string;
|
|
372
|
+
}
|
|
373
|
+
interface FileMetadata {
|
|
374
|
+
key: string;
|
|
375
|
+
size: number;
|
|
376
|
+
createdAt: number;
|
|
377
|
+
lastAccessedAt?: number;
|
|
378
|
+
contentType?: string;
|
|
379
|
+
tags?: Record<string, string>;
|
|
380
|
+
}
|
|
381
|
+
interface LifecycleResult {
|
|
382
|
+
deletedCount: number;
|
|
383
|
+
freedBytes: number;
|
|
384
|
+
reason: 'expired' | 'count_exceeded' | 'size_exceeded';
|
|
385
|
+
}
|
|
386
|
+
declare class FileLifecycleManager {
|
|
387
|
+
private readonly logger;
|
|
388
|
+
private readonly policies;
|
|
389
|
+
/** 注册一个生命周期策略 */
|
|
390
|
+
registerPolicy(policy: FileLifecyclePolicy): void;
|
|
391
|
+
/** 获取所有已注册策略 */
|
|
392
|
+
getPolicies(): FileLifecyclePolicy[];
|
|
393
|
+
/**
|
|
394
|
+
* 评估文件列表,返回需要清理的文件 keys。
|
|
395
|
+
* 调用方负责实际删除操作。
|
|
396
|
+
*/
|
|
397
|
+
evaluate(files: FileMetadata[], policy?: FileLifecyclePolicy): string[];
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
declare const FILE_MANAGER = "FILE_MANAGER";
|
|
401
|
+
type StorageModuleOptions = {
|
|
402
|
+
provider: 'local';
|
|
403
|
+
basePath: string;
|
|
404
|
+
baseUrl?: string;
|
|
405
|
+
} | ({
|
|
406
|
+
provider: 's3';
|
|
407
|
+
} & S3StorageOptions) | ({
|
|
408
|
+
provider: 'webdav';
|
|
409
|
+
} & WebDavStorageOptions);
|
|
410
|
+
interface StorageModuleAsyncOptions {
|
|
411
|
+
imports?: Array<Type | DynamicModule>;
|
|
412
|
+
useFactory?: (...args: unknown[]) => StorageModuleOptions | Promise<StorageModuleOptions>;
|
|
413
|
+
useClass?: Type<StorageModuleOptionsFactory>;
|
|
414
|
+
useExisting?: Type<StorageModuleOptionsFactory>;
|
|
415
|
+
inject?: Array<InjectionToken | OptionalFactoryDependency>;
|
|
416
|
+
}
|
|
417
|
+
interface StorageModuleOptionsFactory {
|
|
418
|
+
createStorageOptions(): StorageModuleOptions | Promise<StorageModuleOptions>;
|
|
419
|
+
}
|
|
420
|
+
declare class ZuckerStorageModule {
|
|
421
|
+
static forRoot(options: StorageModuleOptions): DynamicModule;
|
|
422
|
+
static forRootAsync(options: StorageModuleAsyncOptions): DynamicModule;
|
|
423
|
+
private static createFileManager;
|
|
424
|
+
private static createAsyncProviders;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
declare enum StorageProvider {
|
|
428
|
+
LOCAL = "LOCAL",
|
|
429
|
+
S3 = "S3",
|
|
430
|
+
WEBDAV = "WEBDAV"
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
export { type ChunkUploadState, DANGEROUS_EXTENSIONS, FILE_MANAGER, type FileInfo, FileLifecycleManager, type FileLifecyclePolicy, type FileManager, type FileMetadata, type FileUploadValidation, type LifecycleResult, LocalFileStorage, type PosixFilePermissions, type S3ObjectAclMode, S3Storage, type S3StorageOptions, type S3UploadUrlMode, type StorageModuleAsyncOptions, type StorageModuleOptions, type StorageModuleOptionsFactory, StorageProvider, type StorageProviderType, type UploadOptions, type UploadResult, WebDavStorage, type WebDavStorageOptions, ZuckerStorageModule, generateDateBasedKey, isFileDenied, isSafeOriginalFileName, sanitizeFileName };
|