2024-12-03 19:33:43 +00:00
|
|
|
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,
|
2025-11-23 19:24:33 +00:00
|
|
|
bucketName: keys.videos[0].bucket
|
2024-12-03 19:33:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const images3 = new S3({
|
|
|
|
|
endpoint: keys.endpoint,
|
|
|
|
|
accessKeyId: keys.images[0].access,
|
|
|
|
|
secretAccessKey: keys.images[0].secret,
|
2025-11-23 19:24:33 +00:00
|
|
|
bucketName: keys.images[0].bucket
|
2024-12-03 19:33:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function uploadVideo(video: string) {
|
|
|
|
|
const videoFile = fs.readFileSync(video)
|
|
|
|
|
const uploaded = await videos3.put(video.split('/')[2], videoFile)
|
2025-11-11 16:29:44 +00:00
|
|
|
return uploaded.url.replace(keys.endpoint, 'https://s4.archive.party')
|
2024-12-03 19:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function uploadImage(id: string, url: string) {
|
|
|
|
|
const response = await fetch(url)
|
|
|
|
|
const buffer = Buffer.from(await response.arrayBuffer())
|
2025-11-16 10:04:38 +00:00
|
|
|
const bufferHash = Bun.hash(buffer).toString()
|
|
|
|
|
|
|
|
|
|
const exists = await images3.fileExists(`${id}-${bufferHash}.webp`)
|
|
|
|
|
if (exists) return `${keys.images[0].url}${id}-${bufferHash}.webp`
|
2024-12-03 19:33:43 +00:00
|
|
|
|
2025-11-16 10:04:38 +00:00
|
|
|
const uploaded = await images3.put(`${id}-${bufferHash}.webp`, buffer)
|
2025-11-11 16:29:44 +00:00
|
|
|
return uploaded.url.replace(keys.endpoint, 'https://s4.archive.party')
|
2024-12-03 19:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { uploadVideo, uploadImage }
|