backend/src/utils/upload.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

import * as fs from 'node:fs'
const keys = JSON.parse(fs.readFileSync('s3.json', 'utf-8'))
async function uploadVideo(video: string) {
2026-01-07 19:48:04 +00:00
const uploaded = await fetch(`${keys.endpoint}/${video.split('/')[2]}`, {
method: 'PUT',
headers: {
'x-authtoken': keys.videos[0].secret
},
body: await Bun.file(video).arrayBuffer()
})
2025-11-11 16:29:44 +00:00
return uploaded.url.replace(keys.endpoint, 'https://s4.archive.party')
}
async function uploadImage(id: string, url: string) {
const response = await fetch(url)
2026-01-07 19:48:04 +00:00
const arrayBuffer = await response.arrayBuffer()
const bufferHash = Bun.hash(Buffer.from(arrayBuffer)).toString()
2025-11-16 10:04:38 +00:00
2026-01-07 19:48:04 +00:00
const exists = await fetch(`${keys.endpoint}/${id}-${bufferHash}.webp`, {
method: 'HEAD',
headers: {
'x-authtoken': keys.videos[0].secret
}
})
if (exists.status == 200) return `${keys.images[0].url}${id}-${bufferHash}.webp`
2026-01-07 19:48:04 +00:00
const uploaded = await fetch(`${keys.endpoint}/${id}-${bufferHash}.webp`, {
method: 'PUT',
headers: {
'x-authtoken': keys.videos[0].secret
},
body: await response.arrayBuffer()
})
2025-11-11 16:29:44 +00:00
return uploaded.url.replace(keys.endpoint, 'https://s4.archive.party')
}
export { uploadVideo, uploadImage }