feat(medusa): Delete and download url endpoints (#1705)
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
import { IsString } from "class-validator"
|
||||||
|
import { validator } from "../../../../utils/validator"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [delete] /uploads
|
* [delete] /uploads
|
||||||
* operationId: "AdminDeleteUpload"
|
* operationId: "AdminDeleteUpload"
|
||||||
@@ -11,14 +14,20 @@
|
|||||||
* description: OK
|
* description: OK
|
||||||
*/
|
*/
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
try {
|
const validated = await validator(AdminDeleteUploadReq, req.body, {
|
||||||
|
forbidUnknownValues: false,
|
||||||
|
})
|
||||||
|
|
||||||
const fileService = req.scope.resolve("fileService")
|
const fileService = req.scope.resolve("fileService")
|
||||||
|
|
||||||
await fileService.delete(req.body.file)
|
await fileService.delete(validated)
|
||||||
|
|
||||||
res.status(200).send({ id: "", object: "file", deleted: true })
|
res
|
||||||
} catch (err) {
|
.status(200)
|
||||||
console.log(err)
|
.send({ id: validated.file_key, object: "file", deleted: true })
|
||||||
throw err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
middlewares.wrap(require("./create-upload").default)
|
||||||
)
|
)
|
||||||
|
|
||||||
// removed on purpose
|
route.delete("/", middlewares.wrap(require("./delete-upload").default))
|
||||||
// route.post("/delete", middlewares.wrap(require("./delete-upload").default))
|
|
||||||
|
route.get(
|
||||||
|
"/download-url",
|
||||||
|
middlewares.wrap(require("./get-download-url").default)
|
||||||
|
)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
@@ -29,4 +33,5 @@ export type AdminUploadRes = {
|
|||||||
export type AdminDeleteUploadRes = DeleteResponse
|
export type AdminDeleteUploadRes = DeleteResponse
|
||||||
|
|
||||||
export * from "./create-upload"
|
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
|
[x: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type DeleteFileType = {
|
||||||
|
fileKey: string
|
||||||
|
[x: string]: unknown
|
||||||
|
}
|
||||||
|
|
||||||
export type UploadStreamDescriptorType = {
|
export type UploadStreamDescriptorType = {
|
||||||
name: string
|
name: string
|
||||||
ext?: string
|
ext?: string
|
||||||
@@ -37,7 +42,7 @@ export interface IFileService<T extends TransactionBaseService<any>>
|
|||||||
* remove file from fileservice
|
* remove file from fileservice
|
||||||
* @param fileData Remove file described by record
|
* @param fileData Remove file described by record
|
||||||
* */
|
* */
|
||||||
delete(fileData: Record<string, any>): void
|
delete(fileData: DeleteFileType): Promise<void>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* upload file to fileservice from stream
|
* upload file to fileservice from stream
|
||||||
@@ -72,7 +77,7 @@ export abstract class AbstractFileService<T extends TransactionBaseService<any>>
|
|||||||
fileData: Express.Multer.File
|
fileData: Express.Multer.File
|
||||||
): Promise<FileServiceUploadResult>
|
): Promise<FileServiceUploadResult>
|
||||||
|
|
||||||
abstract delete(fileData: Record<string, any>): void
|
abstract delete(fileData: DeleteFileType): Promise<void>
|
||||||
|
|
||||||
abstract getUploadStreamDescriptor(
|
abstract getUploadStreamDescriptor(
|
||||||
fileData: UploadStreamDescriptorType
|
fileData: UploadStreamDescriptorType
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class DefaultFileService extends AbstractFileService<any> {
|
|||||||
"Please add a file service plugin in order to manipulate files in Medusa"
|
"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(
|
throw new MedusaError(
|
||||||
MedusaError.Types.UNEXPECTED_STATE,
|
MedusaError.Types.UNEXPECTED_STATE,
|
||||||
"Please add a file service plugin in order to manipulate files in Medusa"
|
"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_
|
await this.fileService_
|
||||||
.withTransaction(transactionManager)
|
.withTransaction(transactionManager)
|
||||||
.delete({ key: fileKey })
|
.delete({ fileKey })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user