Use R2 for my data view
This commit is contained in:
parent
8af6678619
commit
9301d6e368
@ -19,57 +19,28 @@ export async function onRequestGet(context: RequestContext) {
|
|||||||
return jsonError("Item does not exist", 404);
|
return jsonError("Item does not exist", 404);
|
||||||
|
|
||||||
if (type === "report") {
|
if (type === "report") {
|
||||||
let unsignedUrls = [];
|
const { AwsClient } = await import("aws4fetch");
|
||||||
const exp = Math.round(Date.now() / 1000) + 1800;
|
const aws = new AwsClient({
|
||||||
|
accessKeyId: context.env.R2_ACCESS_KEY,
|
||||||
|
secretAccessKey: context.env.R2_SECRET_KEY,
|
||||||
|
});
|
||||||
|
|
||||||
for (const attachment of data.attachments)
|
let urls = [];
|
||||||
unsignedUrls.push(
|
|
||||||
`https://mediaproxy.carcrushers.cc/${attachment}?Expires=${exp}&KeyName=portal-media-linkgen`,
|
for (const attachment of data.attachments) {
|
||||||
|
const { url } = await aws.sign(
|
||||||
|
`https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com/${attachment}?X-Amz-Expires=1800`,
|
||||||
|
{
|
||||||
|
aws: {
|
||||||
|
signQuery: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let resolvedUrls = [];
|
urls.push(url);
|
||||||
let signingPromises = [];
|
|
||||||
const key = await crypto.subtle.importKey(
|
|
||||||
"raw",
|
|
||||||
Uint8Array.from(atob(context.env.URL_SIGNING_KEY), (c) =>
|
|
||||||
c.charCodeAt(0),
|
|
||||||
),
|
|
||||||
{ hash: "SHA-1", name: "HMAC" },
|
|
||||||
false,
|
|
||||||
["sign"],
|
|
||||||
);
|
|
||||||
|
|
||||||
for (let i = 0; i < unsignedUrls.length; i++)
|
|
||||||
signingPromises.push(
|
|
||||||
crypto.subtle.sign(
|
|
||||||
"HMAC",
|
|
||||||
key,
|
|
||||||
new TextEncoder().encode(unsignedUrls[i]),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
let signatures: ArrayBuffer[];
|
|
||||||
|
|
||||||
try {
|
|
||||||
signatures = await Promise.all(signingPromises);
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e);
|
|
||||||
|
|
||||||
return jsonError("Failed to create signed links", 500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < signatures.length; i++) {
|
data.resolved_attachments = urls;
|
||||||
resolvedUrls.push(
|
|
||||||
`${unsignedUrls[i]}&Signature=${btoa(
|
|
||||||
String.fromCharCode(...new Uint8Array(signatures[i])),
|
|
||||||
)
|
|
||||||
.replaceAll("+", "-")
|
|
||||||
.replaceAll("/", "_")
|
|
||||||
.replaceAll("=", "")}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
data.resolved_attachments = resolvedUrls;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return jsonResponse(JSON.stringify(data));
|
return jsonResponse(JSON.stringify(data));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user