move away from s3

This commit is contained in:
localhost 2026-01-07 20:48:04 +01:00
parent ce19fea3c0
commit 6491a4b57d
3 changed files with 24 additions and 24 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -27,7 +27,6 @@
"kysely": "^0.27.4",
"pg": "^8.13.1",
"readable-to-ms": "^1.0.3",
"rolling-rate-limiter": "^0.4.2",
"ultralight-s3": "^0.0.7"
"rolling-rate-limiter": "^0.4.2"
}
}

View File

@ -1,36 +1,37 @@
import { S3 } from 'ultralight-s3';
import * as fs from 'node:fs'
const keys = JSON.parse(fs.readFileSync('s3.json', 'utf-8'))
const videos3 = new S3({
endpoint: keys.endpoint,
accessKeyId: keys.videos[0].access,
secretAccessKey: keys.videos[0].secret,
bucketName: keys.videos[0].bucket
})
const images3 = new S3({
endpoint: keys.endpoint,
accessKeyId: keys.images[0].access,
secretAccessKey: keys.images[0].secret,
bucketName: keys.images[0].bucket
});
async function uploadVideo(video: string) {
const videoFile = fs.readFileSync(video)
const uploaded = await videos3.put(video.split('/')[2], videoFile)
const uploaded = await fetch(`${keys.endpoint}/${video.split('/')[2]}`, {
method: 'PUT',
headers: {
'x-authtoken': keys.videos[0].secret
},
body: await Bun.file(video).arrayBuffer()
})
return uploaded.url.replace(keys.endpoint, 'https://s4.archive.party')
}
async function uploadImage(id: string, url: string) {
const response = await fetch(url)
const buffer = Buffer.from(await response.arrayBuffer())
const bufferHash = Bun.hash(buffer).toString()
const arrayBuffer = await response.arrayBuffer()
const bufferHash = Bun.hash(Buffer.from(arrayBuffer)).toString()
const exists = await images3.fileExists(`${id}-${bufferHash}.webp`)
if (exists) return `${keys.images[0].url}${id}-${bufferHash}.webp`
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`
const uploaded = await images3.put(`${id}-${bufferHash}.webp`, buffer)
const uploaded = await fetch(`${keys.endpoint}/${id}-${bufferHash}.webp`, {
method: 'PUT',
headers: {
'x-authtoken': keys.videos[0].secret
},
body: await response.arrayBuffer()
})
return uploaded.url.replace(keys.endpoint, 'https://s4.archive.party')
}