feat(medusa): Delete and download url endpoints (#1705)

This commit is contained in:
Philip Korsholm
2022-06-24 10:37:32 +02:00
committed by GitHub
parent a5f717be5a
commit cc29b641c9
7 changed files with 78 additions and 16 deletions
+1 -1
View File
@@ -13,4 +13,4 @@ Learn more about how you can use this plugin in the [documentation](https://docs
access_key_id: "YOUR-ACCESS-KEY",
secret_access_key: "YOUR-SECRET-KEY",
}
```
```
@@ -1,3 +1,6 @@
import { IsString } from "class-validator"
import { validator } from "../../../../utils/validator"
/**
* [delete] /uploads
* operationId: "AdminDeleteUpload"
@@ -11,14 +14,20 @@
* description: OK
*/
export default async (req, res) => {
try {
const fileService = req.scope.resolve("fileService")
const validated = await validator(AdminDeleteUploadReq, req.body, {
forbidUnknownValues: false,
})
await fileService.delete(req.body.file)
const fileService = req.scope.resolve("fileService")
res.status(200).send({ id: "", object: "file", deleted: true })
} catch (err) {
console.log(err)
throw err
}
await fileService.delete(validated)
res
.status(200)
.send({ id: validated.file_key, object: "file", deleted: true })
}
class AdminDeleteUploadReq {
@IsString()
file_key: string
}
@@ -0,0 +1,43 @@
import { IsString } from "class-validator"
import { AbstractFileService } from "../../../../interfaces"
import { validator } from "../../../../utils/validator"
/**
* [get] /uploads
* operationId: "GetUploadsFileDownloadUrl"
* summary: "Gets a presigned download url for a file"
* description: "Gets a presigned download url for a file"
* x-authenticated: true
* requestBody:
* content:
* application/json:
* schema:
* required:
* - file_key
* properties:
* file_key:
* description: "key of the file to obtain the download link for"
* type: string
* - (path) fileKey=* {string} key of the file to obtain the download link for.
* tags:
* - Uploads
* responses:
* 200:
* description: OK
*/
export default async (req, res) => {
const validated = await validator(AdminGetUploadsFileDownloadUrlReq, req.body)
const fileService: AbstractFileService<any> = req.scope.resolve("fileService")
const url = await fileService.getPresignedDownloadUrl({
fileKey: validated.file_key,
})
res.status(200).send({ download_url: url })
}
class AdminGetUploadsFileDownloadUrlReq {
@IsString()
file_key: string
}
@@ -16,8 +16,12 @@ export default (app) => {
middlewares.wrap(require("./create-upload").default)
)
// removed on purpose
// route.post("/delete", middlewares.wrap(require("./delete-upload").default))
route.delete("/", middlewares.wrap(require("./delete-upload").default))
route.get(
"/download-url",
middlewares.wrap(require("./get-download-url").default)
)
return app
}
@@ -29,4 +33,5 @@ export type AdminUploadRes = {
export type AdminDeleteUploadRes = DeleteResponse
export * from "./create-upload"
// export * from "./delete-upload"
export * from "./delete-upload"
export * from "./get-download-url"
@@ -18,6 +18,11 @@ export type GetUploadedFileType = {
[x: string]: unknown
}
export type DeleteFileType = {
fileKey: string
[x: string]: unknown
}
export type UploadStreamDescriptorType = {
name: string
ext?: string
@@ -37,7 +42,7 @@ export interface IFileService<T extends TransactionBaseService<any>>
* remove file from fileservice
* @param fileData Remove file described by record
* */
delete(fileData: Record<string, any>): void
delete(fileData: DeleteFileType): Promise<void>
/**
* upload file to fileservice from stream
@@ -72,7 +77,7 @@ export abstract class AbstractFileService<T extends TransactionBaseService<any>>
fileData: Express.Multer.File
): Promise<FileServiceUploadResult>
abstract delete(fileData: Record<string, any>): void
abstract delete(fileData: DeleteFileType): Promise<void>
abstract getUploadStreamDescriptor(
fileData: UploadStreamDescriptorType
+1 -1
View File
@@ -15,7 +15,7 @@ class DefaultFileService extends AbstractFileService<any> {
"Please add a file service plugin in order to manipulate files in Medusa"
)
}
delete(fileData: Record<string, any>): void {
delete(fileData: Record<string, any>): Promise<void> {
throw new MedusaError(
MedusaError.Types.UNEXPECTED_STATE,
"Please add a file service plugin in order to manipulate files in Medusa"
@@ -290,7 +290,7 @@ export default class ProductExportStrategy extends AbstractBatchJobStrategy<
await this.fileService_
.withTransaction(transactionManager)
.delete({ key: fileKey })
.delete({ fileKey })
return
}
}