@jackdbd/r2-media-store
Version:
Store Micropub media in a Cloudflare R2 bucket.
41 lines • 1.94 kB
JavaScript
import { DeleteObjectCommand } from '@aws-sdk/client-s3';
import { defaultLog } from './log.js';
const defaults = {
log: defaultLog
};
export const defHardDeleteMedia = (options) => {
const config = Object.assign({}, defaults, options);
const { bucket_name, bucket_prefix, log, s3 } = config;
const hardDeleteMedia = async (url) => {
// TODO: extract this to a separate function, so it can be easily tested
const splits = url.split('/');
const filename = splits.at(-1);
const bucket_path = `${bucket_prefix}${filename}`;
const params = {
Bucket: bucket_name,
Key: bucket_path
};
log.debug(`deleting ${filename} from bucket ${bucket_name} at ${bucket_path}`);
// TODO: how do we know if the operation succeeded? From output.$metadata?
const output = await s3.send(new DeleteObjectCommand(params));
// The output is the same, whether the object existed and was deleted, or
// if it didn't exist in the first place.
// const { VersionId: version_id, $metadata: meta } = output;
// const status_code = meta.httpStatusCode || 204;
// const status_text = status_code === 204 ? "No Content" : "Success";
const { VersionId: version_id } = output;
const details = [];
if (version_id) {
details.push(`The file that was hosted on Cloudflare R2 bucket ${bucket_name} at ${bucket_path} (Version ID: ${version_id}) is no longer available at ${url}.`);
}
else {
details.push(`The file that was hosted on Cloudflare R2 bucket ${bucket_name} at ${bucket_path} is no longer available at ${url}.`);
}
const summary = `Deleted ${url} (hard-delete)`;
log.debug(summary);
details.forEach(log.debug);
return { summary, details };
};
return hardDeleteMedia;
};
//# sourceMappingURL=delete.js.map