sanity-plugin-r2-files
Version:
Store Sanity media files in Cloudflare R2 Bucket
61 lines (60 loc) • 1.8 kB
JavaScript
const uploadFile = ({ credentials, onError, onSuccess, file, fileName }) => {
if (!credentials || typeof credentials.workerUrl !== 'string') {
onError({
name: 'missing-credentials',
message: 'Missing correct credentials',
});
}
const filePath = [credentials.folder, fileName]
.filter(Boolean)
.join('/')
.replace(/\s/g, '-');
const endpoint = credentials.workerUrl;
const url = `${endpoint}/${filePath}`;
const authToken = credentials.secret;
// On cancelling fetch: https://davidwalsh.name/cancel-fetch
let signal;
let controller;
try {
controller = new AbortController();
signal = controller.signal;
}
catch (error) { }
// Upload file to Cloudflare R2
// By sending a PUT request to the Cloudflare Worker
fetch(url, {
method: 'PUT',
headers: {
Authorization: `Bearer ${authToken}`,
'Content-Type': file.type,
},
body: file,
mode: 'cors',
signal,
}).then((response) => {
if (response.ok) {
onSuccess({
fileURL: `${credentials.url}/${filePath}`,
cloudflareR2: {
fileKey: filePath,
baseUrl: credentials.url,
},
});
}
else {
onError({
message: 'Ask your developer to check Cloudflare R2 permissions.',
name: 'failed-presigned',
});
}
});
return () => {
try {
if (controller === null || controller === void 0 ? void 0 : controller.abort) {
controller.abort();
}
}
catch (error) { }
};
};
export default uploadFile;