1 import { FileSystem } from "@medusajs/utils"
2 import fs from "fs/promises"
3 import path from "path"
4 import { LocalFileService } from "../../src/services/local-file"
5
6 jest.setTimeout(10000)
7
8 describe("Local File Plugin", () => {
9 let localService: LocalFileService
10
11 const fixtureImagePath =
12 process.cwd() + "/integration-tests/__fixtures__/catphoto.jpg"
13
14 const uploadDir = path.join(
15 process.cwd(),
16 "integration-tests/__tests__/uploads"
17 )
18
19 const fileSystem = new FileSystem(uploadDir)
20
21 beforeAll(async () => {
22 localService = new LocalFileService(
23 {
24 logger: console as any,
25 },
26 {
27 upload_dir: uploadDir,
28 backend_url: "http://localhost:9000/static",
29 }
30 )
31 })
32
33 afterAll(async () => {
34 await fileSystem.cleanup()
35 })
36
37 it(`should upload, read, and then delete a public file successfully`, async () => {
38 const fileContent = await fs.readFile(fixtureImagePath)
39 const fixtureAsBase64 = fileContent.toString("base64")
40
41 const resp = await localService.upload({
42 filename: "catphoto.jpg",
43 mimeType: "image/jpeg",
44 content: fileContent as any,
45 access: "public",
46 })
47
48 expect(resp).toEqual({
49 key: expect.stringMatching(/catphoto.*\.jpg/),
50 url: expect.stringMatching(
51 /http:\/\/localhost:9000\/static\/.*catphoto.*\.jpg/
52 ),
53 })
54
55 // For local file provider, we can verify the file exists on disk
56 const fileKey = resp.key
57 const baseDir = uploadDir
58 const filePath = path.join(baseDir, fileKey)
59
60 const fileOnDisk = await fs.readFile(filePath)
61
62 const fileOnDiskAsBase64 = fileOnDisk.toString("base64")
63
64 expect(fileOnDiskAsBase64).toEqual(fixtureAsBase64)
65
66 const signedUrl = await localService.getPresignedDownloadUrl({
67 fileKey: resp.key,
68 })
69
70 expect(signedUrl).toEqual(resp.url)
71
72 const buffer = await localService.getAsBuffer({ fileKey: resp.key })
73 expect(buffer).toEqual(fileContent)
74
75 await localService.delete({ fileKey: resp.key })
76
77 await expect(fs.access(filePath)).rejects.toThrow()
78 })
79
80 it("uploads using stream", async () => {
81 const fileContent = await fs.readFile(fixtureImagePath)
82
83 const { writeStream, promise } = await localService.getUploadStream({
84 filename: "catphoto-stream.jpg",
85 mimeType: "image/jpeg",
86 access: "public",
87 })
88
89 writeStream.write(fileContent)
90 writeStream.end()
91
92 const resp = await promise
93
94 expect(resp).toEqual({
95 key: expect.stringMatching(/catphoto-stream.*\.jpg/),
96 url: expect.stringMatching(
97 /http:\/\/localhost:9000\/static\/.*catphoto-stream.*\.jpg/
98 ),
99 })
100
101 const fileKey = resp.key
102 const filePath = path.join(uploadDir, fileKey)
103
104 const fileOnDisk = await fs.readFile(filePath)
105 expect(fileOnDisk).toEqual(fileContent)
106
107 const signedUrl = await localService.getPresignedDownloadUrl({
108 fileKey: resp.key,
109 })
110
111 expect(signedUrl).toEqual(resp.url)
112
113 const buffer = await localService.getAsBuffer({ fileKey: resp.key })
114 expect(buffer).toEqual(fileContent)
115
116 await localService.delete({ fileKey: resp.key })
117 await expect(fs.access(filePath)).rejects.toThrow()
118 })
119 })