@medusajs/file-local 1.0.1-next-20230529135657
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -0
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.js +14 -0
- package/dist/api/index.js.map +1 -0
- package/dist/services/local-file-service.d.ts +20 -0
- package/dist/services/local-file-service.js +139 -0
- package/dist/services/local-file-service.js.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Local file storage
|
|
2
|
+
|
|
3
|
+
Store uploaded files to your Medusa backend locally.
|
|
4
|
+
|
|
5
|
+
> Not suited for production environments
|
|
6
|
+
|
|
7
|
+
[Plugin Documentation](https://docs.medusajs.com/plugins/file-service/local) | [Medusa Website](https://medusajs.com) | [Medusa Repository](https://github.com/medusajs/medusa)
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Store product images locally
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Prerequisites
|
|
16
|
+
|
|
17
|
+
- [Medusa backend](https://docs.medusajs.com/development/backend/install)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## How to Install
|
|
22
|
+
|
|
23
|
+
1\. Run the following command in the directory of the Medusa backend:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @medusajs/file-local
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
2 \. In `medusa-config.js` add the following at the end of the `plugins` array:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
const plugins = [
|
|
33
|
+
// ...
|
|
34
|
+
`@medusajs/file-local`
|
|
35
|
+
]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Test the Plugin
|
|
41
|
+
|
|
42
|
+
1\. Run the following command in the directory of the Medusa backend to run the backend:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm run start
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
2\. Upload an image for a product using the admin dashboard or using [the Admin APIs](https://docs.medusajs.com/api/admin#tag/Upload).
|
|
49
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var express_1 = __importDefault(require("express"));
|
|
7
|
+
exports.default = (function (rootDirectory, pluginOptions) {
|
|
8
|
+
var _a;
|
|
9
|
+
var app = express_1.default.Router();
|
|
10
|
+
var uploadDir = (_a = pluginOptions.upload_dir) !== null && _a !== void 0 ? _a : "uploads/images";
|
|
11
|
+
app.use("/".concat(uploadDir), express_1.default.static(uploadDir));
|
|
12
|
+
return app;
|
|
13
|
+
});
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":";;;;;AAAA,oDAA6B;AAE7B,mBAAe,UAAC,aAAa,EAAE,aAAa;;IAC1C,IAAM,GAAG,GAAG,iBAAO,CAAC,MAAM,EAAE,CAAA;IAE5B,IAAM,SAAS,GAAG,MAAA,aAAa,CAAC,UAAU,mCAAI,gBAAgB,CAAA;IAE9D,GAAG,CAAC,GAAG,CAAC,WAAI,SAAS,CAAE,EAAE,iBAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAA;IAEnD,OAAO,GAAG,CAAA;AACZ,CAAC,EAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="multer" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { AbstractFileService, FileServiceGetUploadStreamResult, FileServiceUploadResult, IFileService } from "@medusajs/medusa";
|
|
4
|
+
declare class LocalService extends AbstractFileService implements IFileService {
|
|
5
|
+
protected uploadDir_: string;
|
|
6
|
+
protected backendUrl_: string;
|
|
7
|
+
constructor({}: {}, options: any);
|
|
8
|
+
upload(file: Express.Multer.File): Promise<FileServiceUploadResult>;
|
|
9
|
+
uploadProtected(file: Express.Multer.File): Promise<{
|
|
10
|
+
url: string;
|
|
11
|
+
}>;
|
|
12
|
+
uploadFile(file: Express.Multer.File, options?: {}): Promise<{
|
|
13
|
+
url: string;
|
|
14
|
+
}>;
|
|
15
|
+
delete(file: any): Promise<void>;
|
|
16
|
+
getUploadStreamDescriptor(fileData: any): Promise<FileServiceGetUploadStreamResult>;
|
|
17
|
+
getDownloadStream(fileData: any): Promise<NodeJS.ReadableStream>;
|
|
18
|
+
getPresignedDownloadUrl(fileData: any): Promise<string>;
|
|
19
|
+
}
|
|
20
|
+
export default LocalService;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
54
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
55
|
+
};
|
|
56
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
+
var medusa_1 = require("@medusajs/medusa");
|
|
58
|
+
var fs_1 = __importDefault(require("fs"));
|
|
59
|
+
var path_1 = require("path");
|
|
60
|
+
var LocalService = /** @class */ (function (_super) {
|
|
61
|
+
__extends(LocalService, _super);
|
|
62
|
+
function LocalService(_a, options) {
|
|
63
|
+
var _this = _super.call(this, {}, options) || this;
|
|
64
|
+
_this.uploadDir_ = options.upload_dir || "uploads/images";
|
|
65
|
+
_this.backendUrl_ = options.backend_url || "http://localhost:9000";
|
|
66
|
+
return _this;
|
|
67
|
+
}
|
|
68
|
+
LocalService.prototype.upload = function (file) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
70
|
+
return __generator(this, function (_a) {
|
|
71
|
+
switch (_a.label) {
|
|
72
|
+
case 0: return [4 /*yield*/, this.uploadFile(file)];
|
|
73
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
LocalService.prototype.uploadProtected = function (file) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
80
|
+
return __generator(this, function (_a) {
|
|
81
|
+
switch (_a.label) {
|
|
82
|
+
case 0: return [4 /*yield*/, this.uploadFile(file, {})];
|
|
83
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
LocalService.prototype.uploadFile = function (file, options) {
|
|
89
|
+
if (options === void 0) { options = {}; }
|
|
90
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
91
|
+
var parsedFilename, fileKey;
|
|
92
|
+
var _this = this;
|
|
93
|
+
return __generator(this, function (_a) {
|
|
94
|
+
parsedFilename = (0, path_1.parse)(file.originalname);
|
|
95
|
+
fileKey = "".concat(parsedFilename.name, "-").concat(Date.now()).concat(parsedFilename.ext);
|
|
96
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
97
|
+
fs_1.default.copyFile(file.path, "".concat(_this.uploadDir_, "/").concat(fileKey), function (err) {
|
|
98
|
+
if (err) {
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
var fileUrl = "".concat(_this.backendUrl_, "/").concat(_this.uploadDir_, "/").concat(fileKey);
|
|
102
|
+
resolve({ url: fileUrl });
|
|
103
|
+
});
|
|
104
|
+
})];
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
LocalService.prototype.delete = function (file) {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
110
|
+
return __generator(this, function (_a) {
|
|
111
|
+
throw Error("Not implemented");
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
LocalService.prototype.getUploadStreamDescriptor = function (fileData) {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
117
|
+
return __generator(this, function (_a) {
|
|
118
|
+
throw Error("Not implemented");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
LocalService.prototype.getDownloadStream = function (fileData) {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
124
|
+
return __generator(this, function (_a) {
|
|
125
|
+
throw Error("Not implemented");
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
LocalService.prototype.getPresignedDownloadUrl = function (fileData) {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
131
|
+
return __generator(this, function (_a) {
|
|
132
|
+
throw Error("Not implemented");
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
return LocalService;
|
|
137
|
+
}(medusa_1.AbstractFileService));
|
|
138
|
+
exports.default = LocalService;
|
|
139
|
+
//# sourceMappingURL=local-file-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-file-service.js","sourceRoot":"","sources":["../../src/services/local-file-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAKyB;AACzB,0CAAmB;AACnB,6BAA4B;AAE5B;IAA2B,gCAAmB;IAI5C,sBAAY,EAAE,EAAE,OAAO;QAAvB,YACE,kBAAM,EAAE,EAAE,OAAO,CAAC,SAInB;QAFC,KAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,gBAAgB,CAAA;QACxD,KAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,uBAAuB,CAAA;;IACnE,CAAC;IAEK,6BAAM,GAAZ,UAAa,IAAyB;;;;4BAC7B,qBAAM,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAA;4BAAlC,sBAAO,SAA2B,EAAA;;;;KACnC;IAEK,sCAAe,GAArB,UAAsB,IAAyB;;;;4BACtC,qBAAM,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,EAAA;4BAAtC,sBAAO,SAA+B,EAAA;;;;KACvC;IAEK,iCAAU,GAAhB,UACE,IAAyB,EACzB,OAAY;QAAZ,wBAAA,EAAA,YAAY;;;;;gBAEN,cAAc,GAAG,IAAA,YAAK,EAAC,IAAI,CAAC,YAAY,CAAC,CAAA;gBAEzC,OAAO,GAAG,UAAG,cAAc,CAAC,IAAI,cAAI,IAAI,CAAC,GAAG,EAAE,SAAG,cAAc,CAAC,GAAG,CAAE,CAAA;gBAE3E,sBAAO,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;wBACjC,YAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,UAAG,KAAI,CAAC,UAAU,cAAI,OAAO,CAAE,EAAE,UAAC,GAAG;4BAC1D,IAAI,GAAG,EAAE;gCACP,MAAM,GAAG,CAAA;6BACV;4BAED,IAAM,OAAO,GAAG,UAAG,KAAI,CAAC,WAAW,cAAI,KAAI,CAAC,UAAU,cAAI,OAAO,CAAE,CAAA;4BAEnE,OAAO,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAA;wBAC3B,CAAC,CAAC,CAAA;oBACJ,CAAC,CAAC,EAAA;;;KACH;IAEK,6BAAM,GAAZ,UAAa,IAAI;;;gBACf,MAAM,KAAK,CAAC,iBAAiB,CAAC,CAAA;;;KAC/B;IAEK,gDAAyB,GAA/B,UACE,QAAQ;;;gBAER,MAAM,KAAK,CAAC,iBAAiB,CAAC,CAAA;;;KAC/B;IAEK,wCAAiB,GAAvB,UAAwB,QAAQ;;;gBAC9B,MAAM,KAAK,CAAC,iBAAiB,CAAC,CAAA;;;KAC/B;IAEK,8CAAuB,GAA7B,UAA8B,QAAQ;;;gBACpC,MAAM,KAAK,CAAC,iBAAiB,CAAC,CAAA;;;KAC/B;IACH,mBAAC;AAAD,CAAC,AAzDD,CAA2B,4BAAmB,GAyD7C;AAED,kBAAe,YAAY,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@medusajs/file-local",
|
|
3
|
+
"version": "1.0.1-next-20230529135657",
|
|
4
|
+
"description": "Local file plugin for Medusa",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/medusajs/medusa",
|
|
12
|
+
"directory": "packages/medusa-file-local"
|
|
13
|
+
},
|
|
14
|
+
"author": "Medusa",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@medusajs/medusa": "1.11.1-next-20230529135657",
|
|
18
|
+
"cross-env": "^5.2.1",
|
|
19
|
+
"jest": "^25.5.4",
|
|
20
|
+
"typescript": "^4.9.5"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"prepare": "cross-env NODE_ENV=production yarn run build",
|
|
24
|
+
"test": "jest --passWithNoTests src",
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"watch": "tsc --watch"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"medusa-interfaces": "^1.3.7"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "81a7ff73d012fda722f6e9ef0bd9ba0232d37808",
|
|
32
|
+
"keywords": [
|
|
33
|
+
"medusa-plugin",
|
|
34
|
+
"medusa-plugin-file"
|
|
35
|
+
]
|
|
36
|
+
}
|