payload-s3-upload
Version:
Send Payload CMS uploads to Amazon S3
50 lines • 2.31 kB
JavaScript
;
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const client_s3_1 = require("@aws-sdk/client-s3");
const getFilesToDelete = (afterDeleteOptions) => {
var _a;
const { doc } = afterDeleteOptions;
const files = [doc.filename];
if (((_a = doc.mimeType) === null || _a === void 0 ? void 0 : _a.includes('image')) && doc.sizes != null) {
Object.values(doc.sizes).forEach((fileData) => {
if (fileData.filename != null)
files.push(fileData.filename);
});
}
return files;
};
const buildDeleteHook = (s3Client, collection) => {
const { s3 } = collection.upload;
const deleteHook = (afterDeleteOptions) => __awaiter(void 0, void 0, void 0, function* () {
const filenames = getFilesToDelete(afterDeleteOptions);
for (const filename of filenames) {
let key = filename;
if (s3.prefix) {
key =
s3.prefix instanceof Function
? path_1.default.posix.join(s3.prefix({ doc: afterDeleteOptions.doc }), key)
: path_1.default.posix.join(s3.prefix, key);
}
yield s3Client.send(new client_s3_1.DeleteObjectCommand({
Bucket: s3.bucket,
Key: key,
}));
}
});
return deleteHook;
};
exports.default = buildDeleteHook;
//# sourceMappingURL=buildDeleteHook.js.map