feat: Add local file provider and wire everything up in the file module (#7134)

This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-04-24 10:59:58 +02:00
1 parent fe68b5c0f2
commit 614d659a59
35 files changed
+684 -49

No files matched your search

@@ -3,7 +3,7 @@ import {
MedusaResponse,
} from "../../../../types/routing"
import { deleteFilesWorkflow } from "@medusajs/core-flows"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils"
import { remoteQueryObjectFromString } from "@medusajs/utils"
export const GET = async (
@@ -20,6 +20,13 @@ export const GET = async (
})
const [file] = await remoteQuery(queryObject)
if (!file) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`File with id: ${req.params.id} not found`
)
}
res.status(200).json({ file })
}
@@ -1,8 +1,14 @@
import multer from "multer"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
import { validateAndTransformQuery } from "../../utils/validate-query"
import { retrieveUploadConfig } from "./query-config"
import { AdminGetUploadParams } from "./validators"
const upload = multer({ dest: "uploads/" })
// TODO: For now we keep the files in memory, as that's how they get passed to the workflows
// This will need revisiting once we are closer to prod-ready v2, since with workflows and potentially
// services on other machines using streams is not as simple as it used to be.
const upload = multer({ storage: multer.memoryStorage() })
export const adminUploadRoutesMiddlewares: MiddlewareRoute[] = [
{
@@ -10,16 +16,21 @@ export const adminUploadRoutesMiddlewares: MiddlewareRoute[] = [
matcher: "/admin/uploads*",
middlewares: [authenticate("admin", ["bearer", "session", "api-key"])],
},
{
method: ["GET"],
matcher: "/admin/uploads/:id",
middlewares: [],
},
// TODO: There is a `/protected` route in v1 that might need a bit more thought when implementing
{
method: ["POST"],
matcher: "/admin/uploads",
middlewares: [upload.array("files")],
middlewares: [
upload.array("files"),
validateAndTransformQuery(AdminGetUploadParams, retrieveUploadConfig),
],
},
{
method: ["GET"],
matcher: "/admin/uploads/:id",
middlewares: [
validateAndTransformQuery(AdminGetUploadParams, retrieveUploadConfig),
],
},
{
method: ["DELETE"],
@@ -4,18 +4,27 @@ import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { MedusaError } from "@medusajs/utils"
export const POST = async (
req: AuthenticatedMedusaRequest<CreateProductDTO>,
res: MedusaResponse
) => {
const input = req.files as any[]
const input = req.files as Express.Multer.File[]
if (!input?.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"No files were uploaded"
)
}
const { result, errors } = await uploadFilesWorkflow(req.scope).run({
input: {
files: input?.map((f) => ({
filename: "test",
mimeType: "image/jpeg",
content: f.buffer,
filename: f.originalname,
mimeType: f.mimetype,
content: f.buffer.toString("binary"),
})),
},
throwOnError: false,
@@ -24,5 +33,6 @@ export const POST = async (
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
res.status(200).json({ files: result })
}
@@ -0,0 +1,5 @@
import { createSelectParams } from "../../utils/validators"
import { z } from "zod"
export type AdminGetUploadParamsType = z.infer<typeof AdminGetUploadParams>
export const AdminGetUploadParams = createSelectParams()