mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
use concurrent map for processing
This commit is contained in:
parent
f67f7152c0
commit
0307e866c4
@ -2,6 +2,8 @@ import { Controller } from "linebridge/dist/server"
|
|||||||
import path from "path"
|
import path from "path"
|
||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import stream from "stream"
|
import stream from "stream"
|
||||||
|
import pmap from "../../utils/pMap"
|
||||||
|
|
||||||
const formidable = require("formidable")
|
const formidable = require("formidable")
|
||||||
|
|
||||||
function resolveToUrl(filepath, req) {
|
function resolveToUrl(filepath, req) {
|
||||||
@ -119,6 +121,7 @@ export default class FilesController extends Controller {
|
|||||||
|
|
||||||
const results = await new Promise((resolve, reject) => {
|
const results = await new Promise((resolve, reject) => {
|
||||||
const processedFiles = []
|
const processedFiles = []
|
||||||
|
let queuePromieses = []
|
||||||
|
|
||||||
// create a new thread for each file
|
// create a new thread for each file
|
||||||
form.parse(req, async (err, fields, data) => {
|
form.parse(req, async (err, fields, data) => {
|
||||||
@ -130,31 +133,52 @@ export default class FilesController extends Controller {
|
|||||||
data.files = [data.files]
|
data.files = [data.files]
|
||||||
}
|
}
|
||||||
|
|
||||||
for await (let file of data.files) {
|
for (let file of data.files) {
|
||||||
// check if is video need to transcode
|
// create process queue
|
||||||
switch (file.mimetype) {
|
queuePromieses.push(async () => {
|
||||||
case "video/quicktime": {
|
// check if is video need to transcode
|
||||||
file.filepath = await videoTranscode(file.filepath, global.uploadCachePath)
|
switch (file.mimetype) {
|
||||||
file.newFilename = path.basename(file.filepath)
|
case "video/quicktime": {
|
||||||
break
|
file.filepath = await videoTranscode(file.filepath, global.uploadCachePath)
|
||||||
|
file.newFilename = path.basename(file.filepath)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
default: {
|
// move file to upload path
|
||||||
// do nothing
|
await fs.promises.rename(file.filepath, path.join(global.uploadPath, file.newFilename))
|
||||||
|
|
||||||
|
// push final filepath to urls
|
||||||
|
return {
|
||||||
|
name: file.originalFilename,
|
||||||
|
id: file.newFilename,
|
||||||
|
url: resolveToUrl(file.newFilename, req),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// move file to upload path
|
|
||||||
await fs.promises.rename(file.filepath, path.join(global.uploadPath, file.newFilename))
|
|
||||||
|
|
||||||
// push final filepath to urls
|
|
||||||
processedFiles.push({
|
|
||||||
name: file.originalFilename,
|
|
||||||
id: file.newFilename,
|
|
||||||
url: resolveToUrl(file.newFilename, req),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wait for all files to be processed
|
||||||
|
await pmap(queuePromieses,
|
||||||
|
async (fn) => {
|
||||||
|
const result = await fn().catch((err) => {
|
||||||
|
console.error(err)
|
||||||
|
|
||||||
|
return null
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
processedFiles.push(result)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
concurrency: 5
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
return resolve(processedFiles)
|
return resolve(processedFiles)
|
||||||
})
|
})
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user