mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
support for upload retry
This commit is contained in:
parent
582c7e389b
commit
744b771d2b
@ -8,6 +8,7 @@ export default async function b2Upload({
|
|||||||
metadata = {},
|
metadata = {},
|
||||||
targetFilename,
|
targetFilename,
|
||||||
isDirectory,
|
isDirectory,
|
||||||
|
retryNumber = 0
|
||||||
}) {
|
}) {
|
||||||
if (isDirectory) {
|
if (isDirectory) {
|
||||||
let files = await fs.promises.readdir(source)
|
let files = await fs.promises.readdir(source)
|
||||||
@ -39,31 +40,48 @@ export default async function b2Upload({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await global.b2Storage.authorize()
|
try {
|
||||||
|
await global.b2Storage.authorize()
|
||||||
|
|
||||||
if (!fs.existsSync(source)) {
|
if (!fs.existsSync(source)) {
|
||||||
throw new OperationError(500, "File not found")
|
throw new OperationError(500, "File not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
const uploadUrl = await global.b2Storage.getUploadUrl({
|
||||||
|
bucketId: process.env.B2_BUCKET_ID,
|
||||||
|
})
|
||||||
|
|
||||||
|
console.debug(`Uploading object to B2 Storage >`, {
|
||||||
|
source: source,
|
||||||
|
remote: remotePath,
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = await fs.promises.readFile(source)
|
||||||
|
|
||||||
|
await global.b2Storage.uploadFile({
|
||||||
|
uploadUrl: uploadUrl.data.uploadUrl,
|
||||||
|
uploadAuthToken: uploadUrl.data.authorizationToken,
|
||||||
|
fileName: remotePath,
|
||||||
|
data: data,
|
||||||
|
info: metadata
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
|
||||||
|
if (retryNumber < 5) {
|
||||||
|
return await b2Upload({
|
||||||
|
source,
|
||||||
|
remotePath,
|
||||||
|
metadata,
|
||||||
|
targetFilename,
|
||||||
|
isDirectory,
|
||||||
|
retryNumber: retryNumber + 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new OperationError(500, "B2 upload failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
const uploadUrl = await global.b2Storage.getUploadUrl({
|
|
||||||
bucketId: process.env.B2_BUCKET_ID,
|
|
||||||
})
|
|
||||||
|
|
||||||
console.debug(`Uploading object to B2 Storage >`, {
|
|
||||||
source: source,
|
|
||||||
remote: remotePath,
|
|
||||||
})
|
|
||||||
|
|
||||||
const data = await fs.promises.readFile(source)
|
|
||||||
|
|
||||||
await global.b2Storage.uploadFile({
|
|
||||||
uploadUrl: uploadUrl.data.uploadUrl,
|
|
||||||
uploadAuthToken: uploadUrl.data.authorizationToken,
|
|
||||||
fileName: remotePath,
|
|
||||||
data: data,
|
|
||||||
info: metadata
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: remotePath,
|
id: remotePath,
|
||||||
url: `https://${process.env.B2_CDN_ENDPOINT}/${process.env.B2_BUCKET}/${remotePath}`,
|
url: `https://${process.env.B2_CDN_ENDPOINT}/${process.env.B2_BUCKET}/${remotePath}`,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user