mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
support folder upload for b2 service
This commit is contained in:
parent
728e9eea2d
commit
7b6d32a8a9
@ -1,23 +1,58 @@
|
|||||||
import fs from "node:fs"
|
import fs from "node:fs"
|
||||||
import path from "node:path"
|
import path from "node:path"
|
||||||
|
import pMap from "p-map"
|
||||||
|
|
||||||
export default async ({
|
export default async function b2Upload({
|
||||||
source,
|
source,
|
||||||
remotePath,
|
remotePath,
|
||||||
metadata = {},
|
metadata = {},
|
||||||
targetFilename,
|
targetFilename,
|
||||||
isDirectory,
|
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()
|
await global.b2Storage.authorize()
|
||||||
|
|
||||||
|
if (!fs.existsSync(source)) {
|
||||||
|
throw new OperationError(500, "File not found")
|
||||||
|
}
|
||||||
|
|
||||||
const uploadUrl = await global.b2Storage.getUploadUrl({
|
const uploadUrl = await global.b2Storage.getUploadUrl({
|
||||||
bucketId: process.env.B2_BUCKET_ID,
|
bucketId: process.env.B2_BUCKET_ID,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!fs.existsSync(source)) {
|
console.debug(`Uploading object to B2 Storage >`, {
|
||||||
throw new OperationError(500, "File not found")
|
source: source,
|
||||||
}
|
remote: remotePath,
|
||||||
|
})
|
||||||
|
|
||||||
const data = await fs.promises.readFile(source)
|
const data = await fs.promises.readFile(source)
|
||||||
|
|
||||||
@ -29,11 +64,9 @@ export default async ({
|
|||||||
info: metadata
|
info: metadata
|
||||||
})
|
})
|
||||||
|
|
||||||
const url = `https://${process.env.B2_CDN_ENDPOINT}/${process.env.B2_BUCKET}/${remotePath}`
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: remotePath,
|
id: remotePath,
|
||||||
url: url,
|
url: `https://${process.env.B2_CDN_ENDPOINT}/${process.env.B2_BUCKET}/${remotePath}`,
|
||||||
metadata: metadata,
|
metadata: metadata,
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user