@edgestore/server 0.7.0 → 0.8.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/dist/adapters/astro/index.d.ts.map +1 -1
- package/dist/adapters/astro/index.js +9 -15
- package/dist/adapters/astro/index.mjs +10 -16
- package/dist/adapters/express/index.d.ts.map +1 -1
- package/dist/adapters/express/index.js +8 -13
- package/dist/adapters/express/index.mjs +9 -14
- package/dist/adapters/fastify/index.d.ts +1 -1
- package/dist/adapters/fastify/index.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +7 -14
- package/dist/adapters/fastify/index.mjs +8 -15
- package/dist/adapters/hono/index.d.ts +4 -70
- package/dist/adapters/hono/index.d.ts.map +1 -1
- package/dist/adapters/hono/index.js +12 -13
- package/dist/adapters/hono/index.mjs +13 -14
- package/dist/adapters/next/app/index.d.ts.map +1 -1
- package/dist/adapters/next/app/index.js +7 -13
- package/dist/adapters/next/app/index.mjs +8 -14
- package/dist/adapters/next/pages/index.d.ts.map +1 -1
- package/dist/adapters/next/pages/index.js +8 -13
- package/dist/adapters/next/pages/index.mjs +9 -14
- package/dist/adapters/remix/index.d.ts.map +1 -1
- package/dist/adapters/remix/index.js +9 -14
- package/dist/adapters/remix/index.mjs +10 -15
- package/dist/adapters/shared.d.ts +8 -0
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +36 -7
- package/dist/adapters/shared.mjs +36 -8
- package/dist/adapters/start/index.d.ts.map +1 -1
- package/dist/adapters/start/index.js +9 -15
- package/dist/adapters/start/index.mjs +10 -16
- package/dist/core/client/index.d.ts +112 -69
- package/dist/core/client/index.d.ts.map +1 -1
- package/dist/core/client/index.js +52 -4
- package/dist/core/client/index.mjs +52 -4
- package/dist/core/sdk/index.d.ts +44 -2
- package/dist/core/sdk/index.d.ts.map +1 -1
- package/dist/core/sdk/index.js +36 -6
- package/dist/core/sdk/index.mjs +36 -6
- package/dist/providers/aws/index.d.ts +1 -1
- package/dist/providers/aws/index.d.ts.map +1 -1
- package/dist/providers/aws/index.js +3 -3
- package/dist/providers/aws/index.mjs +3 -3
- package/dist/providers/azure/index.d.ts.map +1 -1
- package/dist/providers/azure/index.js +33 -7
- package/dist/providers/azure/index.mjs +33 -7
- package/dist/providers/edgestore/index.d.ts.map +1 -1
- package/dist/providers/edgestore/index.js +24 -4
- package/dist/providers/edgestore/index.mjs +24 -4
- package/dist/test-utils/edgestoreSmoke.d.ts +32 -0
- package/dist/test-utils/edgestoreSmoke.d.ts.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +7 -0
- package/dist/version.mjs +4 -0
- package/package.json +11 -2
- package/src/adapters/astro/index.ts +11 -21
- package/src/adapters/express/index.ts +10 -16
- package/src/adapters/fastify/index.ts +13 -19
- package/src/adapters/hono/index.ts +19 -23
- package/src/adapters/next/app/index.ts +12 -22
- package/src/adapters/next/pages/index.ts +11 -16
- package/src/adapters/remix/index.ts +11 -12
- package/src/adapters/shared.ts +49 -4
- package/src/adapters/start/index.ts +11 -12
- package/src/core/client/index.ts +199 -74
- package/src/core/sdk/index.ts +75 -0
- package/src/providers/aws/index.ts +4 -4
- package/src/providers/azure/index.ts +40 -10
- package/src/providers/edgestore/index.ts +29 -0
- package/src/test-utils/edgestoreSmoke.ts +141 -0
- package/src/version.ts +2 -0
package/src/core/client/index.ts
CHANGED
|
@@ -4,7 +4,9 @@ import {
|
|
|
4
4
|
type AnyRouter,
|
|
5
5
|
type InferBucketPathKeys,
|
|
6
6
|
type InferBucketPathObject,
|
|
7
|
+
type InferBucketPathOrder,
|
|
7
8
|
type InferMetadataObject,
|
|
9
|
+
type MaybePromise,
|
|
8
10
|
type Prettify,
|
|
9
11
|
type Simplify,
|
|
10
12
|
} from '@edgestore/shared';
|
|
@@ -49,6 +51,14 @@ export type UploadOptions = {
|
|
|
49
51
|
* This avoids unnecessary zombie files in the bucket.
|
|
50
52
|
*/
|
|
51
53
|
temporary?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Transform the file before it is validated and uploaded.
|
|
56
|
+
*
|
|
57
|
+
* This can be used to compress images, convert formats, encrypt files, etc.
|
|
58
|
+
* The transformed blob's size, MIME type, and extension will be used for the
|
|
59
|
+
* upload request.
|
|
60
|
+
*/
|
|
61
|
+
transform?: ServerUploadTransform;
|
|
52
62
|
};
|
|
53
63
|
|
|
54
64
|
type TextContent = string;
|
|
@@ -61,6 +71,17 @@ type UrlContent = {
|
|
|
61
71
|
extension: string;
|
|
62
72
|
};
|
|
63
73
|
|
|
74
|
+
export type UploadContent = TextContent | BlobContent | UrlContent;
|
|
75
|
+
|
|
76
|
+
export type ServerUploadTransform = (params: {
|
|
77
|
+
blob: Blob;
|
|
78
|
+
extension: string;
|
|
79
|
+
type: string;
|
|
80
|
+
}) => MaybePromise<{
|
|
81
|
+
blob: Blob;
|
|
82
|
+
extension: string;
|
|
83
|
+
}>;
|
|
84
|
+
|
|
64
85
|
// type guard for `content`
|
|
65
86
|
function isTextContent(
|
|
66
87
|
content: TextContent | BlobContent | UrlContent,
|
|
@@ -98,7 +119,7 @@ export type UploadFileRequest<TBucket extends AnyBuilder> = {
|
|
|
98
119
|
* extension: "csv",
|
|
99
120
|
* }
|
|
100
121
|
*/
|
|
101
|
-
content:
|
|
122
|
+
content: UploadContent;
|
|
102
123
|
options?: UploadOptions;
|
|
103
124
|
} & (TBucket['$config']['ctx'] extends Record<string, never>
|
|
104
125
|
? {}
|
|
@@ -111,23 +132,37 @@ export type UploadFileRequest<TBucket extends AnyBuilder> = {
|
|
|
111
132
|
input: z.infer<TBucket['_def']['input']>;
|
|
112
133
|
});
|
|
113
134
|
|
|
135
|
+
type UploadImplementationParams = {
|
|
136
|
+
content: UploadContent;
|
|
137
|
+
ctx?: Record<string, unknown>;
|
|
138
|
+
input?: Record<string, unknown>;
|
|
139
|
+
};
|
|
140
|
+
|
|
114
141
|
export type UploadFileRes<TBucket extends AnyBuilder> =
|
|
115
|
-
TBucket['_def']['type'] extends 'IMAGE'
|
|
142
|
+
(TBucket['_def']['type'] extends 'IMAGE'
|
|
116
143
|
? {
|
|
117
144
|
url: string;
|
|
118
145
|
thumbnailUrl: string | null;
|
|
119
146
|
size: number;
|
|
120
147
|
metadata: InferMetadataObject<TBucket>;
|
|
121
148
|
path: InferBucketPathObject<TBucket>;
|
|
122
|
-
pathOrder:
|
|
149
|
+
pathOrder: InferBucketPathOrder<TBucket>;
|
|
123
150
|
}
|
|
124
151
|
: {
|
|
125
152
|
url: string;
|
|
126
153
|
size: number;
|
|
127
154
|
metadata: InferMetadataObject<TBucket>;
|
|
128
155
|
path: InferBucketPathObject<TBucket>;
|
|
129
|
-
pathOrder:
|
|
130
|
-
}
|
|
156
|
+
pathOrder: InferBucketPathOrder<TBucket>;
|
|
157
|
+
}) &
|
|
158
|
+
(undefined extends TBucket['_def']['autoSignedUrls']
|
|
159
|
+
? unknown
|
|
160
|
+
: {
|
|
161
|
+
signedUrl: string;
|
|
162
|
+
expiresAt: Date;
|
|
163
|
+
expiresIn: number;
|
|
164
|
+
signedThumbnailUrl?: string | null;
|
|
165
|
+
});
|
|
131
166
|
|
|
132
167
|
type Filter<TBucket extends AnyBuilder> = {
|
|
133
168
|
AND?: Filter<TBucket>[];
|
|
@@ -173,69 +208,96 @@ export type ListFilesResponse<TBucket extends AnyBuilder> = {
|
|
|
173
208
|
};
|
|
174
209
|
};
|
|
175
210
|
|
|
211
|
+
type GetSignedUrlRes = {
|
|
212
|
+
url: string;
|
|
213
|
+
signedUrl: string;
|
|
214
|
+
expiresAt: Date;
|
|
215
|
+
expiresIn: number;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
type GetSignedUrlsRes<TBucket extends AnyBuilder> =
|
|
219
|
+
TBucket['_def']['type'] extends 'IMAGE'
|
|
220
|
+
? (GetSignedUrlRes & {
|
|
221
|
+
thumbnailUrl?: string | null;
|
|
222
|
+
signedThumbnailUrl?: string | null;
|
|
223
|
+
})[]
|
|
224
|
+
: GetSignedUrlRes[];
|
|
225
|
+
|
|
226
|
+
type BucketClient<TBucket extends AnyBuilder> = {
|
|
227
|
+
getFile: (params: { url: string }) => Promise<Prettify<GetFileRes<TBucket>>>;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Use this function to upload a file to the bucket directly from your backend.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* // simple example
|
|
235
|
+
* await backendClient.myBucket.upload({
|
|
236
|
+
* content: "some text",
|
|
237
|
+
* });
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* // complete example
|
|
243
|
+
* await backendClient.myBucket.upload({
|
|
244
|
+
* content: {
|
|
245
|
+
* blob: new Blob([text], { type: "text/csv" }),
|
|
246
|
+
* extension: "csv",
|
|
247
|
+
* },
|
|
248
|
+
* options: {
|
|
249
|
+
* temporary: true,
|
|
250
|
+
* replaceTargetUrl: replaceUrl,
|
|
251
|
+
* manualFileName: "test.csv",
|
|
252
|
+
* },
|
|
253
|
+
* ctx: {
|
|
254
|
+
* userId: "123",
|
|
255
|
+
* userRole: "admin",
|
|
256
|
+
* },
|
|
257
|
+
* input: {
|
|
258
|
+
* type: "post",
|
|
259
|
+
* },
|
|
260
|
+
* });
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
upload: (
|
|
264
|
+
params: Prettify<UploadFileRequest<TBucket>>,
|
|
265
|
+
) => Promise<Prettify<UploadFileRes<TBucket>>>;
|
|
266
|
+
/**
|
|
267
|
+
* Confirm a temporary file upload directly from your backend.
|
|
268
|
+
*/
|
|
269
|
+
confirmUpload: (params: { url: string }) => Promise<{ success: boolean }>;
|
|
270
|
+
/**
|
|
271
|
+
* Programmatically delete a file directly from your backend.
|
|
272
|
+
*/
|
|
273
|
+
deleteFile: (params: { url: string }) => Promise<{
|
|
274
|
+
success: boolean;
|
|
275
|
+
}>;
|
|
276
|
+
/**
|
|
277
|
+
* List files in a bucket.
|
|
278
|
+
*
|
|
279
|
+
* You can also filter the results by passing a filter object.
|
|
280
|
+
* The results are paginated.
|
|
281
|
+
*/
|
|
282
|
+
listFiles: (
|
|
283
|
+
params?: ListFilesRequest<TBucket>,
|
|
284
|
+
) => Promise<Prettify<ListFilesResponse<TBucket>>>;
|
|
285
|
+
} & (undefined extends TBucket['_def']['accessControl']
|
|
286
|
+
? unknown
|
|
287
|
+
: {
|
|
288
|
+
getSignedUrl: (params: {
|
|
289
|
+
url: string;
|
|
290
|
+
expiresIn?: number;
|
|
291
|
+
}) => Promise<Prettify<GetSignedUrlRes>>;
|
|
292
|
+
getSignedUrls: (params: {
|
|
293
|
+
urls: string[];
|
|
294
|
+
expiresIn?: number;
|
|
295
|
+
includeThumbnails?: boolean;
|
|
296
|
+
}) => Promise<Prettify<GetSignedUrlsRes<TBucket>[number]>[]>;
|
|
297
|
+
});
|
|
298
|
+
|
|
176
299
|
type EdgeStoreClient<TRouter extends AnyRouter> = {
|
|
177
|
-
[K in keyof TRouter['buckets']]:
|
|
178
|
-
getFile: (params: {
|
|
179
|
-
url: string;
|
|
180
|
-
}) => Promise<GetFileRes<TRouter['buckets'][K]>>;
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Use this function to upload a file to the bucket directly from your backend.
|
|
184
|
-
*
|
|
185
|
-
* @example
|
|
186
|
-
* ```ts
|
|
187
|
-
* // simple example
|
|
188
|
-
* await backendClient.myBucket.upload({
|
|
189
|
-
* content: "some text",
|
|
190
|
-
* });
|
|
191
|
-
* ```
|
|
192
|
-
*
|
|
193
|
-
* @example
|
|
194
|
-
* ```ts
|
|
195
|
-
* // complete example
|
|
196
|
-
* await backendClient.myBucket.upload({
|
|
197
|
-
* content: {
|
|
198
|
-
* blob: new Blob([text], { type: "text/csv" }),
|
|
199
|
-
* extension: "csv",
|
|
200
|
-
* },
|
|
201
|
-
* options: {
|
|
202
|
-
* temporary: true,
|
|
203
|
-
* replaceTargetUrl: replaceUrl,
|
|
204
|
-
* manualFileName: "test.csv",
|
|
205
|
-
* },
|
|
206
|
-
* ctx: {
|
|
207
|
-
* userId: "123",
|
|
208
|
-
* userRole: "admin",
|
|
209
|
-
* },
|
|
210
|
-
* input: {
|
|
211
|
-
* type: "post",
|
|
212
|
-
* },
|
|
213
|
-
* });
|
|
214
|
-
* ```
|
|
215
|
-
*/
|
|
216
|
-
upload: (
|
|
217
|
-
params: UploadFileRequest<TRouter['buckets'][K]>,
|
|
218
|
-
) => Promise<Prettify<UploadFileRes<TRouter['buckets'][K]>>>;
|
|
219
|
-
/**
|
|
220
|
-
* Confirm a temporary file upload directly from your backend.
|
|
221
|
-
*/
|
|
222
|
-
confirmUpload: (params: { url: string }) => Promise<{ success: boolean }>;
|
|
223
|
-
/**
|
|
224
|
-
* Programmatically delete a file directly from your backend.
|
|
225
|
-
*/
|
|
226
|
-
deleteFile: (params: { url: string }) => Promise<{
|
|
227
|
-
success: boolean;
|
|
228
|
-
}>;
|
|
229
|
-
/**
|
|
230
|
-
* List files in a bucket.
|
|
231
|
-
*
|
|
232
|
-
* You can also filter the results by passing a filter object.
|
|
233
|
-
* The results are paginated.
|
|
234
|
-
*/
|
|
235
|
-
listFiles: (
|
|
236
|
-
params?: ListFilesRequest<TRouter['buckets'][K]>,
|
|
237
|
-
) => Promise<Prettify<ListFilesResponse<TRouter['buckets'][K]>>>;
|
|
238
|
-
};
|
|
300
|
+
[K in keyof TRouter['buckets']]: BucketClient<TRouter['buckets'][K]>;
|
|
239
301
|
};
|
|
240
302
|
|
|
241
303
|
export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
@@ -264,11 +326,13 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
264
326
|
}
|
|
265
327
|
const client: EdgeStoreClient<TRouter>[string] = {
|
|
266
328
|
async upload(params) {
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
329
|
+
const {
|
|
330
|
+
content,
|
|
331
|
+
ctx = {},
|
|
332
|
+
input = {},
|
|
333
|
+
}: UploadImplementationParams = params;
|
|
270
334
|
|
|
271
|
-
|
|
335
|
+
let { blob, extension } = await (async () => {
|
|
272
336
|
if (isTextContent(content)) {
|
|
273
337
|
return {
|
|
274
338
|
blob: new Blob([content], { type: 'text/plain' }),
|
|
@@ -287,6 +351,16 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
287
351
|
}
|
|
288
352
|
})();
|
|
289
353
|
|
|
354
|
+
if (params.options?.transform) {
|
|
355
|
+
const transformed = await params.options.transform({
|
|
356
|
+
blob,
|
|
357
|
+
extension,
|
|
358
|
+
type: blob.type,
|
|
359
|
+
});
|
|
360
|
+
blob = transformed.blob;
|
|
361
|
+
extension = transformed.extension;
|
|
362
|
+
}
|
|
363
|
+
|
|
290
364
|
const path = buildPath({
|
|
291
365
|
bucket,
|
|
292
366
|
pathAttrs: {
|
|
@@ -310,6 +384,7 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
310
384
|
const requestUploadRes = await sdk.requestUpload({
|
|
311
385
|
bucketName,
|
|
312
386
|
bucketType: bucket._def.type,
|
|
387
|
+
autoSignedUrls: bucket._def.autoSignedUrls,
|
|
313
388
|
fileInfo: {
|
|
314
389
|
fileName: params.options?.manualFileName,
|
|
315
390
|
replaceTargetUrl: params.options?.replaceTargetUrl,
|
|
@@ -348,13 +423,12 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
348
423
|
...{
|
|
349
424
|
thumbnailUrl: requestUploadRes.thumbnailUrl,
|
|
350
425
|
},
|
|
426
|
+
...mapSignedUploadAccess(requestUploadRes),
|
|
351
427
|
size: blob.size,
|
|
352
428
|
metadata,
|
|
353
429
|
path: parsedPath,
|
|
354
430
|
pathOrder,
|
|
355
|
-
}
|
|
356
|
-
TRouter['buckets'][string]
|
|
357
|
-
>;
|
|
431
|
+
} as unknown as UploadFileRes<TRouter['buckets'][string]>;
|
|
358
432
|
},
|
|
359
433
|
|
|
360
434
|
async getFile(params) {
|
|
@@ -402,6 +476,38 @@ export function initEdgeStoreClient<TRouter extends AnyRouter>(config: {
|
|
|
402
476
|
pagination: res.pagination,
|
|
403
477
|
};
|
|
404
478
|
},
|
|
479
|
+
|
|
480
|
+
async getSignedUrl(params: { url: string; expiresIn?: number }) {
|
|
481
|
+
const [signedUrl] = await sdk.getSignedUrls({
|
|
482
|
+
bucketName,
|
|
483
|
+
urls: [params.url],
|
|
484
|
+
expiresIn: params.expiresIn,
|
|
485
|
+
});
|
|
486
|
+
if (!signedUrl) {
|
|
487
|
+
throw new Error('Missing signed URL response');
|
|
488
|
+
}
|
|
489
|
+
return {
|
|
490
|
+
...signedUrl,
|
|
491
|
+
expiresAt: new Date(signedUrl.expiresAt),
|
|
492
|
+
};
|
|
493
|
+
},
|
|
494
|
+
|
|
495
|
+
async getSignedUrls(params: {
|
|
496
|
+
urls: string[];
|
|
497
|
+
expiresIn?: number;
|
|
498
|
+
includeThumbnails?: boolean;
|
|
499
|
+
}) {
|
|
500
|
+
const signedUrls = await sdk.getSignedUrls({
|
|
501
|
+
bucketName,
|
|
502
|
+
urls: params.urls,
|
|
503
|
+
expiresIn: params.expiresIn,
|
|
504
|
+
includeThumbnails: params.includeThumbnails,
|
|
505
|
+
});
|
|
506
|
+
return signedUrls.map((signedUrl) => ({
|
|
507
|
+
...signedUrl,
|
|
508
|
+
expiresAt: new Date(signedUrl.expiresAt),
|
|
509
|
+
})) as any;
|
|
510
|
+
},
|
|
405
511
|
};
|
|
406
512
|
return client;
|
|
407
513
|
},
|
|
@@ -435,6 +541,25 @@ async function getBlobFromUrl(url: string) {
|
|
|
435
541
|
return await res.blob();
|
|
436
542
|
}
|
|
437
543
|
|
|
544
|
+
function mapSignedUploadAccess(res: {
|
|
545
|
+
accessSignedUrl?: string;
|
|
546
|
+
accessSignedThumbnailUrl?: string | null;
|
|
547
|
+
accessSignedUrlExpiresAt?: Date | string;
|
|
548
|
+
accessSignedUrlExpiresIn?: number;
|
|
549
|
+
}) {
|
|
550
|
+
if (!res.accessSignedUrl) {
|
|
551
|
+
return {};
|
|
552
|
+
}
|
|
553
|
+
return {
|
|
554
|
+
signedUrl: res.accessSignedUrl,
|
|
555
|
+
expiresAt: res.accessSignedUrlExpiresAt
|
|
556
|
+
? new Date(res.accessSignedUrlExpiresAt)
|
|
557
|
+
: new Date(),
|
|
558
|
+
expiresIn: res.accessSignedUrlExpiresIn ?? 0,
|
|
559
|
+
signedThumbnailUrl: res.accessSignedThumbnailUrl ?? null,
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
|
|
438
563
|
export type InferClientResponse<TRouter extends AnyRouter> = {
|
|
439
564
|
[TBucketName in keyof TRouter['buckets']]: {
|
|
440
565
|
[TClienFn in keyof EdgeStoreClient<TRouter>[TBucketName]]: Simplify<
|
package/src/core/sdk/index.ts
CHANGED
|
@@ -6,6 +6,10 @@ import {
|
|
|
6
6
|
} from '@edgestore/shared';
|
|
7
7
|
import { getEnv } from '../../adapters/shared';
|
|
8
8
|
import EdgeStoreCredentialsError from '../../libs/errors/EdgeStoreCredentialsError';
|
|
9
|
+
import {
|
|
10
|
+
EDGE_STORE_PACKAGE_NAME,
|
|
11
|
+
EDGE_STORE_PACKAGE_VERSION,
|
|
12
|
+
} from '../../version';
|
|
9
13
|
|
|
10
14
|
const API_ENDPOINT =
|
|
11
15
|
getEnv('EDGE_STORE_API_ENDPOINT') ?? 'https://api.edgestore.dev';
|
|
@@ -25,6 +29,11 @@ type FileInfoForUpload = {
|
|
|
25
29
|
temporary: boolean;
|
|
26
30
|
};
|
|
27
31
|
|
|
32
|
+
type AutoSignedUrlsForUpload = {
|
|
33
|
+
expiresIn?: number;
|
|
34
|
+
includeThumbnails?: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
28
37
|
export type SimpleOperator =
|
|
29
38
|
| 'eq'
|
|
30
39
|
| 'neq'
|
|
@@ -77,6 +86,8 @@ async function makeRequest<TOutput>(params: {
|
|
|
77
86
|
Authorization: `Basic ${Buffer.from(`${accessKey}:${secretKey}`).toString(
|
|
78
87
|
'base64',
|
|
79
88
|
)}`,
|
|
89
|
+
'x-edgestore-package-name': EDGE_STORE_PACKAGE_NAME,
|
|
90
|
+
'x-edgestore-package-version': EDGE_STORE_PACKAGE_VERSION,
|
|
80
91
|
},
|
|
81
92
|
});
|
|
82
93
|
if (!res.ok) {
|
|
@@ -161,12 +172,14 @@ export const edgeStoreRawSdk = {
|
|
|
161
172
|
bucketType,
|
|
162
173
|
fileInfo,
|
|
163
174
|
multipart,
|
|
175
|
+
autoSignedUrls,
|
|
164
176
|
}: {
|
|
165
177
|
accessKey: string;
|
|
166
178
|
secretKey: string;
|
|
167
179
|
bucketName: string;
|
|
168
180
|
bucketType: string;
|
|
169
181
|
fileInfo: FileInfoForUpload;
|
|
182
|
+
autoSignedUrls?: AutoSignedUrlsForUpload;
|
|
170
183
|
multipart?: {
|
|
171
184
|
parts: number[];
|
|
172
185
|
};
|
|
@@ -184,6 +197,10 @@ export const edgeStoreRawSdk = {
|
|
|
184
197
|
url: string;
|
|
185
198
|
path: string;
|
|
186
199
|
thumbnailUrl: string | null;
|
|
200
|
+
accessSignedUrl?: string;
|
|
201
|
+
accessSignedThumbnailUrl?: string | null;
|
|
202
|
+
accessSignedUrlExpiresAt?: string;
|
|
203
|
+
accessSignedUrlExpiresIn?: number;
|
|
187
204
|
}>({
|
|
188
205
|
path: '/request-upload',
|
|
189
206
|
accessKey,
|
|
@@ -201,6 +218,7 @@ export const edgeStoreRawSdk = {
|
|
|
201
218
|
fileName: fileInfo.fileName,
|
|
202
219
|
replaceTargetUrl: fileInfo.replaceTargetUrl,
|
|
203
220
|
isTemporary: fileInfo.temporary,
|
|
221
|
+
autoSignedUrls,
|
|
204
222
|
},
|
|
205
223
|
});
|
|
206
224
|
return {
|
|
@@ -209,9 +227,51 @@ export const edgeStoreRawSdk = {
|
|
|
209
227
|
accessUrl: res.url,
|
|
210
228
|
path: res.path,
|
|
211
229
|
thumbnailUrl: res.thumbnailUrl,
|
|
230
|
+
accessSignedUrl: res.accessSignedUrl,
|
|
231
|
+
accessSignedThumbnailUrl: res.accessSignedThumbnailUrl,
|
|
232
|
+
accessSignedUrlExpiresAt: res.accessSignedUrlExpiresAt,
|
|
233
|
+
accessSignedUrlExpiresIn: res.accessSignedUrlExpiresIn,
|
|
212
234
|
};
|
|
213
235
|
},
|
|
214
236
|
|
|
237
|
+
async getSignedUrls({
|
|
238
|
+
accessKey,
|
|
239
|
+
secretKey,
|
|
240
|
+
bucketName,
|
|
241
|
+
urls,
|
|
242
|
+
expiresIn,
|
|
243
|
+
includeThumbnails,
|
|
244
|
+
}: {
|
|
245
|
+
accessKey: string;
|
|
246
|
+
secretKey: string;
|
|
247
|
+
bucketName: string;
|
|
248
|
+
urls: string[];
|
|
249
|
+
expiresIn?: number;
|
|
250
|
+
includeThumbnails?: boolean;
|
|
251
|
+
}) {
|
|
252
|
+
const res = await makeRequest<{
|
|
253
|
+
signedUrls: {
|
|
254
|
+
url: string;
|
|
255
|
+
signedUrl: string;
|
|
256
|
+
expiresAt: string;
|
|
257
|
+
expiresIn: number;
|
|
258
|
+
thumbnailUrl?: string | null;
|
|
259
|
+
signedThumbnailUrl?: string | null;
|
|
260
|
+
}[];
|
|
261
|
+
}>({
|
|
262
|
+
path: '/get-signed-urls',
|
|
263
|
+
accessKey,
|
|
264
|
+
secretKey,
|
|
265
|
+
body: {
|
|
266
|
+
bucketName,
|
|
267
|
+
urls,
|
|
268
|
+
expiresIn,
|
|
269
|
+
includeThumbnails,
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
return res.signedUrls;
|
|
273
|
+
},
|
|
274
|
+
|
|
215
275
|
async requestUploadParts({
|
|
216
276
|
accessKey,
|
|
217
277
|
secretKey,
|
|
@@ -389,10 +449,12 @@ export function initEdgeStoreSdk(params: {
|
|
|
389
449
|
bucketType,
|
|
390
450
|
fileInfo,
|
|
391
451
|
multipart,
|
|
452
|
+
autoSignedUrls,
|
|
392
453
|
}: {
|
|
393
454
|
bucketName: string;
|
|
394
455
|
bucketType: string;
|
|
395
456
|
fileInfo: FileInfoForUpload;
|
|
457
|
+
autoSignedUrls?: AutoSignedUrlsForUpload;
|
|
396
458
|
multipart?: {
|
|
397
459
|
parts: number[];
|
|
398
460
|
};
|
|
@@ -404,6 +466,19 @@ export function initEdgeStoreSdk(params: {
|
|
|
404
466
|
bucketType,
|
|
405
467
|
fileInfo,
|
|
406
468
|
multipart,
|
|
469
|
+
autoSignedUrls,
|
|
470
|
+
});
|
|
471
|
+
},
|
|
472
|
+
async getSignedUrls(params: {
|
|
473
|
+
bucketName: string;
|
|
474
|
+
urls: string[];
|
|
475
|
+
expiresIn?: number;
|
|
476
|
+
includeThumbnails?: boolean;
|
|
477
|
+
}) {
|
|
478
|
+
return await edgeStoreRawSdk.getSignedUrls({
|
|
479
|
+
accessKey,
|
|
480
|
+
secretKey,
|
|
481
|
+
...params,
|
|
407
482
|
});
|
|
408
483
|
},
|
|
409
484
|
async requestUploadParts({
|
|
@@ -7,8 +7,8 @@ import {
|
|
|
7
7
|
} from '@aws-sdk/client-s3';
|
|
8
8
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
9
9
|
import {
|
|
10
|
-
type MaybePromise,
|
|
11
10
|
type Provider as EdgeStoreProvider,
|
|
11
|
+
type MaybePromise,
|
|
12
12
|
type RequestUploadParams,
|
|
13
13
|
} from '@edgestore/shared';
|
|
14
14
|
import { v4 as uuidv4 } from 'uuid';
|
|
@@ -175,12 +175,11 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
175
175
|
const pathPrefix = `${esBucketName}${
|
|
176
176
|
fileInfo.isPublic ? '/_public' : ''
|
|
177
177
|
}`;
|
|
178
|
-
const nameId = uuidv4();
|
|
179
178
|
const extension = fileInfo.extension
|
|
180
179
|
? `.${fileInfo.extension.replace('.', '')}`
|
|
181
180
|
: '';
|
|
182
181
|
const defaultResolvedFileName =
|
|
183
|
-
fileInfo.fileName ?? `${
|
|
182
|
+
fileInfo.fileName ?? `${uuidv4()}${extension}`;
|
|
184
183
|
const defaultFilePathFromMetadata = fileInfo.path.reduce((acc, item) => {
|
|
185
184
|
return `${acc}/${item.value}`;
|
|
186
185
|
}, '');
|
|
@@ -194,6 +193,7 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
194
193
|
defaultAccessPath: accessPath,
|
|
195
194
|
});
|
|
196
195
|
}
|
|
196
|
+
accessPath = accessPath.replace(/^\/+/, '');
|
|
197
197
|
|
|
198
198
|
const command = new PutObjectCommand({
|
|
199
199
|
Bucket: bucketName,
|
|
@@ -204,7 +204,7 @@ export function AWSProvider(options?: AWSProviderOptions): EdgeStoreProvider {
|
|
|
204
204
|
expiresIn: 60 * 60, // 1 hour
|
|
205
205
|
});
|
|
206
206
|
|
|
207
|
-
const finalAccessUrl = `${baseUrl}/${accessPath
|
|
207
|
+
const finalAccessUrl = `${baseUrl}/${accessPath}`;
|
|
208
208
|
return {
|
|
209
209
|
uploadUrl: signedUrl,
|
|
210
210
|
accessUrl: finalAccessUrl,
|
|
@@ -54,6 +54,42 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
54
54
|
const containerClient = blobServiceClient.getContainerClient(
|
|
55
55
|
containerName ?? '',
|
|
56
56
|
);
|
|
57
|
+
function getBlobNameFromUrl(url: string) {
|
|
58
|
+
try {
|
|
59
|
+
const blobUrl = new URL(url);
|
|
60
|
+
const containerUrl = new URL(containerClient.url);
|
|
61
|
+
const containerPath = containerUrl.pathname.replace(/\/$/, '');
|
|
62
|
+
|
|
63
|
+
if (blobUrl.origin !== containerUrl.origin) {
|
|
64
|
+
return url;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!blobUrl.pathname.startsWith(`${containerPath}/`)) {
|
|
68
|
+
return url;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return decodeURIComponent(
|
|
72
|
+
blobUrl.pathname.slice(containerPath.length + 1),
|
|
73
|
+
);
|
|
74
|
+
} catch {
|
|
75
|
+
return url;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getBlobName(params: Parameters<Provider['requestUpload']>[0]) {
|
|
80
|
+
const { bucketName: esBucketName, fileInfo } = params;
|
|
81
|
+
const extension = fileInfo.extension
|
|
82
|
+
? `.${fileInfo.extension.replace('.', '')}`
|
|
83
|
+
: '';
|
|
84
|
+
const fileName = fileInfo.fileName ?? `${uuidv4()}${extension}`;
|
|
85
|
+
|
|
86
|
+
return [
|
|
87
|
+
esBucketName,
|
|
88
|
+
...(fileInfo.isPublic ? ['_public'] : []),
|
|
89
|
+
...fileInfo.path.map((item) => item.value),
|
|
90
|
+
fileName,
|
|
91
|
+
].join('/');
|
|
92
|
+
}
|
|
57
93
|
|
|
58
94
|
return {
|
|
59
95
|
name: 'azure',
|
|
@@ -64,7 +100,7 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
64
100
|
return baseUrl;
|
|
65
101
|
},
|
|
66
102
|
async getFile({ url }) {
|
|
67
|
-
const blobClient = containerClient.getBlobClient(url);
|
|
103
|
+
const blobClient = containerClient.getBlobClient(getBlobNameFromUrl(url));
|
|
68
104
|
|
|
69
105
|
const { contentLength, lastModified } = await blobClient.getProperties();
|
|
70
106
|
|
|
@@ -76,14 +112,8 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
76
112
|
uploadedAt: lastModified ?? new Date(),
|
|
77
113
|
};
|
|
78
114
|
},
|
|
79
|
-
async requestUpload(
|
|
80
|
-
const
|
|
81
|
-
const extension = fileInfo.extension
|
|
82
|
-
? `.${fileInfo.extension.replace('.', '')}`
|
|
83
|
-
: '';
|
|
84
|
-
const fileName = fileInfo.fileName ?? `${nameId}${extension}`;
|
|
85
|
-
|
|
86
|
-
const blobClient = containerClient.getBlobClient(fileName);
|
|
115
|
+
async requestUpload(params) {
|
|
116
|
+
const blobClient = containerClient.getBlobClient(getBlobName(params));
|
|
87
117
|
|
|
88
118
|
const url = blobClient.url;
|
|
89
119
|
|
|
@@ -102,7 +132,7 @@ export function AzureProvider(options?: AzureProviderOptions): Provider {
|
|
|
102
132
|
throw new Error('Not implemented');
|
|
103
133
|
},
|
|
104
134
|
async deleteFile({ url }) {
|
|
105
|
-
const blobClient = containerClient.getBlobClient(url);
|
|
135
|
+
const blobClient = containerClient.getBlobClient(getBlobNameFromUrl(url));
|
|
106
136
|
await blobClient.delete();
|
|
107
137
|
return {
|
|
108
138
|
success: true,
|