UNPKG

medusa-file-cloudinary

Version:
142 lines (105 loc) 6.68 kB
"use strict"; function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _fs = _interopRequireDefault(require("fs")); var _cloudinary = require("cloudinary"); var _medusaInterfaces = require("medusa-interfaces"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure undefined"); } function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var CloudinaryService = /*#__PURE__*/function (_FileService) { _inherits(CloudinaryService, _FileService); var _super = _createSuper(CloudinaryService); function CloudinaryService(_ref, options) { var _this; _objectDestructuringEmpty(_ref); _classCallCheck(this, CloudinaryService); _this = _super.call(this); _this.root_ = options.root_folder; _this.nameToPath_ = options.use_file_name_as_path || false; _this.nonPublicIdSlashCount_ = 7; _cloudinary.v2.config({ cloud_name: options.cloud_name, api_key: options.api_key, api_secret: options.api_secret, secure: options.secure || true }); return _this; } // File upload _createClass(CloudinaryService, [{ key: "upload", value: function upload(file) { var _this2 = this; var publicId = this.buildPublicId(file.originalname); return new Promise(function (resolve, reject) { var upload_stream = _cloudinary.v2.uploader.upload_stream({ folder: _this2.root_, public_id: publicId }, function (err, image) { if (err) { console.error(err); reject(err); return; } console.log(image); resolve({ url: image.url }); }); _fs["default"].createReadStream(file.path).pipe(upload_stream); }); } }, { key: "delete", value: function _delete(file) { // file is the url of image. We have to extract the public id from url var publicId; if (typeof file === "string" && file.toLowerCase().includes("cloudinary")) { publicId = this.extractPublicId(file); } else { publicId = file; } _cloudinary.v2.uploader.destroy(publicId, function (result) { resolve(result); }); } /* ------------------------------ helper methods ------------------------------ */ }, { key: "buildPublicId", value: function buildPublicId(originalFileName) { var fileName = this.removeExtension(originalFileName); var cleanFileName = fileName.replace(/\s+/g, "-"); // convert ' ' to '-' var filePath = this.nameToPath_ ? cleanFileName.split(".").join("/") : cleanFileName; var uniqueSuffix = Date.now(); return "".concat(filePath, "_").concat(uniqueSuffix); } }, { key: "extractPublicId", value: function extractPublicId(url) { // example: https://res.cloudinary.com/<cloud_name>/image/upload/v1676396191/store/file-name_1676396190050.png var cUrl = this.removeExtension(url); return cUrl.split("/").slice(this.nonPublicIdSlashCount_).join("/"); } }, { key: "removeExtension", value: function removeExtension(name) { return name.split(".").slice(0, -1).join("."); } }]); return CloudinaryService; }(_medusaInterfaces.FileService); var _default = CloudinaryService; exports["default"] = _default;