@medusajs/file-local-next 0.0.3-snapshot-20240426112214 → 0.0.3-snapshot-20240520151134
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +5 -5
- package/dist/services/local-file.js +65 -169
- package/package.json +2 -2
package/dist/index.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
services
|
3
|
+
const local_file_1 = require("./services/local-file");
|
4
|
+
const services = [local_file_1.LocalFileService];
|
5
|
+
const providerExport = {
|
6
|
+
services,
|
7
7
|
};
|
8
8
|
exports.default = providerExport;
|
9
|
-
//# sourceMappingURL=data:application/json;base64,
|
9
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFDQSxzREFBd0Q7QUFFeEQsTUFBTSxRQUFRLEdBQUcsQ0FBQyw2QkFBZ0IsQ0FBQyxDQUFBO0FBRW5DLE1BQU0sY0FBYyxHQUEwQjtJQUM1QyxRQUFRO0NBQ1QsQ0FBQTtBQUVELGtCQUFlLGNBQWMsQ0FBQSJ9
|
@@ -1,179 +1,75 @@
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
54
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
55
4
|
};
|
56
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
57
6
|
exports.LocalFileService = void 0;
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
7
|
+
const utils_1 = require("@medusajs/utils");
|
8
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
9
|
+
const path_1 = __importDefault(require("path"));
|
10
|
+
class LocalFileService extends utils_1.AbstractFileProviderService {
|
11
|
+
constructor(_, options) {
|
12
|
+
super();
|
13
|
+
this.getUploadFilePath = (fileKey) => {
|
14
|
+
return path_1.default.join(this.uploadDir_, fileKey);
|
15
|
+
};
|
16
|
+
this.getUploadFileUrl = (fileKey) => {
|
17
|
+
return path_1.default.join(this.backendUrl_, this.getUploadFilePath(fileKey));
|
67
18
|
};
|
68
|
-
|
69
|
-
|
19
|
+
this.uploadDir_ = options?.upload_dir || "uploads";
|
20
|
+
this.backendUrl_ = options?.backend_url || "http://localhost:9000";
|
21
|
+
}
|
22
|
+
async upload(file) {
|
23
|
+
if (!file) {
|
24
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `No file provided`);
|
25
|
+
}
|
26
|
+
if (!file.filename) {
|
27
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `No filename provided`);
|
28
|
+
}
|
29
|
+
const parsedFilename = path_1.default.parse(file.filename);
|
30
|
+
if (parsedFilename.dir) {
|
31
|
+
this.ensureDirExists(parsedFilename.dir);
|
32
|
+
}
|
33
|
+
const fileKey = path_1.default.join(parsedFilename.dir, `${Date.now()}-${parsedFilename.base}`);
|
34
|
+
const filePath = this.getUploadFilePath(fileKey);
|
35
|
+
const fileUrl = this.getUploadFileUrl(fileKey);
|
36
|
+
const content = Buffer.from(file.content, "binary");
|
37
|
+
await promises_1.default.writeFile(filePath, content);
|
38
|
+
return {
|
39
|
+
key: fileKey,
|
40
|
+
url: fileUrl,
|
70
41
|
};
|
71
|
-
_this.uploadDir_ = (options === null || options === void 0 ? void 0 : options.upload_dir) || "uploads";
|
72
|
-
_this.backendUrl_ = (options === null || options === void 0 ? void 0 : options.backend_url) || "http://localhost:9000";
|
73
|
-
return _this;
|
74
42
|
}
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
};
|
106
|
-
LocalFileService.prototype.delete = function (file) {
|
107
|
-
return __awaiter(this, void 0, void 0, function () {
|
108
|
-
var filePath, e_1;
|
109
|
-
return __generator(this, function (_a) {
|
110
|
-
switch (_a.label) {
|
111
|
-
case 0:
|
112
|
-
filePath = this.getUploadFilePath(file.fileKey);
|
113
|
-
_a.label = 1;
|
114
|
-
case 1:
|
115
|
-
_a.trys.push([1, 4, , 5]);
|
116
|
-
return [4 /*yield*/, promises_1.default.access(filePath, promises_1.default.constants.F_OK)];
|
117
|
-
case 2:
|
118
|
-
_a.sent();
|
119
|
-
return [4 /*yield*/, promises_1.default.unlink(filePath)];
|
120
|
-
case 3:
|
121
|
-
_a.sent();
|
122
|
-
return [3 /*break*/, 5];
|
123
|
-
case 4:
|
124
|
-
e_1 = _a.sent();
|
125
|
-
return [3 /*break*/, 5];
|
126
|
-
case 5: return [2 /*return*/];
|
127
|
-
}
|
128
|
-
});
|
129
|
-
});
|
130
|
-
};
|
131
|
-
LocalFileService.prototype.getPresignedDownloadUrl = function (fileData) {
|
132
|
-
return __awaiter(this, void 0, void 0, function () {
|
133
|
-
var _a;
|
134
|
-
return __generator(this, function (_b) {
|
135
|
-
switch (_b.label) {
|
136
|
-
case 0:
|
137
|
-
_b.trys.push([0, 2, , 3]);
|
138
|
-
return [4 /*yield*/, promises_1.default.access(this.getUploadFilePath(fileData.fileKey), promises_1.default.constants.F_OK)];
|
139
|
-
case 1:
|
140
|
-
_b.sent();
|
141
|
-
return [3 /*break*/, 3];
|
142
|
-
case 2:
|
143
|
-
_a = _b.sent();
|
144
|
-
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, "File with key ".concat(fileData.fileKey, " not found"));
|
145
|
-
case 3: return [2 /*return*/, this.getUploadFileUrl(fileData.fileKey)];
|
146
|
-
}
|
147
|
-
});
|
148
|
-
});
|
149
|
-
};
|
150
|
-
LocalFileService.prototype.ensureDirExists = function (dirPath) {
|
151
|
-
return __awaiter(this, void 0, void 0, function () {
|
152
|
-
var relativePath, e_2;
|
153
|
-
return __generator(this, function (_a) {
|
154
|
-
switch (_a.label) {
|
155
|
-
case 0:
|
156
|
-
relativePath = path_1.default.join(this.uploadDir_, dirPath);
|
157
|
-
_a.label = 1;
|
158
|
-
case 1:
|
159
|
-
_a.trys.push([1, 3, , 5]);
|
160
|
-
return [4 /*yield*/, promises_1.default.access(relativePath, promises_1.default.constants.F_OK)];
|
161
|
-
case 2:
|
162
|
-
_a.sent();
|
163
|
-
return [3 /*break*/, 5];
|
164
|
-
case 3:
|
165
|
-
e_2 = _a.sent();
|
166
|
-
return [4 /*yield*/, promises_1.default.mkdir(relativePath, { recursive: true })];
|
167
|
-
case 4:
|
168
|
-
_a.sent();
|
169
|
-
return [3 /*break*/, 5];
|
170
|
-
case 5: return [2 /*return*/];
|
171
|
-
}
|
172
|
-
});
|
173
|
-
});
|
174
|
-
};
|
175
|
-
LocalFileService.identifier = "localfs";
|
176
|
-
return LocalFileService;
|
177
|
-
}(utils_1.AbstractFileProviderService));
|
43
|
+
async delete(file) {
|
44
|
+
const filePath = this.getUploadFilePath(file.fileKey);
|
45
|
+
try {
|
46
|
+
await promises_1.default.access(filePath, promises_1.default.constants.F_OK);
|
47
|
+
await promises_1.default.unlink(filePath);
|
48
|
+
}
|
49
|
+
catch (e) {
|
50
|
+
// The file does not exist, so it's a noop.
|
51
|
+
}
|
52
|
+
return;
|
53
|
+
}
|
54
|
+
async getPresignedDownloadUrl(fileData) {
|
55
|
+
try {
|
56
|
+
await promises_1.default.access(this.getUploadFilePath(fileData.fileKey), promises_1.default.constants.F_OK);
|
57
|
+
}
|
58
|
+
catch {
|
59
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `File with key ${fileData.fileKey} not found`);
|
60
|
+
}
|
61
|
+
return this.getUploadFileUrl(fileData.fileKey);
|
62
|
+
}
|
63
|
+
async ensureDirExists(dirPath) {
|
64
|
+
const relativePath = path_1.default.join(this.uploadDir_, dirPath);
|
65
|
+
try {
|
66
|
+
await promises_1.default.access(relativePath, promises_1.default.constants.F_OK);
|
67
|
+
}
|
68
|
+
catch (e) {
|
69
|
+
await promises_1.default.mkdir(relativePath, { recursive: true });
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|
178
73
|
exports.LocalFileService = LocalFileService;
|
179
|
-
|
74
|
+
LocalFileService.identifier = "localfs";
|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2pEO1FBQUMsT0FBTyxDQUFDLEVBQUU7WUFDVixNQUFNLGtCQUFFLENBQUMsS0FBSyxDQUFDLFlBQVksRUFBRSxFQUFFLFNBQVMsRUFBRSxJQUFJLEVBQUUsQ0FBQyxDQUFBO1NBQ2xEO0lBQ0gsQ0FBQzs7QUE3RkgsNENBOEZDO0FBN0ZRLDJCQUFVLEdBQUcsU0FBUyxDQUFBIn0=
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@medusajs/file-local-next",
|
3
|
-
"version": "0.0.3-snapshot-
|
3
|
+
"version": "0.0.3-snapshot-20240520151134",
|
4
4
|
"description": "Local filesystem file storage for Medusa",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"repository": {
|
@@ -29,7 +29,7 @@
|
|
29
29
|
"typescript": "^4.9.5"
|
30
30
|
},
|
31
31
|
"dependencies": {
|
32
|
-
"@medusajs/utils": "1.12.0-snapshot-
|
32
|
+
"@medusajs/utils": "1.12.0-snapshot-20240520151134"
|
33
33
|
},
|
34
34
|
"keywords": [
|
35
35
|
"medusa-plugin",
|