@nextlyhq/storage-s3 0.0.2-alpha.77 → 0.0.2-alpha.79
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/README.md +1 -1
- package/dist/index.cjs +141 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -17
- package/dist/index.d.ts +33 -17
- package/dist/index.mjs +141 -62
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.cts
CHANGED
|
@@ -261,6 +261,20 @@ interface BulkDeleteResult {
|
|
|
261
261
|
interface IStorageAdapter {
|
|
262
262
|
/** Upload file buffer to storage */
|
|
263
263
|
upload(buffer: Buffer, options: UploadOptions): Promise<UploadResult>;
|
|
264
|
+
/**
|
|
265
|
+
* Store bytes at exactly `filePath`, replacing anything there (optional).
|
|
266
|
+
*
|
|
267
|
+
* Core calls it only with a key it derived from a file this storage already
|
|
268
|
+
* holds, to keep a file made from that one, such as a resized copy of an
|
|
269
|
+
* image, beside it. A storage without it cannot keep such files, and core
|
|
270
|
+
* serves the original instead. `upload` stays the way new files are
|
|
271
|
+
* stored, since it is the storage that chooses their keys.
|
|
272
|
+
*
|
|
273
|
+
* @experimental
|
|
274
|
+
*/
|
|
275
|
+
uploadAt?(filePath: string, buffer: Buffer, options: {
|
|
276
|
+
mimeType: string;
|
|
277
|
+
}): Promise<UploadResult>;
|
|
264
278
|
/** Delete file from storage */
|
|
265
279
|
delete(filePath: string): Promise<void>;
|
|
266
280
|
/** Bulk delete files from storage. Optional — adapters that support batch operations should implement this. */
|
|
@@ -631,6 +645,19 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
631
645
|
* @returns Upload result with URL and storage path
|
|
632
646
|
*/
|
|
633
647
|
upload(buffer: Buffer, options: UploadOptions): Promise<UploadResult>;
|
|
648
|
+
/**
|
|
649
|
+
* Write bytes at exactly `filePath`, beside a file already in the bucket.
|
|
650
|
+
*
|
|
651
|
+
* Core uses it to keep a file made from a stored one, such as a resized
|
|
652
|
+
* copy of an image, under a key derived from the original's. One request,
|
|
653
|
+
* since such a file is small, with the same cache setting and access as an
|
|
654
|
+
* upload, so it is served the way its original is.
|
|
655
|
+
*
|
|
656
|
+
* @experimental
|
|
657
|
+
*/
|
|
658
|
+
uploadAt(filePath: string, buffer: Buffer, options: {
|
|
659
|
+
mimeType: string;
|
|
660
|
+
}): Promise<UploadResult>;
|
|
634
661
|
/**
|
|
635
662
|
* Delete file from S3.
|
|
636
663
|
*
|
|
@@ -746,6 +773,12 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
746
773
|
* itself refuses a body of another type or length. The browser sets the
|
|
747
774
|
* length from the body; it is not a header the client sends.
|
|
748
775
|
*
|
|
776
|
+
* It covers the configured `Cache-Control` too, which is returned in
|
|
777
|
+
* `headers`: S3 stores an object's Cache-Control only from the upload
|
|
778
|
+
* request's own header, so a browser upload is served with the same caching
|
|
779
|
+
* as a server one only when the client sends it. The client must send every
|
|
780
|
+
* header in `headers`, as it is; one left out fails the signature.
|
|
781
|
+
*
|
|
749
782
|
* @param key - Storage path/key for the upload
|
|
750
783
|
* @param mimeType - MIME type of the file being uploaded
|
|
751
784
|
* @param expiresIn - URL validity in seconds, at most the configured
|
|
@@ -756,23 +789,6 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
756
789
|
getPresignedUploadUrl(key: string, mimeType: string, expiresIn?: number, options?: {
|
|
757
790
|
contentLength?: number;
|
|
758
791
|
}): Promise<ClientUploadData>;
|
|
759
|
-
/**
|
|
760
|
-
* Generate a unique storage key with date-based prefix.
|
|
761
|
-
*
|
|
762
|
-
* Creates keys in format: {folder}/{year}/{month}/{uuid}-{sanitized-filename}
|
|
763
|
-
*
|
|
764
|
-
* @param filename - Original filename (will be sanitized)
|
|
765
|
-
* @param folder - Optional folder/prefix for organizing uploads
|
|
766
|
-
* @returns Generated storage key
|
|
767
|
-
*/
|
|
768
|
-
private generateKey;
|
|
769
|
-
/**
|
|
770
|
-
* Sanitize filename to prevent directory traversal and S3 key issues.
|
|
771
|
-
*
|
|
772
|
-
* @param filename - Original filename
|
|
773
|
-
* @returns Sanitized filename safe for S3 keys
|
|
774
|
-
*/
|
|
775
|
-
private sanitizeFilename;
|
|
776
792
|
/**
|
|
777
793
|
* Check if an error is a "not found" error from S3.
|
|
778
794
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -261,6 +261,20 @@ interface BulkDeleteResult {
|
|
|
261
261
|
interface IStorageAdapter {
|
|
262
262
|
/** Upload file buffer to storage */
|
|
263
263
|
upload(buffer: Buffer, options: UploadOptions): Promise<UploadResult>;
|
|
264
|
+
/**
|
|
265
|
+
* Store bytes at exactly `filePath`, replacing anything there (optional).
|
|
266
|
+
*
|
|
267
|
+
* Core calls it only with a key it derived from a file this storage already
|
|
268
|
+
* holds, to keep a file made from that one, such as a resized copy of an
|
|
269
|
+
* image, beside it. A storage without it cannot keep such files, and core
|
|
270
|
+
* serves the original instead. `upload` stays the way new files are
|
|
271
|
+
* stored, since it is the storage that chooses their keys.
|
|
272
|
+
*
|
|
273
|
+
* @experimental
|
|
274
|
+
*/
|
|
275
|
+
uploadAt?(filePath: string, buffer: Buffer, options: {
|
|
276
|
+
mimeType: string;
|
|
277
|
+
}): Promise<UploadResult>;
|
|
264
278
|
/** Delete file from storage */
|
|
265
279
|
delete(filePath: string): Promise<void>;
|
|
266
280
|
/** Bulk delete files from storage. Optional — adapters that support batch operations should implement this. */
|
|
@@ -631,6 +645,19 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
631
645
|
* @returns Upload result with URL and storage path
|
|
632
646
|
*/
|
|
633
647
|
upload(buffer: Buffer, options: UploadOptions): Promise<UploadResult>;
|
|
648
|
+
/**
|
|
649
|
+
* Write bytes at exactly `filePath`, beside a file already in the bucket.
|
|
650
|
+
*
|
|
651
|
+
* Core uses it to keep a file made from a stored one, such as a resized
|
|
652
|
+
* copy of an image, under a key derived from the original's. One request,
|
|
653
|
+
* since such a file is small, with the same cache setting and access as an
|
|
654
|
+
* upload, so it is served the way its original is.
|
|
655
|
+
*
|
|
656
|
+
* @experimental
|
|
657
|
+
*/
|
|
658
|
+
uploadAt(filePath: string, buffer: Buffer, options: {
|
|
659
|
+
mimeType: string;
|
|
660
|
+
}): Promise<UploadResult>;
|
|
634
661
|
/**
|
|
635
662
|
* Delete file from S3.
|
|
636
663
|
*
|
|
@@ -746,6 +773,12 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
746
773
|
* itself refuses a body of another type or length. The browser sets the
|
|
747
774
|
* length from the body; it is not a header the client sends.
|
|
748
775
|
*
|
|
776
|
+
* It covers the configured `Cache-Control` too, which is returned in
|
|
777
|
+
* `headers`: S3 stores an object's Cache-Control only from the upload
|
|
778
|
+
* request's own header, so a browser upload is served with the same caching
|
|
779
|
+
* as a server one only when the client sends it. The client must send every
|
|
780
|
+
* header in `headers`, as it is; one left out fails the signature.
|
|
781
|
+
*
|
|
749
782
|
* @param key - Storage path/key for the upload
|
|
750
783
|
* @param mimeType - MIME type of the file being uploaded
|
|
751
784
|
* @param expiresIn - URL validity in seconds, at most the configured
|
|
@@ -756,23 +789,6 @@ export declare class S3StorageAdapter implements IStorageAdapter {
|
|
|
756
789
|
getPresignedUploadUrl(key: string, mimeType: string, expiresIn?: number, options?: {
|
|
757
790
|
contentLength?: number;
|
|
758
791
|
}): Promise<ClientUploadData>;
|
|
759
|
-
/**
|
|
760
|
-
* Generate a unique storage key with date-based prefix.
|
|
761
|
-
*
|
|
762
|
-
* Creates keys in format: {folder}/{year}/{month}/{uuid}-{sanitized-filename}
|
|
763
|
-
*
|
|
764
|
-
* @param filename - Original filename (will be sanitized)
|
|
765
|
-
* @param folder - Optional folder/prefix for organizing uploads
|
|
766
|
-
* @returns Generated storage key
|
|
767
|
-
*/
|
|
768
|
-
private generateKey;
|
|
769
|
-
/**
|
|
770
|
-
* Sanitize filename to prevent directory traversal and S3 key issues.
|
|
771
|
-
*
|
|
772
|
-
* @param filename - Original filename
|
|
773
|
-
* @returns Sanitized filename safe for S3 keys
|
|
774
|
-
*/
|
|
775
|
-
private sanitizeFilename;
|
|
776
792
|
/**
|
|
777
793
|
* Check if an error is a "not found" error from S3.
|
|
778
794
|
*
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DeleteObjectCommand, DeleteObjectsCommand, GetObjectCommand, HeadObjectCommand, ListObjectsV2Command, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
2
2
|
import { Upload } from "@aws-sdk/lib-storage";
|
|
3
3
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
4
|
-
// ../nextly/dist/nextly-error-
|
|
4
|
+
// ../nextly/dist/nextly-error-jIKkC0DY.mjs
|
|
5
5
|
function isDbError(err) {
|
|
6
6
|
if (!err || typeof err !== "object") return false;
|
|
7
7
|
const obj = err;
|
|
@@ -91,6 +91,8 @@ const NEXTLY_ERROR_STATUS = {
|
|
|
91
91
|
PAYLOAD_TOO_LARGE: 413,
|
|
92
92
|
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
93
93
|
BUSINESS_RULE_VIOLATION: 422,
|
|
94
|
+
WORKFLOW_STEP_NOT_SUBMIT: 400,
|
|
95
|
+
WORKFLOW_NO_SUBMIT_STEP: 409,
|
|
94
96
|
INTERNAL_ERROR: 500,
|
|
95
97
|
DATABASE_ERROR: 500,
|
|
96
98
|
DATABASE_BUSY: 503,
|
|
@@ -124,10 +126,13 @@ const NEXTLY_ERROR_STATUS = {
|
|
|
124
126
|
NEXTLY_DATABASE_NOT_SET_UP: 503,
|
|
125
127
|
NEXTLY_DATABASE_NOT_MIGRATED: 503,
|
|
126
128
|
NEXTLY_CORE_DESTRUCTIVE_REFUSED: 409,
|
|
129
|
+
NEXTLY_CORE_UPGRADE_UNSUPPORTED: 409,
|
|
130
|
+
NEXTLY_CORE_UPGRADE_INCOMPLETE: 500,
|
|
127
131
|
NEXTLY_MIGRATION_DRIFT: 409,
|
|
128
132
|
NEXTLY_MIGRATION_APPLY_FAILED: 500,
|
|
129
133
|
NEXTLY_MIGRATION_FOREIGN_KEY_VIOLATION: 409,
|
|
130
134
|
NEXTLY_MIGRATION_PARTIALLY_APPLIED: 500,
|
|
135
|
+
NEXTLY_MIGRATION_NOT_PROVEN: 409,
|
|
131
136
|
NEXTLY_MIGRATION_CONCURRENT_RUN: 409,
|
|
132
137
|
NEXTLY_MIGRATION_FILE_MISSING: 404,
|
|
133
138
|
NEXTLY_MIGRATION_SNAPSHOT_MISSING: 404,
|
|
@@ -178,6 +183,9 @@ const NEXTLY_ERROR_STATUS = {
|
|
|
178
183
|
NEXTLY_PER_LANGUAGE_STORAGE_CHANGED: 500,
|
|
179
184
|
NEXTLY_PER_LANGUAGE_STORAGE_MIXED: 500,
|
|
180
185
|
NEXTLY_PER_LANGUAGE_STORAGE_LANGUAGES_UNDECLARED: 500,
|
|
186
|
+
NEXTLY_LANGUAGE_CODES_COLLIDE: 500,
|
|
187
|
+
NEXTLY_LANGUAGE_CODES_NOT_UPGRADED: 500,
|
|
188
|
+
NEXTLY_UPGRADE_LOCALISATION_UNRESOLVED: 500,
|
|
181
189
|
NEXTLY_POSTGRES_SCHEMA_MOVED: 500
|
|
182
190
|
};
|
|
183
191
|
({ ...Object.fromEntries([
|
|
@@ -568,6 +576,37 @@ var NextlyError = class NextlyError extends Error {
|
|
|
568
576
|
});
|
|
569
577
|
}
|
|
570
578
|
/**
|
|
579
|
+
* A submit for review naming a step that does not submit: an approval, a
|
|
580
|
+
* request for changes or a move. Its own code, so a caller that may only
|
|
581
|
+
* submit is told this is not a step it can take this way, never the refusal
|
|
582
|
+
* the step itself would meet. Names the step, the caller's own word.
|
|
583
|
+
*/
|
|
584
|
+
static stepNotSubmit(opts) {
|
|
585
|
+
return new NextlyError({
|
|
586
|
+
code: "WORKFLOW_STEP_NOT_SUBMIT",
|
|
587
|
+
publicMessage: "That step does not submit for review.",
|
|
588
|
+
publicData: { step: opts.step },
|
|
589
|
+
logContext: opts.logContext
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* A submit for review naming no step, of a part whose stage no submit step
|
|
594
|
+
* leaves: content already in review, or a workflow that enters review by
|
|
595
|
+
* another kind of step. Names the part and its stage, which a read of the
|
|
596
|
+
* document tells the caller anyway.
|
|
597
|
+
*/
|
|
598
|
+
static noSubmitStep(opts) {
|
|
599
|
+
return new NextlyError({
|
|
600
|
+
code: "WORKFLOW_NO_SUBMIT_STEP",
|
|
601
|
+
publicMessage: "No step submits this content for review from the stage it is in.",
|
|
602
|
+
publicData: {
|
|
603
|
+
part: opts.part,
|
|
604
|
+
stage: opts.stage
|
|
605
|
+
},
|
|
606
|
+
logContext: opts.logContext
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
571
610
|
* An API key refused because its role allows more than the person creating
|
|
572
611
|
* it may do. The sentence is FORBIDDEN's and names no permission; which one
|
|
573
612
|
* exceeded goes to the log through `logContext`. The code is its own, so a
|
|
@@ -877,7 +916,7 @@ var NextlyError = class NextlyError extends Error {
|
|
|
877
916
|
return NextlyError.isCode(err, "RATE_LIMITED");
|
|
878
917
|
}
|
|
879
918
|
};
|
|
880
|
-
// ../nextly/dist/validate-external-url-
|
|
919
|
+
// ../nextly/dist/validate-external-url-ZJNqaKd2.mjs
|
|
881
920
|
/** Default response body cap: reject anything larger before buffering it all. */
|
|
882
921
|
/**
|
|
883
922
|
* How many bytes a bounded external read may buffer, absent a caller's own cap.
|
|
@@ -967,6 +1006,63 @@ function resolveReadBounds(options) {
|
|
|
967
1006
|
timeoutMs: options?.timeoutMs ?? DEFAULT_READ_TIMEOUT_MS
|
|
968
1007
|
};
|
|
969
1008
|
}
|
|
1009
|
+
// src/storage-key.ts
|
|
1010
|
+
/**
|
|
1011
|
+
* The key a new file is stored under in the bucket.
|
|
1012
|
+
*
|
|
1013
|
+
* One definition for both ways a file arrives: a server upload
|
|
1014
|
+
* (`S3StorageAdapter.upload`, given the collection's prefix as `folder`) and a
|
|
1015
|
+
* direct browser upload (the plugin's `getClientUploadUrl`, which builds the
|
|
1016
|
+
* key before signing it), so the two cannot disagree about the slash after
|
|
1017
|
+
* the prefix and store one collection's files in two places.
|
|
1018
|
+
*
|
|
1019
|
+
* @module storage-key
|
|
1020
|
+
*/
|
|
1021
|
+
/**
|
|
1022
|
+
* Sanitize a filename to prevent directory traversal and S3 key issues.
|
|
1023
|
+
*
|
|
1024
|
+
* Keeps only the basename, and replaces every character outside
|
|
1025
|
+
* `a-z A-Z 0-9 . _ -` with a hyphen.
|
|
1026
|
+
*
|
|
1027
|
+
* @param filename - Original filename
|
|
1028
|
+
* @returns Sanitized filename safe for S3 keys
|
|
1029
|
+
*/
|
|
1030
|
+
function sanitizeFilename(filename) {
|
|
1031
|
+
return (filename.split(/[/\\]/).pop() || filename).replace(/[^a-zA-Z0-9._-]/g, "-");
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* The folder without its trailing slashes, so `"proofs"` and `"proofs/"` name
|
|
1035
|
+
* the same folder and a folder of only slashes names none.
|
|
1036
|
+
*
|
|
1037
|
+
* Trimmed by a loop rather than a pattern: the value is configuration, and a
|
|
1038
|
+
* loop has no backtracking for a long run of slashes to exploit.
|
|
1039
|
+
*/
|
|
1040
|
+
function folderName(folder) {
|
|
1041
|
+
if (!folder) return "";
|
|
1042
|
+
let end = folder.length;
|
|
1043
|
+
while (end > 0 && folder[end - 1] === "/") end--;
|
|
1044
|
+
return folder.slice(0, end);
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Generate a unique storage key with a date-based layout.
|
|
1048
|
+
*
|
|
1049
|
+
* Creates keys in the format `{folder}/{year}/{month}/{uuid}-{sanitized-filename}`,
|
|
1050
|
+
* or `{year}/{month}/{uuid}-{sanitized-filename}` with no folder. The folder
|
|
1051
|
+
* may be written with or without a trailing slash; the key has exactly one.
|
|
1052
|
+
*
|
|
1053
|
+
* @param filename - Original filename (will be sanitized)
|
|
1054
|
+
* @param folder - Optional folder/prefix for organizing uploads
|
|
1055
|
+
* @returns Generated storage key
|
|
1056
|
+
*/
|
|
1057
|
+
function generateStorageKey(filename, folder) {
|
|
1058
|
+
const sanitized = sanitizeFilename(filename);
|
|
1059
|
+
const uuid = crypto.randomUUID();
|
|
1060
|
+
const date = /* @__PURE__ */ new Date();
|
|
1061
|
+
const year = date.getFullYear();
|
|
1062
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
1063
|
+
const name = folderName(folder);
|
|
1064
|
+
return `${name ? `${name}/${year}/${month}` : `${year}/${month}`}/${uuid}-${sanitized}`;
|
|
1065
|
+
}
|
|
970
1066
|
// src/adapter.ts
|
|
971
1067
|
/**
|
|
972
1068
|
* S3 Storage Adapter
|
|
@@ -1138,7 +1234,7 @@ var S3StorageAdapter = class {
|
|
|
1138
1234
|
*/
|
|
1139
1235
|
async upload(buffer, options) {
|
|
1140
1236
|
const client = this.ensureClient();
|
|
1141
|
-
const key =
|
|
1237
|
+
const key = generateStorageKey(options.filename, options.folder);
|
|
1142
1238
|
const uploadParams = {
|
|
1143
1239
|
Bucket: this.resolvedConfig.bucket,
|
|
1144
1240
|
Key: key,
|
|
@@ -1149,7 +1245,7 @@ var S3StorageAdapter = class {
|
|
|
1149
1245
|
if (!this.isR2) uploadParams.ACL = this.resolvedConfig.acl;
|
|
1150
1246
|
const disposition = options.contentDisposition ?? this.resolvedConfig.contentDisposition;
|
|
1151
1247
|
if (disposition) {
|
|
1152
|
-
const filename =
|
|
1248
|
+
const filename = sanitizeFilename(options.filename);
|
|
1153
1249
|
uploadParams.ContentDisposition = disposition === "attachment" ? `attachment; filename="${filename}"` : "inline";
|
|
1154
1250
|
}
|
|
1155
1251
|
uploadParams.Metadata = { "original-filename": options.filename };
|
|
@@ -1163,6 +1259,32 @@ var S3StorageAdapter = class {
|
|
|
1163
1259
|
};
|
|
1164
1260
|
}
|
|
1165
1261
|
/**
|
|
1262
|
+
* Write bytes at exactly `filePath`, beside a file already in the bucket.
|
|
1263
|
+
*
|
|
1264
|
+
* Core uses it to keep a file made from a stored one, such as a resized
|
|
1265
|
+
* copy of an image, under a key derived from the original's. One request,
|
|
1266
|
+
* since such a file is small, with the same cache setting and access as an
|
|
1267
|
+
* upload, so it is served the way its original is.
|
|
1268
|
+
*
|
|
1269
|
+
* @experimental
|
|
1270
|
+
*/
|
|
1271
|
+
async uploadAt(filePath, buffer, options) {
|
|
1272
|
+
const client = this.ensureClient();
|
|
1273
|
+
const params = {
|
|
1274
|
+
Bucket: this.resolvedConfig.bucket,
|
|
1275
|
+
Key: filePath,
|
|
1276
|
+
Body: buffer,
|
|
1277
|
+
ContentType: options.mimeType,
|
|
1278
|
+
CacheControl: this.resolvedConfig.cacheControl
|
|
1279
|
+
};
|
|
1280
|
+
if (!this.isR2) params.ACL = this.resolvedConfig.acl;
|
|
1281
|
+
await client.send(new PutObjectCommand(params));
|
|
1282
|
+
return {
|
|
1283
|
+
url: this.getPublicUrl(filePath),
|
|
1284
|
+
path: filePath
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1287
|
+
/**
|
|
1166
1288
|
* Delete file from S3.
|
|
1167
1289
|
*
|
|
1168
1290
|
* @param filePath - Storage path/key to delete
|
|
@@ -1400,6 +1522,12 @@ var S3StorageAdapter = class {
|
|
|
1400
1522
|
* itself refuses a body of another type or length. The browser sets the
|
|
1401
1523
|
* length from the body; it is not a header the client sends.
|
|
1402
1524
|
*
|
|
1525
|
+
* It covers the configured `Cache-Control` too, which is returned in
|
|
1526
|
+
* `headers`: S3 stores an object's Cache-Control only from the upload
|
|
1527
|
+
* request's own header, so a browser upload is served with the same caching
|
|
1528
|
+
* as a server one only when the client sends it. The client must send every
|
|
1529
|
+
* header in `headers`, as it is; one left out fails the signature.
|
|
1530
|
+
*
|
|
1403
1531
|
* @param key - Storage path/key for the upload
|
|
1404
1532
|
* @param mimeType - MIME type of the file being uploaded
|
|
1405
1533
|
* @param expiresIn - URL validity in seconds, at most the configured
|
|
@@ -1423,41 +1551,22 @@ var S3StorageAdapter = class {
|
|
|
1423
1551
|
return {
|
|
1424
1552
|
uploadUrl: await getSignedUrl(client, command, {
|
|
1425
1553
|
expiresIn: expiration,
|
|
1426
|
-
signableHeaders: /* @__PURE__ */ new Set([
|
|
1554
|
+
signableHeaders: /* @__PURE__ */ new Set([
|
|
1555
|
+
"content-type",
|
|
1556
|
+
"content-length",
|
|
1557
|
+
"cache-control"
|
|
1558
|
+
])
|
|
1427
1559
|
}),
|
|
1428
1560
|
path: key,
|
|
1429
1561
|
method: "PUT",
|
|
1430
|
-
headers: {
|
|
1562
|
+
headers: {
|
|
1563
|
+
"Content-Type": mimeType,
|
|
1564
|
+
"Cache-Control": this.resolvedConfig.cacheControl
|
|
1565
|
+
},
|
|
1431
1566
|
expiresAt: new Date(Date.now() + expiration * 1e3)
|
|
1432
1567
|
};
|
|
1433
1568
|
}
|
|
1434
1569
|
/**
|
|
1435
|
-
* Generate a unique storage key with date-based prefix.
|
|
1436
|
-
*
|
|
1437
|
-
* Creates keys in format: {folder}/{year}/{month}/{uuid}-{sanitized-filename}
|
|
1438
|
-
*
|
|
1439
|
-
* @param filename - Original filename (will be sanitized)
|
|
1440
|
-
* @param folder - Optional folder/prefix for organizing uploads
|
|
1441
|
-
* @returns Generated storage key
|
|
1442
|
-
*/
|
|
1443
|
-
generateKey(filename, folder) {
|
|
1444
|
-
const sanitized = this.sanitizeFilename(filename);
|
|
1445
|
-
const uuid = crypto.randomUUID();
|
|
1446
|
-
const date = /* @__PURE__ */ new Date();
|
|
1447
|
-
const year = date.getFullYear();
|
|
1448
|
-
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
1449
|
-
return `${folder ? `${folder}/${year}/${month}` : `${year}/${month}`}/${uuid}-${sanitized}`;
|
|
1450
|
-
}
|
|
1451
|
-
/**
|
|
1452
|
-
* Sanitize filename to prevent directory traversal and S3 key issues.
|
|
1453
|
-
*
|
|
1454
|
-
* @param filename - Original filename
|
|
1455
|
-
* @returns Sanitized filename safe for S3 keys
|
|
1456
|
-
*/
|
|
1457
|
-
sanitizeFilename(filename) {
|
|
1458
|
-
return (filename.split(/[/\\]/).pop() || filename).replace(/[^a-zA-Z0-9._-]/g, "-");
|
|
1459
|
-
}
|
|
1460
|
-
/**
|
|
1461
1570
|
* Check if an error is a "not found" error from S3.
|
|
1462
1571
|
*
|
|
1463
1572
|
* @param error - Error to check
|
|
@@ -1614,36 +1723,6 @@ function s3Storage(config) {
|
|
|
1614
1723
|
}
|
|
1615
1724
|
};
|
|
1616
1725
|
}
|
|
1617
|
-
/**
|
|
1618
|
-
* Generate a unique storage key with date-based prefix.
|
|
1619
|
-
*
|
|
1620
|
-
* Creates keys in format: {prefix}{year}/{month}/{uuid}-{sanitized-filename}
|
|
1621
|
-
* This matches the format used by S3StorageAdapter for consistency.
|
|
1622
|
-
*
|
|
1623
|
-
* @param filename - Original filename (will be sanitized)
|
|
1624
|
-
* @param prefix - Optional folder/prefix for organizing uploads
|
|
1625
|
-
* @returns Generated storage key
|
|
1626
|
-
*/
|
|
1627
|
-
function generateStorageKey(filename, prefix) {
|
|
1628
|
-
const sanitized = sanitizeFilename(filename);
|
|
1629
|
-
const uuid = crypto.randomUUID();
|
|
1630
|
-
const date = /* @__PURE__ */ new Date();
|
|
1631
|
-
const year = date.getFullYear();
|
|
1632
|
-
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
1633
|
-
return `${prefix ? `${prefix}${year}/${month}` : `${year}/${month}`}/${uuid}-${sanitized}`;
|
|
1634
|
-
}
|
|
1635
|
-
/**
|
|
1636
|
-
* Sanitize filename to prevent directory traversal and S3 key issues.
|
|
1637
|
-
*
|
|
1638
|
-
* Removes path separators and replaces unsafe characters with hyphens.
|
|
1639
|
-
* Keeps only: a-z, A-Z, 0-9, dot, underscore, hyphen
|
|
1640
|
-
*
|
|
1641
|
-
* @param filename - Original filename
|
|
1642
|
-
* @returns Sanitized filename safe for S3 keys
|
|
1643
|
-
*/
|
|
1644
|
-
function sanitizeFilename(filename) {
|
|
1645
|
-
return (filename.split(/[/\\]/).pop() || filename).replace(/[^a-zA-Z0-9._-]/g, "-");
|
|
1646
|
-
}
|
|
1647
1726
|
// src/index.ts
|
|
1648
1727
|
const PACKAGE_NAME = "@nextly/storage-s3";
|
|
1649
1728
|
const PACKAGE_VERSION = "0.1.0";
|