UNPKG

medusa-file-minio

Version:

MinIO server file connector for Medusa

276 lines 14.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { 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; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var medusa_1 = require("@medusajs/medusa"); var medusa_core_utils_1 = require("medusa-core-utils"); var aws_sdk_1 = __importDefault(require("aws-sdk")); var fs_1 = __importDefault(require("fs")); var path_1 = require("path"); var stream_1 = __importDefault(require("stream")); var MinioService = /** @class */ (function (_super) { __extends(MinioService, _super); function MinioService(_a, options) { var _this = this; var _b, _c, _d; _this = _super.call(this, arguments[0], options) || this; _this.bucket_ = options.bucket; _this.accessKeyId_ = options.access_key_id; _this.secretAccessKey_ = options.secret_access_key; _this.private_bucket_ = options.private_bucket; _this.private_access_key_id_ = (_b = options.private_access_key_id) !== null && _b !== void 0 ? _b : _this.accessKeyId_; _this.private_secret_access_key_ = (_c = options.private_secret_access_key) !== null && _c !== void 0 ? _c : _this.secretAccessKey_; _this.endpoint_ = options.endpoint; _this.s3ForcePathStyle_ = true; _this.signatureVersion_ = "v4"; _this.downloadUrlDuration = (_d = options.download_url_duration) !== null && _d !== void 0 ? _d : 60; // 60 seconds return _this; } MinioService.prototype.buildUrl = function (bucket, key) { return "".concat(this.endpoint_, "/").concat(bucket, "/").concat(key); }; MinioService.prototype.upload = function (file) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.uploadFile(file)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; MinioService.prototype.uploadProtected = function (file) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: this.validatePrivateBucketConfiguration_(true); return [4 /*yield*/, this.uploadFile(file, { isProtected: true })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; MinioService.prototype.uploadFile = function (file, options) { if (options === void 0) { options = { isProtected: false }; } return __awaiter(this, void 0, void 0, function () { var parsedFilename, fileKey, client, params, result; return __generator(this, function (_a) { switch (_a.label) { case 0: parsedFilename = (0, path_1.parse)(file.originalname); fileKey = "".concat(parsedFilename.name, "-").concat(Date.now()).concat(parsedFilename.ext); client = this.getClient(options.isProtected); params = { ACL: options.isProtected ? "private" : "public-read", Bucket: options.isProtected ? this.private_bucket_ : this.bucket_, Body: fs_1.default.createReadStream(file.path), Key: fileKey, ContentType: file.mimetype, }; return [4 /*yield*/, client.upload(params).promise()]; case 1: result = _a.sent(); return [2 /*return*/, { url: result.Location, key: result.Key }]; } }); }); }; MinioService.prototype.delete = function (file) { return __awaiter(this, void 0, void 0, function () { var privateClient, publicClient, params; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: privateClient = this.getClient(false); publicClient = this.getClient(true); params = { Bucket: this.bucket_, Key: "".concat(file.fileKey), }; return [4 /*yield*/, Promise.all([ new Promise(function (resolve, reject) { return publicClient.deleteObject(__assign(__assign({}, params), { Bucket: _this.bucket_ }), function (err, data) { if (err) { reject(err); return; } resolve(data); }); }), new Promise(function (resolve, reject) { return privateClient.deleteObject(__assign(__assign({}, params), { Bucket: _this.private_bucket_ }), function (err, data) { if (err) { reject(err); return; } resolve(data); }); }), ])]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; MinioService.prototype.getUploadStreamDescriptor = function (fileData) { var _a; return __awaiter(this, void 0, void 0, function () { var usePrivateBucket, client, pass, fileKey, params; return __generator(this, function (_b) { usePrivateBucket = (_a = fileData.isPrivate) !== null && _a !== void 0 ? _a : true; this.validatePrivateBucketConfiguration_(usePrivateBucket); client = this.getClient(usePrivateBucket); pass = new stream_1.default.PassThrough(); fileKey = "".concat(fileData.name, ".").concat(fileData.ext); params = { Bucket: usePrivateBucket ? this.private_bucket_ : this.bucket_, Body: pass, Key: fileKey, ContentType: fileData.contentType, }; return [2 /*return*/, { writeStream: pass, promise: client.upload(params).promise(), url: this.buildUrl(params.Bucket, fileKey), fileKey: fileKey, }]; }); }); }; MinioService.prototype.getDownloadStream = function (fileData) { var _a; return __awaiter(this, void 0, void 0, function () { var usePrivateBucket, client, params; return __generator(this, function (_b) { usePrivateBucket = (_a = fileData.isPrivate) !== null && _a !== void 0 ? _a : true; this.validatePrivateBucketConfiguration_(usePrivateBucket); client = this.getClient(usePrivateBucket); params = { Bucket: usePrivateBucket ? this.private_bucket_ : this.bucket_, Key: "".concat(fileData.fileKey), }; return [2 /*return*/, client.getObject(params).createReadStream()]; }); }); }; MinioService.prototype.getPresignedDownloadUrl = function (_a) { var _b = _a.isPrivate, isPrivate = _b === void 0 ? true : _b, fileData = __rest(_a, ["isPrivate"]); return __awaiter(this, void 0, void 0, function () { var client, params; return __generator(this, function (_c) { switch (_c.label) { case 0: this.validatePrivateBucketConfiguration_(isPrivate); client = this.getClient(isPrivate, { signatureVersion: "v4", }); params = { Bucket: isPrivate ? this.private_bucket_ : this.bucket_, Key: "".concat(fileData.fileKey), Expires: this.downloadUrlDuration, }; return [4 /*yield*/, client.getSignedUrlPromise("getObject", params)]; case 1: return [2 /*return*/, _c.sent()]; } }); }); }; MinioService.prototype.validatePrivateBucketConfiguration_ = function (usePrivateBucket) { if (usePrivateBucket && (!this.private_access_key_id_ || !this.private_bucket_)) { throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Private bucket is not configured"); } }; MinioService.prototype.getClient = function (usePrivateBucket, additionalConfiguration) { if (usePrivateBucket === void 0) { usePrivateBucket = false; } if (additionalConfiguration === void 0) { additionalConfiguration = {}; } return new aws_sdk_1.default.S3(__assign({ accessKeyId: usePrivateBucket ? this.private_access_key_id_ : this.accessKeyId_, secretAccessKey: usePrivateBucket ? this.private_secret_access_key_ : this.secretAccessKey_, endpoint: this.endpoint_, s3ForcePathStyle: this.s3ForcePathStyle_, signatureVersion: this.signatureVersion_ }, additionalConfiguration)); }; return MinioService; }(medusa_1.AbstractFileService)); exports.default = MinioService; //# sourceMappingURL=minio.js.map