support folder upload for b2 service

This commit is contained in:
SrGooglo 2025-01-25 19:44:22 +00:00
parent 728e9eea2d
commit 7b6d32a8a9

View File

@ -1,23 +1,58 @@
import fs from "node:fs"
import path from "node:path"
import pMap from "p-map"
export default async ({
export default async function b2Upload({
source,
remotePath,
metadata = {},
targetFilename,
isDirectory,
}) => {
// use backblaze b2
}) {
if (isDirectory) {
let files = await fs.promises.readdir(source)
files = files.map((file) => {
const filePath = path.join(source, file)
const isTargetDirectory = fs.lstatSync(filePath).isDirectory()
return {
source: filePath,
remotePath: path.join(remotePath, file),
isDirectory: isTargetDirectory,
}
})
await pMap(
files,
b2Upload,
{
concurrency: 5
}
)
return {
id: remotePath,
url: `https://${process.env.B2_CDN_ENDPOINT}/${process.env.B2_BUCKET}/${remotePath}/${targetFilename}`,
metadata: metadata,
}
}
await global.b2Storage.authorize()
if (!fs.existsSync(source)) {
throw new OperationError(500, "File not found")
}
const uploadUrl = await global.b2Storage.getUploadUrl({
bucketId: process.env.B2_BUCKET_ID,
})
if (!fs.existsSync(source)) {
throw new OperationError(500, "File not found")
}
console.debug(`Uploading object to B2 Storage >`, {
source: source,
remote: remotePath,
})
const data = await fs.promises.readFile(source)
@ -29,11 +64,9 @@ export default async ({
info: metadata
})
const url = `https://${process.env.B2_CDN_ENDPOINT}/${process.env.B2_BUCKET}/${remotePath}`
return {
id: remotePath,
url: url,
url: `https://${process.env.B2_CDN_ENDPOINT}/${process.env.B2_BUCKET}/${remotePath}`,
metadata: metadata,
}
}