chore(): Modules providers reorganization (#7234)
Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
1 parent
2f7b53488d
commit
93f6e60c17
47 files changed
+14
-9
No files matched your search
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"name": "@medusajs/file-local-next",
|
||||
"version": "0.0.2",
|
||||
"description": "Local filesystem file storage for Medusa",
|
||||
"main": "dist/index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/medusajs/medusa",
|
||||
"directory": "packages/file-local"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
},
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
|
||||
"test": "jest --passWithNoTests src",
|
||||
"build": "rimraf dist && tsc -p ./tsconfig.json",
|
||||
"watch": "tsc --watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"cross-env": "^5.2.1",
|
||||
"jest": "^25.5.4",
|
||||
"rimraf": "^5.0.1",
|
||||
"typescript": "^4.9.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/utils": "^1.11.7"
|
||||
},
|
||||
"keywords": [
|
||||
"medusa-plugin",
|
||||
"medusa-plugin-file"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { ModuleProviderExports } from "@medusajs/types"
|
||||
import { LocalFileService } from "./services/local-file"
|
||||
|
||||
const services = [LocalFileService]
|
||||
|
||||
const providerExport: ModuleProviderExports = {
|
||||
services,
|
||||
}
|
||||
|
||||
export default providerExport
|
||||
@@ -0,0 +1,100 @@
|
||||
import { FileTypes, LocalFileServiceOptions } from "@medusajs/types"
|
||||
import { AbstractFileProviderService, MedusaError } from "@medusajs/utils"
|
||||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
|
||||
export class LocalFileService extends AbstractFileProviderService {
|
||||
static identifier = "localfs"
|
||||
protected uploadDir_: string
|
||||
protected backendUrl_: string
|
||||
|
||||
constructor(_, options: LocalFileServiceOptions) {
|
||||
super()
|
||||
this.uploadDir_ = options?.upload_dir || "uploads"
|
||||
this.backendUrl_ = options?.backend_url || "http://localhost:9000"
|
||||
}
|
||||
|
||||
async upload(
|
||||
file: FileTypes.ProviderUploadFileDTO
|
||||
): Promise<FileTypes.ProviderFileResultDTO> {
|
||||
if (!file) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, `No file provided`)
|
||||
}
|
||||
|
||||
if (!file.filename) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
`No filename provided`
|
||||
)
|
||||
}
|
||||
|
||||
const parsedFilename = path.parse(file.filename)
|
||||
|
||||
if (parsedFilename.dir) {
|
||||
this.ensureDirExists(parsedFilename.dir)
|
||||
}
|
||||
|
||||
const fileKey = path.join(
|
||||
parsedFilename.dir,
|
||||
`${Date.now()}-${parsedFilename.base}`
|
||||
)
|
||||
|
||||
const filePath = this.getUploadFilePath(fileKey)
|
||||
const fileUrl = this.getUploadFileUrl(fileKey)
|
||||
|
||||
const content = Buffer.from(file.content, "binary")
|
||||
await fs.writeFile(filePath, content)
|
||||
|
||||
return {
|
||||
key: fileKey,
|
||||
url: fileUrl,
|
||||
}
|
||||
}
|
||||
|
||||
async delete(file: FileTypes.ProviderDeleteFileDTO): Promise<void> {
|
||||
const filePath = this.getUploadFilePath(file.fileKey)
|
||||
try {
|
||||
await fs.access(filePath, fs.constants.F_OK)
|
||||
await fs.unlink(filePath)
|
||||
} catch (e) {
|
||||
// The file does not exist, so it's a noop.
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
async getPresignedDownloadUrl(
|
||||
fileData: FileTypes.ProviderGetFileDTO
|
||||
): Promise<string> {
|
||||
try {
|
||||
await fs.access(
|
||||
this.getUploadFilePath(fileData.fileKey),
|
||||
fs.constants.F_OK
|
||||
)
|
||||
} catch {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`File with key ${fileData.fileKey} not found`
|
||||
)
|
||||
}
|
||||
|
||||
return this.getUploadFileUrl(fileData.fileKey)
|
||||
}
|
||||
|
||||
private getUploadFilePath = (fileKey: string) => {
|
||||
return path.join(this.uploadDir_, fileKey)
|
||||
}
|
||||
|
||||
private getUploadFileUrl = (fileKey: string) => {
|
||||
return path.join(this.backendUrl_, this.getUploadFilePath(fileKey))
|
||||
}
|
||||
|
||||
private async ensureDirExists(dirPath: string) {
|
||||
const relativePath = path.join(this.uploadDir_, dirPath)
|
||||
try {
|
||||
await fs.access(relativePath, fs.constants.F_OK)
|
||||
} catch (e) {
|
||||
await fs.mkdir(relativePath, { recursive: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": [
|
||||
"es5",
|
||||
"es6",
|
||||
"es2019"
|
||||
],
|
||||
"target": "es5",
|
||||
"jsx": "react-jsx" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
"declaration": true,
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"noImplicitReturns": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"downlevelIteration": true, // to use ES5 specific tooling
|
||||
"inlineSourceMap": true /* Emit a single file with source maps instead of having a separate file. */
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": [
|
||||
"dist",
|
||||
"build",
|
||||
"src/**/__tests__",
|
||||
"src/**/__mocks__",
|
||||
"src/**/__fixtures__",
|
||||
"node_modules",
|
||||
".eslintrc.js"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in new issue
Block a user