mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 19:44:15 +00:00
improve handler
This commit is contained in:
parent
7c193e885b
commit
09c349f66e
@ -10,116 +10,153 @@ import StandardUpload from "./providers/standard"
|
|||||||
import B2Upload from "./providers/b2"
|
import B2Upload from "./providers/b2"
|
||||||
|
|
||||||
export default async ({
|
export default async ({
|
||||||
source,
|
source,
|
||||||
parentDir,
|
parentDir,
|
||||||
service,
|
service,
|
||||||
useCompression,
|
useCompression,
|
||||||
cachePath,
|
cachePath,
|
||||||
transmux,
|
transmux,
|
||||||
transmuxOptions,
|
transmuxOptions,
|
||||||
isDirectory,
|
isDirectory,
|
||||||
|
onProgress,
|
||||||
}) => {
|
}) => {
|
||||||
if (!source) {
|
if (!source) {
|
||||||
throw new OperationError(500, "source is required")
|
throw new OperationError(500, "source is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!service) {
|
if (!service) {
|
||||||
service = "standard"
|
service = "standard"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parentDir) {
|
if (!parentDir) {
|
||||||
parentDir = "/"
|
parentDir = "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (transmuxOptions) {
|
if (transmuxOptions) {
|
||||||
transmuxOptions = JSON.parse(transmuxOptions)
|
transmuxOptions = JSON.parse(transmuxOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useCompression) {
|
if (useCompression) {
|
||||||
try {
|
if (typeof onProgress === "function") {
|
||||||
const processOutput = await PostProcess({
|
onProgress(10, {
|
||||||
filepath: source,
|
event: "post_processing",
|
||||||
cachePath: cachePath,
|
})
|
||||||
})
|
}
|
||||||
|
|
||||||
if (processOutput) {
|
try {
|
||||||
if (processOutput.filepath) {
|
const processOutput = await PostProcess({
|
||||||
source = processOutput.filepath
|
filepath: source,
|
||||||
}
|
cachePath: cachePath,
|
||||||
}
|
})
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
throw new OperationError(500, `Failed to process file`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (transmux) {
|
if (processOutput) {
|
||||||
try {
|
if (processOutput.filepath) {
|
||||||
const processOutput = await Transmux({
|
source = processOutput.filepath
|
||||||
transmuxer: transmux,
|
}
|
||||||
transmuxOptions: transmuxOptions,
|
}
|
||||||
filepath: source,
|
} catch (error) {
|
||||||
cachePath: cachePath
|
console.error(error)
|
||||||
})
|
throw new OperationError(500, `Failed to process file`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (processOutput) {
|
if (transmux) {
|
||||||
if (processOutput.filepath) {
|
if (typeof onProgress === "function") {
|
||||||
source = processOutput.filepath
|
onProgress(30, {
|
||||||
}
|
event: "transmuxing",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (processOutput.isDirectory) {
|
try {
|
||||||
isDirectory = true
|
const processOutput = await Transmux({
|
||||||
}
|
transmuxer: transmux,
|
||||||
}
|
transmuxOptions: transmuxOptions,
|
||||||
} catch (error) {
|
filepath: source,
|
||||||
console.error(error)
|
cachePath: cachePath,
|
||||||
throw new OperationError(500, `Failed to transmux file`)
|
})
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const type = mimeTypes.lookup(path.basename(source))
|
if (processOutput) {
|
||||||
const hash = await getFileHash(fs.createReadStream(source))
|
if (processOutput.filepath) {
|
||||||
|
source = processOutput.filepath
|
||||||
|
}
|
||||||
|
|
||||||
const remotePath = path.join(parentDir, hash)
|
if (processOutput.isDirectory) {
|
||||||
|
isDirectory = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
throw new OperationError(500, `Failed to transmux file`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let result = {}
|
const type = mimeTypes.lookup(path.basename(source))
|
||||||
|
const hash = await getFileHash(fs.createReadStream(source))
|
||||||
|
|
||||||
const metadata = {
|
let fileId = `${hash}`
|
||||||
"Content-Type": type,
|
|
||||||
"File-Hash": hash,
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
// FIXME: This is a walkaround to avoid to hashing the entire directories
|
||||||
switch (service) {
|
if (isDirectory) {
|
||||||
case "b2":
|
fileId = global.nanoid()
|
||||||
if (!global.b2Storage) {
|
}
|
||||||
throw new OperationError(500, "B2 storage not configured on environment, unsupported service. Please use `standard` service.")
|
|
||||||
}
|
|
||||||
|
|
||||||
result = await B2Upload({
|
let remotePath = path.join(parentDir, fileId)
|
||||||
source: isDirectory ? path.dirname(source) : source,
|
|
||||||
remotePath: remotePath,
|
|
||||||
metadata: metadata,
|
|
||||||
isDirectory: isDirectory,
|
|
||||||
targetFilename: isDirectory ? path.basename(source) : null,
|
|
||||||
})
|
|
||||||
break
|
|
||||||
case "standard":
|
|
||||||
result = await StandardUpload({
|
|
||||||
source: isDirectory ? path.dirname(source) : source,
|
|
||||||
remotePath: remotePath,
|
|
||||||
metadata: metadata,
|
|
||||||
isDirectory: isDirectory,
|
|
||||||
targetFilename: isDirectory ? path.basename(source) : null,
|
|
||||||
})
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw new OperationError(500, "Unsupported service")
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
throw new OperationError(500, "Failed to upload to storage")
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
let result = {}
|
||||||
|
|
||||||
|
const metadata = {
|
||||||
|
"Content-Type": type,
|
||||||
|
"File-Hash": hash,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof onProgress === "function") {
|
||||||
|
onProgress(80, {
|
||||||
|
event: "uploading_s3",
|
||||||
|
service: service,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch (service) {
|
||||||
|
case "b2":
|
||||||
|
if (!global.b2Storage) {
|
||||||
|
throw new OperationError(
|
||||||
|
500,
|
||||||
|
"B2 storage not configured on environment, unsupported service. Please use `standard` service.",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = await B2Upload({
|
||||||
|
source: isDirectory ? path.dirname(source) : source,
|
||||||
|
remotePath: remotePath,
|
||||||
|
metadata: metadata,
|
||||||
|
isDirectory: isDirectory,
|
||||||
|
targetFilename: isDirectory ? path.basename(source) : null,
|
||||||
|
})
|
||||||
|
break
|
||||||
|
case "standard":
|
||||||
|
result = await StandardUpload({
|
||||||
|
source: isDirectory ? path.dirname(source) : source,
|
||||||
|
remotePath: remotePath,
|
||||||
|
metadata: metadata,
|
||||||
|
isDirectory: isDirectory,
|
||||||
|
targetFilename: isDirectory ? path.basename(source) : null,
|
||||||
|
})
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
throw new OperationError(500, "Unsupported service")
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
throw new OperationError(500, "Failed to upload to storage")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof onProgress === "function") {
|
||||||
|
onProgress(100, {
|
||||||
|
event: "done",
|
||||||
|
result: result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user