both sides hash validation for uploads

This commit is contained in:
localhost 2026-07-01 20:01:45 +02:00
parent 3b54b2e13c
commit f6356448cd
1 changed files with 14 additions and 3 deletions

View File

@ -2,12 +2,18 @@ import * as fs from 'node:fs'
const keys = JSON.parse(fs.readFileSync('s3.json', 'utf-8'))
async function uploadVideo(video: string) {
const fileBuffer = await Bun.file(video).arrayBuffer()
const hasher = new Bun.CryptoHasher("sha256");
hasher.update(fileBuffer);
const fileHash = hasher.digest("hex");
const uploaded = await fetch(`${keys.endpoint}/preservetube/${video.split('/')[2]}`, {
method: 'PUT',
headers: {
'x-authtoken': keys.videos[0].secret
'x-authtoken': keys.videos[0].secret,
'x-file-hash': fileHash
},
body: await Bun.file(video).arrayBuffer()
body: fileBuffer
})
if (!uploaded.ok) throw new Error(`failed to upload video - ${uploaded.status} (${uploaded.statusText}) ${await uploaded.text()}`)
return uploaded.url.replace(keys.endpoint, 'https://s5.archive.party')
@ -18,6 +24,10 @@ async function uploadImage(id: string, url: string) {
const arrayBuffer = await response.arrayBuffer()
const bufferHash = Bun.hash(Buffer.from(arrayBuffer)).toString()
const hasher = new Bun.CryptoHasher("sha256");
hasher.update(arrayBuffer);
const fileHash = hasher.digest("hex");
const exists = await fetch(`${keys.endpoint}/preservetube-media/${id}-${bufferHash}.webp`, {
method: 'HEAD',
headers: {
@ -29,7 +39,8 @@ async function uploadImage(id: string, url: string) {
const uploaded = await fetch(`${keys.endpoint}/preservetube-media//${id}-${bufferHash}.webp`, {
method: 'PUT',
headers: {
'x-authtoken': keys.videos[0].secret
'x-authtoken': keys.videos[0].secret,
'x-file-hash': fileHash
},
body: arrayBuffer
})