|
1
|
import { FileTypes, IFileProvider } from "@medusajs/types" |
|
2
|
import type { Readable, Writable } from "stream" |
|
3
|
|
|
4
|
/** |
|
5
|
* ### constructor |
|
6
|
* |
|
7
|
* The constructor allows you to access resources from the module's container using the first parameter, |
|
8
|
* and the module's options using the second parameter. |
|
9
|
* |
|
10
|
* If you're creating a client or establishing a connection with a third-party service, do it in the constructor. |
|
11
|
* |
|
12
|
* #### Example |
|
13
|
* |
|
14
|
* ```ts |
|
15
|
* import { Logger } from "@medusajs/framework/types" |
|
16
|
* import { AbstractFileProviderService } from "@medusajs/framework/utils" |
|
17
|
* |
|
18
|
* type InjectedDependencies = { |
|
19
|
* logger: Logger |
|
20
|
* } |
|
21
|
* |
|
22
|
* type Options = { |
|
23
|
* apiKey: string |
|
24
|
* } |
|
25
|
* |
|
26
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
27
|
* protected logger_: Logger |
|
28
|
* protected options_: Options |
|
29
|
* static identifier = "my-file" |
|
30
|
* // assuming you're initializing a client |
|
31
|
* protected client |
|
32
|
* |
|
33
|
* constructor ( |
|
34
|
* { logger }: InjectedDependencies, |
|
35
|
* options: Options |
|
36
|
* ) { |
|
37
|
* super() |
|
38
|
* |
|
39
|
* this.logger_ = logger |
|
40
|
* this.options_ = options |
|
41
|
* |
|
42
|
* // assuming you're initializing a client |
|
43
|
* this.client = new Client(options) |
|
44
|
* } |
|
45
|
* } |
|
46
|
* |
|
47
|
* export default MyFileProviderService |
|
48
|
* ``` |
|
49
|
*/ |
|
50
|
export class AbstractFileProviderService implements IFileProvider { |
|
51
|
/** |
|
52
|
* Each file provider has a unique ID used to identify it. The provider's ID |
|
53
|
* will be stored as `fs_{identifier}_{id}`, where `{id}` is the provider's `id` |
|
54
|
* property in the `medusa-config.ts`. |
|
55
|
* |
|
56
|
* @example |
|
57
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
58
|
* static identifier = "my-file" |
|
59
|
* // ... |
|
60
|
* } |
|
61
|
*/ |
|
62
|
static identifier: string |
|
63
|
|
|
64
|
/** |
|
65
|
* This method validates the options of the provider set in `medusa-config.ts`. |
|
66
|
* Implementing this method is optional. It's useful if your provider requires custom validation. |
|
67
|
* |
|
68
|
* If the options aren't valid, throw an error. |
|
69
|
* |
|
70
|
* @param options - The provider's options. |
|
71
|
* |
|
72
|
* @example |
|
73
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
74
|
* static validateOptions(options: Record<any, any>) { |
|
75
|
* if (!options.apiKey) { |
|
76
|
* throw new MedusaError( |
|
77
|
* MedusaError.Types.INVALID_DATA, |
|
78
|
* "API key is required in the provider's options." |
|
79
|
* ) |
|
80
|
* } |
|
81
|
* } |
|
82
|
* } |
|
83
|
*/ |
|
84
|
static validateOptions(options: Record<any, any>): void | never {} |
|
85
|
|
|
86
|
/** |
|
87
|
* @ignore |
|
88
|
*/ |
|
89
|
getIdentifier() { |
|
90
|
return (this.constructor as any).identifier |
|
91
|
} |
|
92
|
|
|
93
|
/** |
|
94
|
* This method uploads a file using your provider's custom logic. In this method, you can upload the file |
|
95
|
* into your provider's storage, and return the uploaded file's details. |
|
96
|
* |
|
97
|
* This method will be used when uploading product images, CSV files for imports, or other |
|
98
|
* custom file uploads. |
|
99
|
* |
|
100
|
* @param {FileTypes.ProviderUploadFileDTO} file - The file to upload. |
|
101
|
* @returns {Promise<FileTypes.ProviderFileResultDTO>} The uploaded file's details. |
|
102
|
* |
|
103
|
* @example |
|
104
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
105
|
* // ... |
|
106
|
* async upload( |
|
107
|
* file: ProviderUploadFileDTO |
|
108
|
* ): Promise<ProviderFileResultDTO> { |
|
109
|
* // TODO upload file to third-party provider |
|
110
|
* // or using custom logic |
|
111
|
* // for example: |
|
112
|
* this.client.upload(file) |
|
113
|
* |
|
114
|
* return { |
|
115
|
* url: "some-url.com", |
|
116
|
* key: "file-name-or-id" |
|
117
|
* } |
|
118
|
* } |
|
119
|
* } |
|
120
|
*/ |
|
121
|
async upload( |
|
122
|
file: FileTypes.ProviderUploadFileDTO |
|
123
|
): Promise<FileTypes.ProviderFileResultDTO> { |
|
124
|
throw Error("upload must be overridden by the child class") |
|
125
|
} |
|
126
|
|
|
127
|
/** |
|
128
|
* This method deletes one or more files from the storage. It's used when an admin user |
|
129
|
* deletes a product image, or other custom file deletions. |
|
130
|
* |
|
131
|
* @param {FileTypes.ProviderDeleteFileDTO | FileTypes.ProviderDeleteFileDTO[]} files - The details of the files to delete. |
|
132
|
* @returns {Promise<void>} Resolves when the files are deleted. |
|
133
|
* |
|
134
|
* @example |
|
135
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
136
|
* // ... |
|
137
|
* async delete( |
|
138
|
* files: FileTypes.ProviderDeleteFileDTO | FileTypes.ProviderDeleteFileDTO[] |
|
139
|
* ): Promise<void> { |
|
140
|
* // TODO logic to remove the file from storage |
|
141
|
* // Use the `file.fileKey` to delete the file, which is the identifier of the file |
|
142
|
* // in the provider's storage. |
|
143
|
* // for example: |
|
144
|
* const fileArray = Array.isArray(files) ? files : [files] |
|
145
|
* for (const file of fileArray) { |
|
146
|
* this.client.delete(file.fileKey) |
|
147
|
* } |
|
148
|
* } |
|
149
|
* } |
|
150
|
*/ |
|
151
|
async delete( |
|
152
|
files: FileTypes.ProviderDeleteFileDTO | FileTypes.ProviderDeleteFileDTO[] |
|
153
|
): Promise<void> { |
|
154
|
throw Error("delete must be overridden by the child class") |
|
155
|
} |
|
156
|
|
|
157
|
/** |
|
158
|
* This method is used to retrieve a download URL of the file. For some providers, |
|
159
|
* such as S3, a presigned URL indicates a temporary URL to get access to a file. |
|
160
|
* |
|
161
|
* If your provider doesn’t perform or offer a similar functionality, you can |
|
162
|
* return the URL to download the file. |
|
163
|
* |
|
164
|
* @param {FileTypes.ProviderGetFileDTO} fileData - The details of the file to get its |
|
165
|
* presigned URL. |
|
166
|
* @returns {Promise<string>} The file's presigned URL. |
|
167
|
* |
|
168
|
* @example |
|
169
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
170
|
* // ... |
|
171
|
* async getPresignedDownloadUrl( |
|
172
|
* fileData: ProviderGetFileDTO |
|
173
|
* ): Promise<string> { |
|
174
|
* // TODO logic to get the presigned URL |
|
175
|
* // Use the `file.fileKey` to delete the file, which is the identifier of the file |
|
176
|
* // in the provider's storage. |
|
177
|
* // for example: |
|
178
|
* return this.client.getPresignedUrl(fileData.fileKey) |
|
179
|
* } |
|
180
|
* } |
|
181
|
*/ |
|
182
|
async getPresignedDownloadUrl( |
|
183
|
fileData: FileTypes.ProviderGetFileDTO |
|
184
|
): Promise<string> { |
|
185
|
throw Error("getPresignedDownloadUrl must be overridden by the child class") |
|
186
|
} |
|
187
|
|
|
188
|
/** |
|
189
|
* This method retrieves an uploaded file as a stream. This is useful when streaming |
|
190
|
* a file to clients or you want to process the file in chunks. |
|
191
|
* |
|
192
|
* @param {FileTypes.ProviderGetFileDTO} fileData - The details of the file to get its stream. |
|
193
|
* @returns {Promise<Readable>} The file's stream. |
|
194
|
* |
|
195
|
* @since 2.8.0 |
|
196
|
* |
|
197
|
* @example |
|
198
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
199
|
* // ... |
|
200
|
* async getAsStream(file: ProviderDeleteFileDTO): Promise<Readable> { |
|
201
|
* // TODO logic to get the file as a stream |
|
202
|
* // Use the `file.fileKey` to get the file, which is the identifier of the file |
|
203
|
* // in the provider's storage. |
|
204
|
* // for example: |
|
205
|
* this.client.getAsStream(file.fileKey) |
|
206
|
* } |
|
207
|
* } |
|
208
|
*/ |
|
209
|
getDownloadStream(fileData: FileTypes.ProviderGetFileDTO): Promise<Readable> { |
|
210
|
throw Error("getDownloadStream must be overridden by the child class") |
|
211
|
} |
|
212
|
|
|
213
|
/** |
|
214
|
* This method retrieves an uploaded file as a buffer. This is useful when you want to |
|
215
|
* process the entire file in memory or send it as a response. |
|
216
|
* |
|
217
|
* @param {FileTypes.ProviderGetFileDTO} fileData - The details of the file to get its buffer. |
|
218
|
* @returns {Promise<Buffer>} The file's buffer. |
|
219
|
* |
|
220
|
* @since 2.8.0 |
|
221
|
* |
|
222
|
* @example |
|
223
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
224
|
* // ... |
|
225
|
* async getAsBuffer(file: ProviderDeleteFileDTO): Promise<Buffer> { |
|
226
|
* // TODO logic to get the file as a buffer |
|
227
|
* // Use the `file.fileKey` to get the file, which is the identifier of the file |
|
228
|
* // in the provider's storage. |
|
229
|
* // for example: |
|
230
|
* this.client.getAsBuffer(file.fileKey) |
|
231
|
* } |
|
232
|
* } |
|
233
|
*/ |
|
234
|
getAsBuffer(fileData: FileTypes.ProviderGetFileDTO): Promise<Buffer> { |
|
235
|
throw Error("getAsBuffer must be overridden by the child class") |
|
236
|
} |
|
237
|
|
|
238
|
/** |
|
239
|
* This method returns a writeable stream to upload a file. |
|
240
|
* |
|
241
|
* @param {FileTypes.ProviderUploadStreamDTO} fileData - The details of the file to upload. |
|
242
|
* @returns {Promise<{ writeStream: Writable, promise: Promise<FileTypes.ProviderFileResultDTO>, url: string, fileKey: string }>} The writeable stream and upload promise. |
|
243
|
* |
|
244
|
* @since 2.8.0 |
|
245
|
* |
|
246
|
* @example |
|
247
|
* class MyFileProviderService extends AbstractFileProviderService { |
|
248
|
* // ... |
|
249
|
* async getUploadStream(fileData: FileTypes.ProviderUploadStreamDTO): Promise<{ |
|
250
|
* writeStream: Writable |
|
251
|
* promise: Promise<FileTypes.ProviderFileResultDTO> |
|
252
|
* url: string |
|
253
|
* fileKey: string |
|
254
|
* }> { |
|
255
|
* // TODO logic to get the writeable stream |
|
256
|
* } |
|
257
|
* } |
|
258
|
*/ |
|
259
|
getUploadStream(fileData: FileTypes.ProviderUploadStreamDTO): Promise<{ |
|
260
|
writeStream: Writable |
|
261
|
promise: Promise<FileTypes.ProviderFileResultDTO> |
|
262
|
url: string |
|
263
|
fileKey: string |
|
264
|
}> { |
|
265
|
throw Error("getUploadStream must be overridden by the child class") |
|
266
|
} |
|
267
|
} |