1 import {
2 Context,
3 CreateFileDTO,
4 FileDTO,
5 FileTypes,
6 FilterableFileProps,
7 FindConfig,
8 GetUploadFileUrlDTO,
9 ModuleJoinerConfig,
10 UploadFileUrlDTO,
11 } from "@medusajs/framework/types"
12 import type { Readable, Writable } from "stream"
13
14 import { MedusaError } from "@medusajs/framework/utils"
15 import { joinerConfig } from "../joiner-config"
16 import FileProviderService from "./file-provider-service"
17
18 type InjectedDependencies = {
19 fileProviderService: FileProviderService
20 }
21
22 export default class FileModuleService implements FileTypes.IFileModuleService {
23 protected readonly fileProviderService_: FileProviderService
24 constructor({ fileProviderService }: InjectedDependencies) {
25 this.fileProviderService_ = fileProviderService
26 }
27
28 __joinerConfig(): ModuleJoinerConfig {
29 return joinerConfig
30 }
31
32 getProvider() {
33 return this.fileProviderService_
34 }
35
36 createFiles(
37 data: CreateFileDTO[],
38 sharedContext?: Context
39 ): Promise<FileDTO[]>
40 createFiles(data: CreateFileDTO, sharedContext?: Context): Promise<FileDTO>
41
42 async createFiles(
43 data: CreateFileDTO[] | CreateFileDTO
44 ): Promise<FileDTO[] | FileDTO> {
45 const input = Array.isArray(data) ? data : [data]
46 // TODO: Validate file mime type, have config for allowed types
47
48 const files = await Promise.all(
49 input.map((file) => this.fileProviderService_.upload(file))
50 )
51 const result = files.map((file) => ({
52 id: file.key,
53 url: file.url,
54 }))
55
56 return Array.isArray(data) ? result : result[0]
57 }
58
59 getUploadFileUrls(
60 data: GetUploadFileUrlDTO[],
61 sharedContext?: Context
62 ): Promise<UploadFileUrlDTO[]>
63 getUploadFileUrls(
64 data: GetUploadFileUrlDTO,
65 sharedContext?: Context
66 ): Promise<UploadFileUrlDTO>
67
68 async getUploadFileUrls(
69 data: GetUploadFileUrlDTO[] | GetUploadFileUrlDTO
70 ): Promise<UploadFileUrlDTO[] | UploadFileUrlDTO> {
71 const input = Array.isArray(data) ? data : [data]
72
73 const result = await Promise.all(
74 input.map((file) => this.fileProviderService_.getPresignedUploadUrl(file))
75 )
76
77 return Array.isArray(data) ? result : result[0]
78 }
79
80 async deleteFiles(ids: string[], sharedContext?: Context): Promise<void>
81 async deleteFiles(id: string, sharedContext?: Context): Promise<void>
82 async deleteFiles(ids: string[] | string): Promise<void> {
83 const input = Array.isArray(ids) ? ids : [ids]
84 await this.fileProviderService_.delete(
85 input.map((id) => {
86 return { fileKey: id }
87 })
88 )
89 }
90
91 async retrieveFile(id: string): Promise<FileDTO> {
92 const res = await this.fileProviderService_.getPresignedDownloadUrl({
93 fileKey: id,
94 })
95
96 return {
97 id,
98 url: res,
99 }
100 }
101
102 async listFiles(
103 filters?: FilterableFileProps,
104 config?: FindConfig<FileDTO>,
105 sharedContext?: Context
106 ): Promise<FileDTO[]> {
107 if (!filters?.id) {
108 throw new MedusaError(
109 MedusaError.Types.INVALID_DATA,
110 "Listing of files is only supported when filtering by ID."
111 )
112 }
113
114 const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id]
115
116 const res = await Promise.all(
117 ids.map(async (id) => {
118 const res = await this.fileProviderService_.getPresignedDownloadUrl({
119 fileKey: id,
120 })
121 return { id, url: res }
122 })
123 )
124
125 return res
126 }
127
128 async listAndCountFiles(
129 filters?: FilterableFileProps,
130 config?: FindConfig<FileDTO>,
131 sharedContext?: Context
132 ): Promise<[FileDTO[], number]> {
133 if (!filters?.id) {
134 throw new MedusaError(
135 MedusaError.Types.INVALID_DATA,
136 "Listing of files is only supported when filtering by ID."
137 )
138 }
139
140 const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id]
141
142 const res = await Promise.all(
143 ids.map(async (id) => {
144 const res = await this.fileProviderService_.getPresignedDownloadUrl({
145 fileKey: id,
146 })
147 return { id, url: res }
148 })
149 )
150
151 return [res, res.length]
152 }
153
154 /**
155 * Get the file contents as a readable stream.
156 *
157 * @example
158 * const stream = await fileModuleService.getAsStream("file_123")
159 * writeable.pipe(stream)
160 */
161 getDownloadStream(id: string): Promise<Readable> {
162 return this.fileProviderService_.getDownloadStream({ fileKey: id })
163 }
164
165 /**
166 * Get the file contents as a Node.js Buffer
167 *
168 * @example
169 * const contents = await fileModuleService.getAsBuffer("file_123")
170 * contents.toString('utf-8')
171 */
172 getAsBuffer(id: string): Promise<Buffer> {
173 return this.fileProviderService_.getAsBuffer({ fileKey: id })
174 }
175
176 /**
177 * Get a writeable stream to upload a file.
178 *
179 * @example
180 * const { writeStream, promise } = await fileModuleService.getUploadStream({
181 * filename: "test.csv",
182 * mimeType: "text/csv",
183 * })
184 *
185 * stream.pipe(writeStream)
186 * const result = await promise
187 */
188 getUploadStream(data: FileTypes.ProviderUploadStreamDTO): Promise<{
189 writeStream: Writable
190 promise: Promise<FileTypes.ProviderFileResultDTO>
191 url: string
192 fileKey: string
193 }> {
194 return this.fileProviderService_.getUploadStream(data)
195 }
196 }