| | const fs = require('fs'); |
| | const path = require('path'); |
| | const sharp = require('sharp'); |
| | const { resizeImageBuffer } = require('../images/resize'); |
| | const { updateUser, updateFile } = require('~/models'); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | async function uploadLocalImage({ req, file, file_id, endpoint, resolution = 'high' }) { |
| | const appConfig = req.config; |
| | const inputFilePath = file.path; |
| | const inputBuffer = await fs.promises.readFile(inputFilePath); |
| | const { |
| | buffer: resizedBuffer, |
| | width, |
| | height, |
| | } = await resizeImageBuffer(inputBuffer, resolution, endpoint); |
| | const extension = path.extname(inputFilePath); |
| |
|
| | const { imageOutput } = appConfig.paths; |
| | const userPath = path.join(imageOutput, req.user.id); |
| |
|
| | if (!fs.existsSync(userPath)) { |
| | fs.mkdirSync(userPath, { recursive: true }); |
| | } |
| |
|
| | const fileName = `${file_id}__${path.basename(inputFilePath)}`; |
| | const newPath = path.join(userPath, fileName); |
| | const targetExtension = `.${appConfig.imageOutputType}`; |
| |
|
| | if (extension.toLowerCase() === targetExtension) { |
| | const bytes = Buffer.byteLength(resizedBuffer); |
| | await fs.promises.writeFile(newPath, resizedBuffer); |
| | const filepath = path.posix.join('/', 'images', req.user.id, path.basename(newPath)); |
| | return { filepath, bytes, width, height }; |
| | } |
| |
|
| | const outputFilePath = newPath.replace(extension, targetExtension); |
| | const data = await sharp(resizedBuffer).toFormat(appConfig.imageOutputType).toBuffer(); |
| | await fs.promises.writeFile(outputFilePath, data); |
| | const bytes = Buffer.byteLength(data); |
| | const filepath = path.posix.join('/', 'images', req.user.id, path.basename(outputFilePath)); |
| | await fs.promises.unlink(inputFilePath); |
| | return { filepath, bytes, width, height }; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | function encodeImage(imagePath) { |
| | return new Promise((resolve, reject) => { |
| | fs.readFile(imagePath, (err, data) => { |
| | if (err) { |
| | reject(err); |
| | } else { |
| | resolve(data.toString('base64')); |
| | } |
| | }); |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | async function prepareImagesLocal(req, file) { |
| | const appConfig = req.config; |
| | const { publicPath, imageOutput } = appConfig.paths; |
| | const userPath = path.join(imageOutput, req.user.id); |
| |
|
| | if (!fs.existsSync(userPath)) { |
| | fs.mkdirSync(userPath, { recursive: true }); |
| | } |
| | const filepath = path.join(publicPath, file.filepath); |
| |
|
| | const promises = []; |
| | promises.push(updateFile({ file_id: file.file_id })); |
| | promises.push(encodeImage(filepath)); |
| | return await Promise.all(promises); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | async function processLocalAvatar({ buffer, userId, manual, agentId }) { |
| | const userDir = path.resolve( |
| | __dirname, |
| | '..', |
| | '..', |
| | '..', |
| | '..', |
| | '..', |
| | 'client', |
| | 'public', |
| | 'images', |
| | userId, |
| | ); |
| |
|
| | const metadata = await sharp(buffer).metadata(); |
| | const extension = metadata.format === 'gif' ? 'gif' : 'png'; |
| |
|
| | const timestamp = new Date().getTime(); |
| | |
| | const fileName = agentId |
| | ? `agent-${agentId}-avatar-${timestamp}.${extension}` |
| | : `avatar-${timestamp}.${extension}`; |
| | const urlRoute = `/images/${userId}/${fileName}`; |
| | const avatarPath = path.join(userDir, fileName); |
| |
|
| | await fs.promises.mkdir(userDir, { recursive: true }); |
| | await fs.promises.writeFile(avatarPath, buffer); |
| |
|
| | const isManual = manual === 'true'; |
| | let url = `${urlRoute}?manual=${isManual}`; |
| |
|
| | |
| | if (isManual && !agentId) { |
| | await updateUser(userId, { avatar: url }); |
| | } |
| |
|
| | return url; |
| | } |
| |
|
| | module.exports = { uploadLocalImage, encodeImage, prepareImagesLocal, processLocalAvatar }; |
| |
|