├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── src ├── api │ └── index.js └── services │ └── fileStorage.js └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/preset-env"] 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /api 2 | /services 3 | /models 4 | /subscribers 5 | /dist 6 | /lib 7 | .DS_store 8 | .env* 9 | /*.js 10 | !index.js 11 | !jest.config.js 12 | 13 | # Logs 14 | logs 15 | *.log 16 | npm-debug.log* 17 | yarn-debug.log* 18 | yarn-error.log* 19 | lerna-debug.log* 20 | .pnpm-debug.log* 21 | 22 | # Diagnostic reports (https://nodejs.org/api/report.html) 23 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 24 | 25 | # Runtime data 26 | pids 27 | *.pid 28 | *.seed 29 | *.pid.lock 30 | 31 | # Directory for instrumented libs generated by jscoverage/JSCover 32 | lib-cov 33 | 34 | # Coverage directory used by tools like istanbul 35 | coverage 36 | *.lcov 37 | 38 | # nyc test coverage 39 | .nyc_output 40 | 41 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 42 | .grunt 43 | 44 | # Bower dependency directory (https://bower.io/) 45 | bower_components 46 | 47 | # node-waf configuration 48 | .lock-wscript 49 | 50 | # Compiled binary addons (https://nodejs.org/api/addons.html) 51 | build/Release 52 | 53 | # Dependency directories 54 | node_modules/ 55 | jspm_packages/ 56 | 57 | # Snowpack dependency directory (https://snowpack.dev/) 58 | web_modules/ 59 | 60 | # TypeScript cache 61 | *.tsbuildinfo 62 | 63 | # Optional npm cache directory 64 | .npm 65 | 66 | # Optional eslint cache 67 | .eslintcache 68 | 69 | # Microbundle cache 70 | .rpt2_cache/ 71 | .rts2_cache_cjs/ 72 | .rts2_cache_es/ 73 | .rts2_cache_umd/ 74 | 75 | # Optional REPL history 76 | .node_repl_history 77 | 78 | # Output of 'npm pack' 79 | *.tgz 80 | 81 | # Yarn Integrity file 82 | .yarn-integrity 83 | 84 | # dotenv environment variables file 85 | .env 86 | .env.test 87 | .env.production 88 | 89 | # parcel-bundler cache (https://parceljs.org/) 90 | .cache 91 | .parcel-cache 92 | 93 | # Next.js build output 94 | .next 95 | out 96 | 97 | # Nuxt.js build / generate output 98 | .nuxt 99 | dist 100 | 101 | # Gatsby files 102 | .cache/ 103 | # Comment in the public line in if your project uses Gatsby and not Next.js 104 | # https://nextjs.org/blog/next-9-1#public-directory-support 105 | # public 106 | 107 | # vuepress build output 108 | .vuepress/dist 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* 131 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /lib 2 | /src 3 | node_modules 4 | .DS_store 5 | .env* 6 | /*.js 7 | !index.js 8 | yarn.lock 9 | .gitignore 10 | .babelrc -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ❗ Update (v0.1.3) ❗: 2 | 3 | This is no longer maintained, as there is an official package for local filestorage now: [@medusajs/file-local](https://www.npmjs.com/package/@medusajs/file-local). 4 | 5 | --- 6 | 7 | # Local filestorage Medusa plugin 8 | 9 | This repo provides the plugin to get you started serving images using your filesystem. Follow the steps below to get ready. Store your images locally on your installation of [Medusa](https://github.com/medusajs/medusa) e-commerce. 10 | 11 | ## Adding plugin to Medusa.js Core 12 | 13 | 1. Download the [package](https://www.npmjs.com/package/medusa-plugin-filestorage-local) 14 | 15 | 16 | ``` 17 | npm install medusa-plugin-filestorage-local 18 | pnpm install medusa-plugin-filestorage-local 19 | yarn add medusa-plugin-filestorage-local 20 | ``` 21 | 22 | 2. Register it in the `medusa-config.js` 23 | 24 | ### Default configuration 25 | 26 | ```js 27 | const plugins = [ 28 | ... 29 | { 30 | resolve: "medusa-plugin-filestorage-local", 31 | options: { 32 | // The baseurl for your medusajs server 33 | serverBaseUrl: process.env.MEDUSA_BACKEND_URL || "http://localhost:9000", 34 | // when enabled saves the file as a base64 encoded string inside the database (deleting that row is not yet supported) 35 | saveInDatabase: false, // recommended: false 36 | // the folder where your files are stored on the server 37 | fileLocation: "uploads/persistent/", 38 | } 39 | }, 40 | ... 41 | ] 42 | ``` 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "medusa-plugin-filestorage-local", 3 | "version": "0.1.3", 4 | "description": "", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "github:blechlawine/medusa-plugin-filestorage-local" 9 | }, 10 | "scripts": { 11 | "build": "babel src -d .", 12 | "prepare": "cross-env NODE_ENV=production npm run build", 13 | "watch": "babel -w src --out-dir . --ignore **/__tests__" 14 | }, 15 | "keywords": [ 16 | "medusa", 17 | "local", 18 | "filestorage", 19 | "medusa-plugin" 20 | ], 21 | "peerDependencies": { 22 | "medusa-interfaces": "1.x" 23 | }, 24 | "author": "Blechlawine", 25 | "license": "MIT", 26 | "devDependencies": { 27 | "@babel/cli": "^7.16.0", 28 | "@babel/core": "^7.16.0", 29 | "@babel/preset-env": "^7.16.4", 30 | "cross-env": "^7.0.3" 31 | }, 32 | "dependencies": { 33 | "cors": "^2.8.5", 34 | "express": "^4.17.1", 35 | "medusa-core-utils": "^1.1.30" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/api/index.js: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | const { Router } = express; 3 | 4 | export default () => { 5 | const router = Router(); 6 | 7 | router.use("/uploads/persistent", express.static("uploads/persistent")); 8 | 9 | return router; 10 | }; 11 | -------------------------------------------------------------------------------- /src/services/fileStorage.js: -------------------------------------------------------------------------------- 1 | import { FileService } from "medusa-interfaces"; 2 | import fs from "fs"; 3 | 4 | class FileStorageService extends FileService { 5 | constructor(_, config) { 6 | super(); 7 | this.config = { 8 | serverBaseUrl: "http://localhost:9000", 9 | saveInDatabase: false, 10 | fileLocation: "uploads/persistent/", 11 | ...config, 12 | }; 13 | if (!fs.existsSync(this.config.fileLocation)) { 14 | fs.mkdirSync(this.config.fileLocation); 15 | } 16 | } 17 | 18 | upload(file) { 19 | return new Promise((resolve, reject) => { 20 | if (this.config.saveInDatabase) { 21 | // saves the image as a base64 encoded string in the database (in the url column) 22 | let data = fs.readFileSync(file.path, { encoding: "base64" }); 23 | resolve({ 24 | url: `data:${file.mimetype};base64, ${data.toString("base64")}`, 25 | }); 26 | } else { 27 | fs.copyFile( 28 | file.path, 29 | this.config.fileLocation + file.filename, 30 | (err) => { 31 | if (err) throw err; 32 | resolve({ 33 | url: `${this.config.serverBaseUrl}/${this.config.fileLocation}${file.filename}`, 34 | }); 35 | }, 36 | ); 37 | } 38 | }); 39 | } 40 | 41 | delete(file) { 42 | // The Promise resolve value is ignored 43 | console.log(file); 44 | return Promise.resolve("deleted"); 45 | } 46 | } 47 | 48 | export default FileStorageService; 49 | --------------------------------------------------------------------------------