mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
support providers
This commit is contained in:
parent
c25528269a
commit
70778caba9
@ -3,7 +3,7 @@ import fs from "fs"
|
|||||||
|
|
||||||
import * as Errors from "@shared-classes/Errors"
|
import * as Errors from "@shared-classes/Errors"
|
||||||
import FileUpload from "@shared-classes/FileUpload"
|
import FileUpload from "@shared-classes/FileUpload"
|
||||||
import useCompression from "@services/useCompression"
|
import PostProcess from "@services/post-process"
|
||||||
|
|
||||||
const cachePath = global.cache.constructor.cachePath
|
const cachePath = global.cache.constructor.cachePath
|
||||||
|
|
||||||
@ -15,6 +15,8 @@ export default async (req, res) => {
|
|||||||
return new Errors.AuthorizationError(req, res)
|
return new Errors.AuthorizationError(req, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const providerType = req.headers["provider-type"]
|
||||||
|
|
||||||
const userPath = path.join(cachePath, req.session.user_id)
|
const userPath = path.join(cachePath, req.session.user_id)
|
||||||
|
|
||||||
// 10 GB in bytes
|
// 10 GB in bytes
|
||||||
@ -40,21 +42,56 @@ export default async (req, res) => {
|
|||||||
build = await build()
|
build = await build()
|
||||||
|
|
||||||
if (!req.headers["no-compression"]) {
|
if (!req.headers["no-compression"]) {
|
||||||
build = await useCompression(build)
|
build = await PostProcess(build)
|
||||||
}
|
}
|
||||||
|
|
||||||
// compose remote path
|
// compose remote path
|
||||||
const remotePath = `${req.session.user_id}/${path.basename(build.filepath)}`
|
const remotePath = `${req.session.user_id}/${path.basename(build.filepath)}`
|
||||||
|
|
||||||
// upload to storage
|
let url = null
|
||||||
await global.storage.fPutObject(process.env.S3_BUCKET, remotePath, build.filepath)
|
|
||||||
|
switch (providerType) {
|
||||||
|
case "music": {
|
||||||
|
console.debug("uploading to backblaze b2")
|
||||||
|
|
||||||
|
// use backblaze b2
|
||||||
|
await global.b2Storage.authorize()
|
||||||
|
|
||||||
|
const uploadUrl = await global.b2Storage.getUploadUrl({
|
||||||
|
bucketId: process.env.B2_BUCKET_ID,
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = await fs.promises.readFile(build.filepath)
|
||||||
|
|
||||||
|
await global.b2Storage.uploadFile({
|
||||||
|
uploadUrl: uploadUrl.data.uploadUrl,
|
||||||
|
uploadAuthToken: uploadUrl.data.authorizationToken,
|
||||||
|
fileName: remotePath,
|
||||||
|
data: data,
|
||||||
|
info: build.metadata
|
||||||
|
})
|
||||||
|
|
||||||
|
url = path.join(`https://`, process.env.B2_ENDPOINT, process.env.B2_BUCKET, remotePath)
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
console.debug("uploading to minio")
|
||||||
|
// upload to storage
|
||||||
|
await global.storage.fPutObject(process.env.S3_BUCKET, remotePath, build.filepath, build.metadata ?? {
|
||||||
|
"Content-Type": build.mimetype,
|
||||||
|
})
|
||||||
|
|
||||||
|
// compose url
|
||||||
|
url = global.storage.composeRemoteURL(remotePath)
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// remove from cache
|
// remove from cache
|
||||||
fs.promises.rm(build.cachePath, { recursive: true, force: true })
|
fs.promises.rm(build.cachePath, { recursive: true, force: true })
|
||||||
|
|
||||||
// compose url
|
|
||||||
const url = global.storage.composeRemoteURL(remotePath)
|
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
name: build.filename,
|
name: build.filename,
|
||||||
id: remotePath,
|
id: remotePath,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user