improve handler

This commit is contained in:
SrGooglo 2025-02-05 02:50:22 +00:00
parent 7c193e885b
commit 09c349f66e

View File

@ -10,116 +10,153 @@ import StandardUpload from "./providers/standard"
import B2Upload from "./providers/b2"
export default async ({
source,
parentDir,
service,
useCompression,
cachePath,
transmux,
transmuxOptions,
isDirectory,
source,
parentDir,
service,
useCompression,
cachePath,
transmux,
transmuxOptions,
isDirectory,
onProgress,
}) => {
if (!source) {
throw new OperationError(500, "source is required")
}
if (!source) {
throw new OperationError(500, "source is required")
}
if (!service) {
service = "standard"
}
if (!service) {
service = "standard"
}
if (!parentDir) {
parentDir = "/"
}
if (!parentDir) {
parentDir = "/"
}
if (transmuxOptions) {
transmuxOptions = JSON.parse(transmuxOptions)
}
if (transmuxOptions) {
transmuxOptions = JSON.parse(transmuxOptions)
}
if (useCompression) {
try {
const processOutput = await PostProcess({
filepath: source,
cachePath: cachePath,
})
if (useCompression) {
if (typeof onProgress === "function") {
onProgress(10, {
event: "post_processing",
})
}
if (processOutput) {
if (processOutput.filepath) {
source = processOutput.filepath
}
}
} catch (error) {
console.error(error)
throw new OperationError(500, `Failed to process file`)
}
}
try {
const processOutput = await PostProcess({
filepath: source,
cachePath: cachePath,
})
if (transmux) {
try {
const processOutput = await Transmux({
transmuxer: transmux,
transmuxOptions: transmuxOptions,
filepath: source,
cachePath: cachePath
})
if (processOutput) {
if (processOutput.filepath) {
source = processOutput.filepath
}
}
} catch (error) {
console.error(error)
throw new OperationError(500, `Failed to process file`)
}
}
if (processOutput) {
if (processOutput.filepath) {
source = processOutput.filepath
}
if (transmux) {
if (typeof onProgress === "function") {
onProgress(30, {
event: "transmuxing",
})
}
if (processOutput.isDirectory) {
isDirectory = true
}
}
} catch (error) {
console.error(error)
throw new OperationError(500, `Failed to transmux file`)
}
}
try {
const processOutput = await Transmux({
transmuxer: transmux,
transmuxOptions: transmuxOptions,
filepath: source,
cachePath: cachePath,
})
const type = mimeTypes.lookup(path.basename(source))
const hash = await getFileHash(fs.createReadStream(source))
if (processOutput) {
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 = {
"Content-Type": type,
"File-Hash": hash,
}
let fileId = `${hash}`
try {
switch (service) {
case "b2":
if (!global.b2Storage) {
throw new OperationError(500, "B2 storage not configured on environment, unsupported service. Please use `standard` service.")
}
// FIXME: This is a walkaround to avoid to hashing the entire directories
if (isDirectory) {
fileId = global.nanoid()
}
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")
}
let remotePath = path.join(parentDir, fileId)
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
}