nestjs-r2-storage 1.4.2 → 1.4.3
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/dist/index.d.ts +8 -6
- package/dist/index.js.map +1 -1
- package/dist/r2-storage/cloudflare.service.d.ts +32 -49
- package/dist/r2-storage/cloudflare.service.js +215 -219
- package/dist/r2-storage/cloudflare.service.js.map +1 -1
- package/dist/r2-storage/interfaces/cloudflare-service.interface.d.ts +16 -0
- package/dist/r2-storage/interfaces/cloudflare-service.interface.js +3 -0
- package/dist/r2-storage/interfaces/cloudflare-service.interface.js.map +1 -0
- package/dist/r2-storage/interfaces/photo-manager.interface.d.ts +37 -0
- package/dist/r2-storage/interfaces/photo-manager.interface.js +3 -0
- package/dist/r2-storage/interfaces/photo-manager.interface.js.map +1 -0
- package/dist/r2-storage/interfaces/storage-options.interface.d.ts +1 -1
- package/dist/r2-storage/photo-manager.service.d.ts +2 -37
- package/dist/r2-storage/photo-manager.service.js +333 -333
- package/dist/r2-storage/photo-manager.service.js.map +1 -1
- package/dist/r2-storage/utils/validate-options.utils.d.ts +2 -0
- package/dist/r2-storage/utils/validate-options.utils.js +21 -0
- package/dist/r2-storage/utils/validate-options.utils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
export { StorageOptions, FileFieldConfig, PhotoFieldConfig, StorageModuleOptions, AccessMode, } from
|
|
2
|
-
export { CloudflareService,
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
1
|
+
export { StorageOptions, FileFieldConfig, PhotoFieldConfig, StorageModuleOptions, AccessMode, } from "./r2-storage/interfaces/storage-options.interface";
|
|
2
|
+
export { CloudflareService, AccessModeError, } from "./r2-storage/cloudflare.service";
|
|
3
|
+
export { UploadUrlResult, DownloadUrlResult, FileInfo, } from "./r2-storage/interfaces/cloudflare-service.interface";
|
|
4
|
+
export { PhotoManagerService } from "./r2-storage/photo-manager.service";
|
|
5
|
+
export { PhotoField, AppendUrlsOptions, PhotoUploadRequest, PhotoUploadResponse, CreatePhotosResult, UpdatePhotosResult, DeletePhotosResult, } from "./r2-storage/interfaces/photo-manager.interface";
|
|
6
|
+
export { R2StorageModule } from "./r2-storage/r2-storage.module";
|
|
7
|
+
export { PhotoFields, UploadUrls, StorageInfo, } from "./r2-storage/decorators/photo-fields.decorator";
|
|
8
|
+
export { parseFieldPath, getNestedValue, setNestedValue, collectNestedValues, isArrayPath, getArrayBasePath, getArrayElementPath, getAllArrayItemPaths, ParsedPath, PathSegment, } from "./r2-storage/utils/nested-value.util";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAQA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAQA,sEAGyC;AAFvC,uHAAA,iBAAiB,OAAA;AACjB,qHAAA,eAAe,OAAA;AASjB,4EAAyE;AAAhE,4HAAA,mBAAmB,OAAA;AAY5B,oEAAiE;AAAxD,oHAAA,eAAe,OAAA;AAExB,yFAIwD;AAHtD,qHAAA,WAAW,OAAA;AACX,oHAAA,UAAU,OAAA;AACV,qHAAA,WAAW,OAAA;AAGb,0EAW8C;AAV5C,mHAAA,cAAc,OAAA;AACd,mHAAA,cAAc,OAAA;AACd,mHAAA,cAAc,OAAA;AACd,wHAAA,mBAAmB,OAAA;AACnB,gHAAA,WAAW,OAAA;AACX,qHAAA,gBAAgB,OAAA;AAChB,wHAAA,mBAAmB,OAAA;AACnB,yHAAA,oBAAoB,OAAA"}
|
|
@@ -1,49 +1,32 @@
|
|
|
1
|
-
import { OnModuleInit, OnModuleDestroy, BadRequestException } from
|
|
2
|
-
import { StorageOptions } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
onModuleDestroy(): void;
|
|
34
|
-
private initializeClient;
|
|
35
|
-
setOptions(options: StorageOptions): void;
|
|
36
|
-
getOptions(): StorageOptions;
|
|
37
|
-
private sanitizeFilename;
|
|
38
|
-
private detectMimeType;
|
|
39
|
-
getUploadUrl(fileKey: string, fileSize: number, customFilename?: string, contentType?: string): Promise<UploadUrlResult>;
|
|
40
|
-
getDownloadUrl(fileKey: string): Promise<DownloadUrlResult>;
|
|
41
|
-
deleteFile(fileKey: string): Promise<boolean>;
|
|
42
|
-
deleteFiles(fileKeys: string[]): Promise<{
|
|
43
|
-
success: string[];
|
|
44
|
-
failed: string[];
|
|
45
|
-
}>;
|
|
46
|
-
getFileInfo(fileKey: string): Promise<FileInfo | null>;
|
|
47
|
-
fileExists(fileKey: string): Promise<boolean>;
|
|
48
|
-
generateFileKey(prefix: string, filename: string): string;
|
|
49
|
-
}
|
|
1
|
+
import { OnModuleInit, OnModuleDestroy, BadRequestException } from "@nestjs/common";
|
|
2
|
+
import { StorageOptions } from "./interfaces/storage-options.interface";
|
|
3
|
+
import { DownloadUrlResult, FileInfo, UploadUrlResult } from "./interfaces/cloudflare-service.interface";
|
|
4
|
+
export declare class AccessModeError extends BadRequestException {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class CloudflareService implements OnModuleInit, OnModuleDestroy {
|
|
8
|
+
private readonly storageOptions;
|
|
9
|
+
private s3Client;
|
|
10
|
+
private options;
|
|
11
|
+
private readonly defaultExpiry;
|
|
12
|
+
private readonly defaultAccessMode;
|
|
13
|
+
constructor(storageOptions: StorageOptions);
|
|
14
|
+
private get accessMode();
|
|
15
|
+
private isPublicAccessAllowed;
|
|
16
|
+
onModuleInit(): void;
|
|
17
|
+
onModuleDestroy(): void;
|
|
18
|
+
private initializeClient;
|
|
19
|
+
getOptions(): StorageOptions;
|
|
20
|
+
private sanitizeFilename;
|
|
21
|
+
private detectMimeType;
|
|
22
|
+
getUploadUrl(fileKey: string, fileSize: number, customFilename?: string, contentType?: string): Promise<UploadUrlResult>;
|
|
23
|
+
getDownloadUrl(fileKey: string): Promise<DownloadUrlResult>;
|
|
24
|
+
deleteFile(fileKey: string): Promise<boolean>;
|
|
25
|
+
deleteFiles(fileKeys: string[]): Promise<{
|
|
26
|
+
success: string[];
|
|
27
|
+
failed: string[];
|
|
28
|
+
}>;
|
|
29
|
+
getFileInfo(fileKey: string): Promise<FileInfo | null>;
|
|
30
|
+
fileExists(fileKey: string): Promise<boolean>;
|
|
31
|
+
generateFileKey(prefix: string, filename: string): string;
|
|
32
|
+
}
|
|
@@ -1,220 +1,216 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
-
};
|
|
24
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
-
var ownKeys = function(o) {
|
|
26
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
-
var ar = [];
|
|
28
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
-
return ar;
|
|
30
|
-
};
|
|
31
|
-
return ownKeys(o);
|
|
32
|
-
};
|
|
33
|
-
return function (mod) {
|
|
34
|
-
if (mod && mod.__esModule) return mod;
|
|
35
|
-
var result = {};
|
|
36
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
-
__setModuleDefault(result, mod);
|
|
38
|
-
return result;
|
|
39
|
-
};
|
|
40
|
-
})();
|
|
41
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
42
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
-
};
|
|
44
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
45
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
46
|
-
};
|
|
47
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
-
exports.CloudflareService = exports.AccessModeError = void 0;
|
|
49
|
-
const common_1 = require("@nestjs/common");
|
|
50
|
-
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
51
|
-
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
52
|
-
const path = __importStar(require("path"));
|
|
53
|
-
const mime = __importStar(require("mime-types"));
|
|
54
|
-
const constants_1 = require("./constants");
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
this.
|
|
66
|
-
this.
|
|
67
|
-
this.
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
this.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
(0, common_1.Injectable)(),
|
|
217
|
-
__param(0, (0, common_1.Inject)(constants_1.STORAGE_OPTIONS)),
|
|
218
|
-
__metadata("design:paramtypes", [Object])
|
|
219
|
-
], CloudflareService);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
42
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
+
};
|
|
44
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
45
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.CloudflareService = exports.AccessModeError = void 0;
|
|
49
|
+
const common_1 = require("@nestjs/common");
|
|
50
|
+
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
51
|
+
const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
|
|
52
|
+
const path = __importStar(require("path"));
|
|
53
|
+
const mime = __importStar(require("mime-types"));
|
|
54
|
+
const constants_1 = require("./constants");
|
|
55
|
+
const validate_options_utils_1 = require("./utils/validate-options.utils");
|
|
56
|
+
class AccessModeError extends common_1.BadRequestException {
|
|
57
|
+
constructor(message) {
|
|
58
|
+
super(message);
|
|
59
|
+
this.name = "AccessModeError";
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.AccessModeError = AccessModeError;
|
|
63
|
+
let CloudflareService = class CloudflareService {
|
|
64
|
+
constructor(storageOptions) {
|
|
65
|
+
this.storageOptions = storageOptions;
|
|
66
|
+
this.defaultExpiry = 3600;
|
|
67
|
+
this.defaultAccessMode = "hybrid";
|
|
68
|
+
this.options = this.storageOptions;
|
|
69
|
+
}
|
|
70
|
+
get accessMode() {
|
|
71
|
+
return this.options.accessMode || this.defaultAccessMode;
|
|
72
|
+
}
|
|
73
|
+
isPublicAccessAllowed() {
|
|
74
|
+
return this.accessMode === "public-read" || this.accessMode === "hybrid";
|
|
75
|
+
}
|
|
76
|
+
onModuleInit() {
|
|
77
|
+
(0, validate_options_utils_1.validateOptions)(this.options);
|
|
78
|
+
this.initializeClient();
|
|
79
|
+
}
|
|
80
|
+
onModuleDestroy() {
|
|
81
|
+
if (this.s3Client) {
|
|
82
|
+
this.s3Client.destroy();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
initializeClient() {
|
|
86
|
+
this.s3Client = new client_s3_1.S3Client({
|
|
87
|
+
endpoint: this.options.endpoint,
|
|
88
|
+
region: this.options.region || "auto",
|
|
89
|
+
credentials: {
|
|
90
|
+
accessKeyId: this.options.accessKeyId,
|
|
91
|
+
secretAccessKey: this.options.secretAccessKey,
|
|
92
|
+
},
|
|
93
|
+
forcePathStyle: true,
|
|
94
|
+
requestChecksumCalculation: "WHEN_REQUIRED",
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
getOptions() {
|
|
98
|
+
return this.options;
|
|
99
|
+
}
|
|
100
|
+
sanitizeFilename(filename) {
|
|
101
|
+
const sanitized = filename.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
102
|
+
const unique = `${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
103
|
+
const ext = path.extname(sanitized);
|
|
104
|
+
const basename = path.basename(sanitized, ext);
|
|
105
|
+
return `${basename}_${unique}${ext}`;
|
|
106
|
+
}
|
|
107
|
+
detectMimeType(filename, fallbackContentType = "application/octet-stream") {
|
|
108
|
+
const mimeType = mime.lookup(filename);
|
|
109
|
+
return mimeType || fallbackContentType;
|
|
110
|
+
}
|
|
111
|
+
async getUploadUrl(fileKey, fileSize, customFilename, contentType) {
|
|
112
|
+
const filename = customFilename || path.basename(fileKey);
|
|
113
|
+
const sanitizedFilename = this.sanitizeFilename(filename);
|
|
114
|
+
const finalFileKey = fileKey.includes("/")
|
|
115
|
+
? `${path.dirname(fileKey)}/${sanitizedFilename}`
|
|
116
|
+
: sanitizedFilename;
|
|
117
|
+
const mimeType = contentType || this.detectMimeType(sanitizedFilename);
|
|
118
|
+
const command = new client_s3_1.PutObjectCommand({
|
|
119
|
+
Bucket: this.options.bucketName,
|
|
120
|
+
Key: finalFileKey,
|
|
121
|
+
ContentType: mimeType,
|
|
122
|
+
});
|
|
123
|
+
const expiry = this.options.signedUrlExpiry || this.defaultExpiry;
|
|
124
|
+
const uploadUrl = await (0, s3_request_presigner_1.getSignedUrl)(this.s3Client, command, {
|
|
125
|
+
expiresIn: expiry,
|
|
126
|
+
signableHeaders: new Set(["host", "content-type"]),
|
|
127
|
+
});
|
|
128
|
+
let publicUrl = null;
|
|
129
|
+
if (this.options.publicUrlBase && this.isPublicAccessAllowed()) {
|
|
130
|
+
publicUrl = `${this.options.publicUrlBase}/${finalFileKey}`;
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
uploadUrl,
|
|
134
|
+
fileKey: finalFileKey,
|
|
135
|
+
publicUrl,
|
|
136
|
+
mimeType,
|
|
137
|
+
sizeField: fileSize,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
async getDownloadUrl(fileKey) {
|
|
141
|
+
const command = new client_s3_1.GetObjectCommand({
|
|
142
|
+
Bucket: this.options.bucketName,
|
|
143
|
+
Key: fileKey,
|
|
144
|
+
});
|
|
145
|
+
const expiry = this.options.signedUrlExpiry || this.defaultExpiry;
|
|
146
|
+
const downloadUrl = await (0, s3_request_presigner_1.getSignedUrl)(this.s3Client, command, {
|
|
147
|
+
expiresIn: expiry,
|
|
148
|
+
});
|
|
149
|
+
let publicUrl = null;
|
|
150
|
+
if (this.options.publicUrlBase && this.isPublicAccessAllowed()) {
|
|
151
|
+
publicUrl = `${this.options.publicUrlBase}/${fileKey}`;
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
downloadUrl,
|
|
155
|
+
publicUrl,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
async deleteFile(fileKey) {
|
|
159
|
+
try {
|
|
160
|
+
const command = new client_s3_1.DeleteObjectCommand({
|
|
161
|
+
Bucket: this.options.bucketName,
|
|
162
|
+
Key: fileKey,
|
|
163
|
+
});
|
|
164
|
+
await this.s3Client.send(command);
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
console.error(`Failed to delete file ${fileKey}:`, error);
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async deleteFiles(fileKeys) {
|
|
173
|
+
const results = await Promise.all(fileKeys.map(async (fileKey) => {
|
|
174
|
+
const success = await this.deleteFile(fileKey);
|
|
175
|
+
return { fileKey, success };
|
|
176
|
+
}));
|
|
177
|
+
return {
|
|
178
|
+
success: results.filter((r) => r.success).map((r) => r.fileKey),
|
|
179
|
+
failed: results.filter((r) => !r.success).map((r) => r.fileKey),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async getFileInfo(fileKey) {
|
|
183
|
+
try {
|
|
184
|
+
const command = new client_s3_1.HeadObjectCommand({
|
|
185
|
+
Bucket: this.options.bucketName,
|
|
186
|
+
Key: fileKey,
|
|
187
|
+
});
|
|
188
|
+
const response = await this.s3Client.send(command);
|
|
189
|
+
return {
|
|
190
|
+
size: response.ContentLength || 0,
|
|
191
|
+
lastModified: response.LastModified,
|
|
192
|
+
contentType: response.ContentType,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (error?.$metadata?.httpStatusCode === 404) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
async fileExists(fileKey) {
|
|
203
|
+
const fileInfo = await this.getFileInfo(fileKey);
|
|
204
|
+
return fileInfo !== null;
|
|
205
|
+
}
|
|
206
|
+
generateFileKey(prefix, filename) {
|
|
207
|
+
return `${prefix}/${this.sanitizeFilename(filename)}`;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
exports.CloudflareService = CloudflareService;
|
|
211
|
+
exports.CloudflareService = CloudflareService = __decorate([
|
|
212
|
+
(0, common_1.Injectable)(),
|
|
213
|
+
__param(0, (0, common_1.Inject)(constants_1.STORAGE_OPTIONS)),
|
|
214
|
+
__metadata("design:paramtypes", [Object])
|
|
215
|
+
], CloudflareService);
|
|
220
216
|
//# sourceMappingURL=cloudflare.service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cloudflare.service.js","sourceRoot":"","sources":["../../src/r2-storage/cloudflare.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"cloudflare.service.js","sourceRoot":"","sources":["../../src/r2-storage/cloudflare.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAMwB;AACxB,kDAM4B;AAC5B,wEAA6D;AAC7D,2CAA6B;AAC7B,iDAAmC;AAKnC,2CAA8C;AAC9C,2EAAiE;AAOjE,MAAa,eAAgB,SAAQ,4BAAmB;IACtD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AALD,0CAKC;AAGM,IAAM,iBAAiB,GAAvB,MAAM,iBAAiB;IAM5B,YAC2B,cAA+C;QAA9B,mBAAc,GAAd,cAAc,CAAgB;QAJzD,kBAAa,GAAG,IAAI,CAAC;QACrB,sBAAiB,GAAe,QAAQ,CAAC;QAKxD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,cAAc,CAAC;IACrC,CAAC;IAED,IAAY,UAAU;QACpB,OAAO,IAAI,CAAC,OAAO,CAAC,UAAU,IAAI,IAAI,CAAC,iBAAiB,CAAC;IAC3D,CAAC;IAEO,qBAAqB;QAC3B,OAAO,IAAI,CAAC,UAAU,KAAK,aAAa,IAAI,IAAI,CAAC,UAAU,KAAK,QAAQ,CAAC;IAC3E,CAAC;IAED,YAAY;QACV,IAAA,wC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d,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,OAAe;QAC9B,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QACjD,OAAO,QAAQ,KAAK,IAAI,CAAC;IAC3B,CAAC;IAED,eAAe,CAAC,MAAc,EAAE,QAAgB;QAC9C,OAAO,GAAG,MAAM,IAAI,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,EAAE,CAAC;IACxD,CAAC;CACF,CAAA;AAlNY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,mBAAU,GAAE;IAQR,WAAA,IAAA,eAAM,EAAC,2BAAe,CAAC,CAAA;;GAPf,iBAAiB,CAkN7B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface UploadUrlResult {
|
|
2
|
+
uploadUrl: string;
|
|
3
|
+
fileKey: string;
|
|
4
|
+
publicUrl: string | null;
|
|
5
|
+
mimeType: string;
|
|
6
|
+
sizeField?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface DownloadUrlResult {
|
|
9
|
+
downloadUrl: string;
|
|
10
|
+
publicUrl: string | null;
|
|
11
|
+
}
|
|
12
|
+
export interface FileInfo {
|
|
13
|
+
size: number;
|
|
14
|
+
lastModified?: Date;
|
|
15
|
+
contentType?: string;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cloudflare-service.interface.js","sourceRoot":"","sources":["../../../src/r2-storage/interfaces/cloudflare-service.interface.ts"],"names":[],"mappings":""}
|