mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
improve data managament on upload
This commit is contained in:
parent
97afeb9227
commit
d33176ca66
@ -1,9 +1,10 @@
|
|||||||
import path from "path"
|
import path from "path"
|
||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import { videoTranscode } from "../../../lib/videoTranscode"
|
import { videoTranscode } from "@lib/videoTranscode"
|
||||||
import Jimp from "jimp"
|
import Jimp from "jimp"
|
||||||
|
import mime from "mime-types"
|
||||||
|
|
||||||
import pmap from "../../../utils/pMap"
|
import pmap from "@utils/pMap"
|
||||||
|
|
||||||
const formidable = require("formidable")
|
const formidable = require("formidable")
|
||||||
|
|
||||||
@ -94,7 +95,11 @@ export default async (payload) => {
|
|||||||
uploadDir: params.cacheUploadDir,
|
uploadDir: params.cacheUploadDir,
|
||||||
maxFileSize: params.maxFileSize,
|
maxFileSize: params.maxFileSize,
|
||||||
maxFields: params.maxFields,
|
maxFields: params.maxFields,
|
||||||
filename: (name, ext) => {
|
filename: (name, ext, part, form) => {
|
||||||
|
if (!ext) {
|
||||||
|
ext = `.${mime.extension(part.mimetype)}`
|
||||||
|
}
|
||||||
|
|
||||||
name = global.nanoid()
|
name = global.nanoid()
|
||||||
|
|
||||||
return name + ext
|
return name + ext
|
||||||
@ -157,6 +162,12 @@ export default async (payload) => {
|
|||||||
file = await handleImage(file, params)
|
file = await handleImage(file, params)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
case "image/webp": {
|
||||||
|
file = await handleImage(file, params)
|
||||||
|
}
|
||||||
|
case "image/jfif": {
|
||||||
|
file = await handleImage(file, params)
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
@ -169,9 +180,13 @@ export default async (payload) => {
|
|||||||
filename: file.newFilename,
|
filename: file.newFilename,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// upload path must be user_id + file.newFilename
|
||||||
|
const uploadPath = req.user?._id ? `${req.user?._id.toString()}/${file.newFilename}` : file.newFilename
|
||||||
|
|
||||||
// upload to s3
|
// upload to s3
|
||||||
await new Promise((_resolve, _reject) => {
|
await new Promise((_resolve, _reject) => {
|
||||||
global.storage.fPutObject(global.storage.defaultBucket, file.newFilename, file.filepath, metadata, (err, etag) => {
|
global.storage.fPutObject(global.storage.defaultBucket, uploadPath, file.filepath, metadata, (err, etag) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return _reject(new Error(`Failed to upload file to storage server > ${err.message}`))
|
return _reject(new Error(`Failed to upload file to storage server > ${err.message}`))
|
||||||
}
|
}
|
||||||
@ -186,12 +201,12 @@ export default async (payload) => {
|
|||||||
await fs.promises.unlink(file.filepath)
|
await fs.promises.unlink(file.filepath)
|
||||||
|
|
||||||
// get url location
|
// get url location
|
||||||
const remoteUrlObj = global.storage.composeRemoteURL(file.newFilename)
|
const remoteUrlObj = global.storage.composeRemoteURL(uploadPath)
|
||||||
|
|
||||||
// push final filepath to urls
|
// push final filepath to urls
|
||||||
return {
|
return {
|
||||||
name: file.originalFilename,
|
name: file.originalFilename,
|
||||||
id: file.newFilename,
|
id: uploadPath,
|
||||||
url: remoteUrlObj,
|
url: remoteUrlObj,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user