-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Chillandchat/base64-to-binary-migration
- Loading branch information
Showing
7 changed files
with
296 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
import debug from "../utils/debug"; | ||
import sanitizeDirectory from "../utils/sanitizeDirectory"; | ||
|
||
/** | ||
* This is the content endpoint, this endpoint will send the content to the user once called. | ||
* This endpoint is used to send images, videos, and other files to the user. | ||
* | ||
* @compatible | ||
* @type {GET} The type of request to the endpoint is get. | ||
* @param {string} path The path of the file. | ||
*/ | ||
|
||
const content = (req: Request, res: Response, _next: NextFunction): void => { | ||
try { | ||
sanitizeDirectory(req.url.slice(8)) | ||
.then((cleanPath: any): void => { | ||
res.status(200).sendFile(cleanPath.fullPath, { | ||
root: `${__dirname}/../../user-content`, | ||
}); | ||
debug.log(`Content: ${cleanPath.fullPath} sent.`); | ||
}) | ||
.catch((err): Response => res.status(403).send(err.toString())); | ||
} catch (err: unknown) { | ||
res.status(500).send(err); | ||
debug.error(err); | ||
} | ||
}; | ||
|
||
export default content; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import fs from "fs"; | ||
import sharp from "sharp"; | ||
import { exec } from "child_process"; | ||
import { FileTypeResult, fromBuffer } from "file-type"; | ||
|
||
import debug from "../utils/debug"; | ||
import user from "../schema/authSchema"; | ||
import { AuthSchemaType } from "../utils"; | ||
|
||
/** | ||
* This is the legacy upload content endpoint, this endpoint uses base-64 | ||
* to save the server's chat content in the server. | ||
* | ||
* @note | ||
* Please note that this is the legacy version, and should not be used. | ||
* The only reason this exists is for backward compatibility. | ||
* | ||
* @deprecated | ||
* @type {POST} This is a POST typed endpoint. | ||
* @param {string} id The id of the content. | ||
* @param {string} content The content of the file. | ||
* @param {ContentType} type The type of the file. | ||
* @param {string} user The user who uploaded this content. | ||
*/ | ||
|
||
const legacyUploadContent = async ( | ||
req: Request, | ||
res: Response, | ||
_next: NextFunction | ||
): Promise<void> => { | ||
if (req.query.key !== String(process.env.KEY)) { | ||
res.status(401).send("Invalid api key."); | ||
return; | ||
} | ||
|
||
try { | ||
await user | ||
.findOne({ username: { $eq: req.body.user } }) | ||
.exec() | ||
.then(async (user: AuthSchemaType): Promise<void> => { | ||
if (user === null) { | ||
res.status(400).send("ERROR: Non-existent user!"); | ||
return; | ||
} | ||
|
||
const uuid4Regex: RegExp = | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89aAbB][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||
|
||
if (!uuid4Regex.test(req.body.id) || req.body.user.includes(" ")) { | ||
res | ||
.status(400) | ||
.send("ERROR: Invalid input format, please correct format."); | ||
return; | ||
} | ||
|
||
const videoFormat: FileTypeResult | null | undefined = | ||
req.body.type === "CHILL&CHAT_GIF" | ||
? await fromBuffer(Buffer.from(req.body.content, "base64")) | ||
: null; | ||
|
||
const fileType: string = | ||
videoFormat !== null ? videoFormat.ext : "webp"; | ||
if (!fs.existsSync(`${__dirname}/../../user-content/${req.body.user}`)) | ||
fs.mkdirSync(`${__dirname}/../../user-content/${req.body.user}`, { | ||
recursive: true, | ||
}); | ||
|
||
fs.writeFileSync( | ||
`${__dirname}/../../user-content/${req.body.user}/${req.body.id}.${fileType}`, | ||
req.body.type === "CHILL&CHAT_GIF" | ||
? Buffer.from(req.body.content, "base64") | ||
: await sharp(Buffer.from(req.body.content, "base64")) | ||
.webp({ lossless: true }) | ||
.toBuffer(), | ||
"base64" | ||
); | ||
|
||
if (req.body.type === "CHILL&CHAT_GIF") { | ||
const command: string = `ffmpeg -ss 00:00:00.000 -i ${__dirname}/../../user-content/${req.body.user}/${req.body.id}.${fileType} -pix_fmt rgb24 -s 320x240 -r 10 -t 00:00:10.000 ${__dirname}/../../user-content/${req.body.user}/${req.body.id}.gif`; | ||
exec(command); | ||
} | ||
}); | ||
} catch (err: unknown) { | ||
res.status(500).send(`SERVER ERROR: ${err}`); | ||
debug.error(err); | ||
} | ||
}; | ||
|
||
export default legacyUploadContent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.