@agilo/medusa-file-r2
Version:
Cloudflare R2 storage plugin for Medusa
174 lines (173 loc) • 6.3 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var r2_exports = {};
__export(r2_exports, {
default: () => r2_default
});
module.exports = __toCommonJS(r2_exports);
var import_fs = __toESM(require("fs"));
var import_stream = __toESM(require("stream"));
var import_path = __toESM(require("path"));
var import_s3 = __toESM(require("aws-sdk/clients/s3.js"));
var import_medusa = require("@medusajs/medusa");
class R2StorageService extends import_medusa.AbstractFileService {
bucket;
endpoint;
access_key;
secret_key;
public_url;
cache_control;
presigned_url_expires;
constructor(container, options) {
super(container, options);
if (typeof options.bucket !== "string" || !options.bucket) {
throw new Error("R2StorageService requires a bucket");
}
if (typeof options.endpoint !== "string" || !options.endpoint) {
throw new Error("R2StorageService requires an endpoint");
}
if (typeof options.access_key !== "string" || !options.access_key) {
throw new Error("R2StorageService requires an access_key");
}
if (typeof options.secret_key !== "string" || !options.secret_key) {
throw new Error("R2StorageService requires a secret_key");
}
if (typeof options.public_url !== "string" || !options.public_url) {
throw new Error("R2StorageService requires a public_url");
}
this.bucket = options.bucket;
this.endpoint = options.endpoint;
this.access_key = options.access_key;
this.secret_key = options.secret_key;
this.public_url = options.public_url;
this.cache_control = typeof options.cache_control === "string" ? options.cache_control : "max-age=31536000";
this.presigned_url_expires = typeof options.presigned_url_expires === "number" ? options.presigned_url_expires : 60 * 60;
}
storageClient() {
const client = new import_s3.default({
region: "auto",
signatureVersion: "v4",
endpoint: this.endpoint,
accessKeyId: this.access_key,
secretAccessKey: this.secret_key
});
return client;
}
async uploadFile(fileData, isPrivate) {
const client = this.storageClient();
const parsedFilename = import_path.default.parse(fileData.originalname);
const timestamp = Date.now();
const fileKey = `${parsedFilename.name}-${timestamp}${parsedFilename.ext}`;
const encodedFileKey = `${encodeURIComponent(parsedFilename.name)}-${timestamp}${parsedFilename.ext}`;
const params = {
ACL: isPrivate ? "private" : "public-read",
Bucket: this.bucket,
Key: fileKey,
Body: import_fs.default.createReadStream(fileData.path),
ContentType: fileData.mimetype,
CacheControl: this.cache_control
};
try {
const data = await client.upload(params).promise();
return {
url: `${this.public_url}/${encodedFileKey}`,
key: data.Key
};
} catch (err) {
console.error(err);
throw new Error("An error occurred while uploading the file.");
}
}
async upload(fileData) {
return this.uploadFile(fileData);
}
async uploadProtected(fileData) {
return this.uploadFile(fileData, true);
}
async delete(fileData) {
const client = this.storageClient();
const params = {
Bucket: this.bucket,
Key: fileData.fileKey
};
try {
await client.deleteObject(params).promise();
} catch (err) {
console.error(err);
throw new Error("An error occurred while deleting the file.");
}
}
async getDownloadStream(fileData) {
const client = this.storageClient();
const params = {
Bucket: this.bucket,
Key: fileData.fileKey
};
try {
return client.getObject(params).createReadStream();
} catch (err) {
console.error(err);
throw new Error("An error occurred while downloading the file.");
}
}
async getPresignedDownloadUrl(fileData) {
const client = this.storageClient();
const params = {
Bucket: this.bucket,
Key: fileData.fileKey,
Expires: this.presigned_url_expires
};
try {
return client.getSignedUrlPromise("getObject", params);
} catch (err) {
console.error(err);
throw new Error("An error occurred while downloading the file.");
}
}
async getUploadStreamDescriptor(fileData) {
const client = this.storageClient();
const pass = new import_stream.default.PassThrough();
const fileKey = `${fileData.name}.${fileData.ext}`;
const isPrivate = fileData.isPrivate ?? true;
const params = {
ACL: isPrivate ? "private" : "public-read",
Bucket: this.bucket,
Body: pass,
Key: fileKey,
ContentType: typeof fileData.contentType === "string" ? fileData.contentType : "application/octet-stream"
};
return {
fileKey,
writeStream: pass,
promise: client.upload(params).promise(),
url: `${this.endpoint}/${this.bucket}/${fileKey}`
};
}
}
var r2_default = R2StorageService;