From 1e943859eb6d9908ba72c75eadf51e0f75953759 Mon Sep 17 00:00:00 2001 From: tycrek Date: Mon, 14 Mar 2022 19:35:51 -0600 Subject: [PATCH] Removed all of these now redundant symbols --- src/routers/resource.ts | 14 ++++----- src/routers/upload.ts | 22 ++++++------- src/storage.ts | 70 ++++++++++++++++++++--------------------- src/utils.ts | 6 ++-- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/routers/resource.ts b/src/routers/resource.ts index 1381d81..398fbd1 100644 --- a/src/routers/resource.ts +++ b/src/routers/resource.ts @@ -28,8 +28,8 @@ router.use((req: Request, res: Response, next) => { }); // View file -router.get('/', (req: Request, res: Response, next) => data().get(req.ass?.resourceId).then((fileData: FileData) => { - const resourceId = req.ass!.resourceId; +router.get('/', (req: Request, res: Response, next) => data().get(req.ass.resourceId).then((fileData: FileData) => { + const resourceId = req.ass.resourceId; // Build OpenGraph meta tags const og = fileData.opengraph, ogs = ['']; @@ -62,7 +62,7 @@ router.get('/', (req: Request, res: Response, next) => data().get(req.ass?.resou }).catch(next)); // Direct resource -router.get('/direct*', (req: Request, res: Response, next) => data().get(req.ass?.resourceId).then((fileData: FileData) => { +router.get('/direct*', (req: Request, res: Response, next) => data().get(req.ass.resourceId).then((fileData: FileData) => { // Send file as an attachement for downloads if (req.query.download) res.header('Content-Disposition', `attachment; filename="${fileData.originalname}"`); @@ -89,7 +89,7 @@ router.get('/direct*', (req: Request, res: Response, next) => data().get(req.ass // Thumbnail response router.get('/thumbnail', (req: Request, res: Response, next) => - data().get(req.ass?.resourceId) + data().get(req.ass.resourceId) .then(({ is, thumbnail }: { is: IsPossible, thumbnail: string }) => fs.readFile((!is || (is.image || is.video)) ? path(diskFilePath, 'thumbnails/', thumbnail) : is.audio ? 'views/ass-audio-icon.png' : 'views/ass-file-icon.png')) .then((fileData: Buffer) => res.type('jpg').send(fileData)) .catch(next)); @@ -98,7 +98,7 @@ router.get('/thumbnail', (req: Request, res: Response, next) => // https://oembed.com/ // https://old.reddit.com/r/discordapp/comments/82p8i6/a_basic_tutorial_on_how_to_get_the_most_out_of/ router.get('/oembed', (req: Request, res: Response, next) => - data().get(req.ass?.resourceId) + data().get(req.ass.resourceId) .then((fileData: FileData) => res.type('json').send({ version: '1.0', @@ -117,7 +117,7 @@ router.get('/oembed', (req: Request, res: Response, next) => // Delete file router.get('/delete/:deleteId', (req: Request, res: Response, next) => { let oldName: string, oldType: string; // skipcq: JS-0119 - data().get(req.ass?.resourceId) + data().get(req.ass.resourceId) .then((fileData: FileData) => { // Extract info for logs oldName = fileData.originalname; @@ -135,7 +135,7 @@ router.get('/delete/:deleteId', (req: Request, res: Response, next) => { (!fileData.is || (fileData.is.image || fileData.is.video)) && fs.existsSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail)) ? fs.rmSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail)) : () => Promise.resolve()]); }) - .then(() => data().del(req.ass?.resourceId)) + .then(() => data().del(req.ass.resourceId)) .then(() => (log.success('Deleted', oldName, oldType), res.type('text').send('File has been deleted!'))) // skipcq: JS-0090 .catch(next); }); diff --git a/src/routers/upload.ts b/src/routers/upload.ts index 36aab91..748fbe5 100644 --- a/src/routers/upload.ts +++ b/src/routers/upload.ts @@ -51,19 +51,19 @@ router.post('/', (req: Request, res: Response, next: Function) => { const generator = req.headers['x-ass-access']?.toString() || resourceIdType; // Save domain with file - req.file!.domain = `${getTrueHttp()}${trueDomain}`; + req.file.domain = `${getTrueHttp()}${trueDomain}`; // Get the uploaded time in milliseconds - req.file!.timestamp = DateTime.now().toMillis(); + req.file.timestamp = DateTime.now().toMillis(); // Save the timezone offset req.file!.timeoffset = req.headers['x-ass-timeoffset']?.toString() || 'UTC+0'; // Keep track of the token that uploaded the resource - req.file!.token = req.token ?? ''; + req.file.token = req.token ?? ''; // Attach any embed overrides, if necessary - req.file!.opengraph = { + req.file.opengraph = { title: req.headers['x-ass-og-title'], description: req.headers['x-ass-og-description'], author: req.headers['x-ass-og-author'], @@ -74,13 +74,13 @@ router.post('/', (req: Request, res: Response, next: Function) => { }; // Fix spaces in originalname - req.file!.originalname = req.file!.originalname.replace(/\s/g, spaceReplace === '!' ? '' : spaceReplace); + req.file!.originalname = req.file.originalname.replace(/\s/g, spaceReplace === '!' ? '' : spaceReplace); // Generate a unique resource ID let resourceId = ''; // Function to call to generate a fresh ID. Used for multiple attempts in case an ID is already taken - const gen = () => generateId(generator, resourceIdSize, parseInt(req.headers['x-ass-gfycat']?.toString() || gfyIdSize.toString()), req.file!.originalname); + const gen = () => generateId(generator, resourceIdSize, parseInt(req.headers['x-ass-gfycat']?.toString() || gfyIdSize.toString()), req.file.originalname); // Keeps track of the number of attempts in case all ID's are taken const attempts = { @@ -109,13 +109,13 @@ router.post('/', (req: Request, res: Response, next: Function) => { .then(() => data().put(resourceId.split('.')[0], req.file)) .then(() => { // Log the upload - const logInfo = `${req.file!.originalname} (${req.file!.mimetype}, ${formatBytes(req.file!.size)})`; + const logInfo = `${req.file!.originalname} (${req.file!.mimetype}, ${formatBytes(req.file.size)})`; log.success('File uploaded', logInfo, `uploaded by ${users[req.token ?? ''] ? users[req.token ?? ''].username : ''}`); // Build the URLs const resourceUrl = `${getTrueHttp()}${trueDomain}/${resourceId}`; const thumbnailUrl = `${getTrueHttp()}${trueDomain}/${resourceId}/thumbnail`; - const deleteUrl = `${getTrueHttp()}${trueDomain}/${resourceId}/delete/${req.file!.deleteId}`; + const deleteUrl = `${getTrueHttp()}${trueDomain}/${resourceId}/delete/${req.file.deleteId}`; // Send the response res.type('json').send({ resource: resourceUrl, thumbnail: thumbnailUrl, delete: deleteUrl }) @@ -135,9 +135,9 @@ router.post('/', (req: Request, res: Response, next: Function) => { .setTitle(logInfo) //@ts-ignore .setURL(resourceUrl) - .setDescription(`**Size:** \`${formatBytes(req.file!.size)}\`\n**[Delete](${deleteUrl})**`) + .setDescription(`**Size:** \`${formatBytes(req.file.size)}\`\n**[Delete](${deleteUrl})**`) .setThumbnail(thumbnailUrl) - .setColor(req.file!.vibrant) + .setColor(req.file.vibrant) .setTimestamp(); // Send the embed to the webhook, then delete the client after to free resources @@ -149,7 +149,7 @@ router.post('/', (req: Request, res: Response, next: Function) => { // Also update the users upload count if (!users[req.token ?? '']) { - const generateUsername = () => generateId('random', 20, 0, req.file!.size.toString()); // skipcq: JS-0074 + const generateUsername = () => generateId('random', 20, 0, req.file.size.toString()); // skipcq: JS-0074 let username: string = generateUsername(); // eslint-disable-next-line @typescript-eslint/ban-ts-comment diff --git a/src/storage.ts b/src/storage.ts index 7545f99..7bd54dc 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -34,22 +34,22 @@ function getDatedDirname() { } function getLocalFilename(req: Request) { - return `${getDatedDirname()}/${saveAsOriginal ? req.file!.originalname : req.file!.sha1}`; + return `${getDatedDirname()}/${saveAsOriginal ? req.file!.originalname : req.file.sha1}`; } export function processUploaded(req: Request, res: Response, next: Function) { // skipcq: JS-0045 // Fix file object - req.file = req.files!.file; + req.file = req.files.file; // Other fixes - req.file!.ext = '.'.concat((req.file!.filename ?? '').split('.').pop() ?? ''); - req.file!.originalname = req.file!.filename ?? ''; - req.file!.path = req.file!.file ?? ''; - req.file!.randomId = generateId('random', ID_GEN_LENGTH, 0, ''); - req.file!.deleteId = generateId('random', ID_GEN_LENGTH, 0, ''); + req.file!.ext = '.'.concat((req.file.filename ?? '').split('.').pop() ?? ''); + req.file!.originalname = req.file.filename ?? ''; + req.file!.path = req.file.file ?? ''; + req.file.randomId = generateId('random', ID_GEN_LENGTH, 0, ''); + req.file.deleteId = generateId('random', ID_GEN_LENGTH, 0, ''); // Set up types - req.file!.is = { + req.file.is = { image: false, video: false, audio: false, @@ -57,16 +57,16 @@ export function processUploaded(req: Request, res: Response, next: Function) { / }; // Specify correct type - const isType = req.file!.mimetype.includes('image') ? 'image' : req.file!.mimetype.includes('video') ? 'video' : req.file!.mimetype.includes('audio') ? 'audio' : 'other'; - req.file!.is[isType] = true; + const isType = req.file!.mimetype.includes('image') ? 'image' : req.file!.mimetype.includes('video') ? 'video' : req.file.mimetype.includes('audio') ? 'audio' : 'other'; + req.file.is[isType] = true; // Block the resource if the mimetype is not an image or video - if (mediaStrict && !ALLOWED_MIMETYPES.test(req.file!.mimetype)) + if (mediaStrict && !ALLOWED_MIMETYPES.test(req.file.mimetype)) return log - .warn('Upload blocked', req.file!.originalname, req.file!.mimetype) + .warn('Upload blocked', req.file!.originalname, req.file.mimetype) .warn('Strict media mode', 'only images, videos, & audio are file permitted') .callback(() => - fs.remove(req.file!.path) + fs.remove(req.file.path) .then(() => log .debug('Temp file', 'deleted') .callback(() => res.sendStatus(CODE_UNSUPPORTED_MEDIA_TYPE))) @@ -75,29 +75,29 @@ export function processUploaded(req: Request, res: Response, next: Function) { / .callback(next, err))); // Remove unwanted fields - delete req.file!.uuid; - delete req.file!.field; - delete req.file!.file; - delete req.file!.filename; - delete req.file!.truncated; - delete req.file!.done; + delete req.file.uuid; + delete req.file.field; + delete req.file.file; + delete req.file.filename; + delete req.file.truncated; + delete req.file.done; // Operations // @ts-ignore - Promise.all([Thumbnail(req.file), Vibrant(req.file), Hash(req.file), fs.stat(req.file!.path)]) + Promise.all([Thumbnail(req.file), Vibrant(req.file), Hash(req.file), fs.stat(req.file.path)]) // skipcq: JS-0086 .then(([thumbnail, vibrant, sha1, stat]: [string, string, string, Stats]) => ( - req.file!.thumbnail = thumbnail, // skipcq: JS-0090 - req.file!.vibrant = vibrant, // skipcq: JS-0090 - req.file!.sha1 = sha1, // skipcq: JS-0090 - req.file!.size = stat.size // skipcq: JS-0090 + req.file.thumbnail = thumbnail, // skipcq: JS-0090 + req.file.vibrant = vibrant, // skipcq: JS-0090 + req.file.sha1 = sha1, // skipcq: JS-0090 + req.file.size = stat.size // skipcq: JS-0090 )) // Check if file size is too big - .then(() => { if (req.file!.size / Math.pow(1024, 2) > maxUploadSize) throw new Error('LIMIT_FILE_SIZE'); }) + .then(() => { if (req.file.size / Math.pow(1024, 2) > maxUploadSize) throw new Error('LIMIT_FILE_SIZE'); }) // Save file - .then(() => log.debug('Saving file', req.file!.originalname, s3enabled ? 'in S3' : useSia ? 'on Sia blockchain' : 'on disk')) + .then(() => log.debug('Saving file', req.file.originalname, s3enabled ? 'in S3' : useSia ? 'on Sia blockchain' : 'on disk')) .then(() => // skipcq: JS-0229 new Promise((resolve, reject) => { @@ -105,31 +105,31 @@ export function processUploaded(req: Request, res: Response, next: Function) { / // Upload to Amazon S3 if (s3enabled) return s3.putObject({ Bucket: s3bucket, - Key: req.file!.randomId.concat(req.file!.ext), + Key: req.file!.randomId.concat(req.file.ext), ACL: 'public-read', - ContentType: req.file!.mimetype, - Body: fs.createReadStream(req.file!.path) + ContentType: req.file.mimetype, + Body: fs.createReadStream(req.file.path) }).promise().then(resolve).catch(reject); // Use Sia Skynet - else if (useSia) return SkynetUpload(req.file!.path) - .then((skylink) => req.file!.randomId = skylink) + else if (useSia) return SkynetUpload(req.file.path) + .then((skylink) => req.file.randomId = skylink) .then(resolve).catch(reject); // Save to local storage else return fs.ensureDir(getDatedDirname()) - .then(() => fs.copy(req.file!.path, getLocalFilename(req), { preserveTimestamps: true })) + .then(() => fs.copy(req.file.path, getLocalFilename(req), { preserveTimestamps: true })) .then(resolve).catch(reject); })) - .then(() => log.debug('File saved', req.file!.originalname, s3enabled ? 'in S3' : useSia ? 'on Sia blockchain' : 'on disk')) + .then(() => log.debug('File saved', req.file.originalname, s3enabled ? 'in S3' : useSia ? 'on Sia blockchain' : 'on disk')) .catch((err) => next(err)) // Delete the file - .then(() => fs.remove(req.file!.path)) + .then(() => fs.remove(req.file.path)) .then(() => log.debug('Temp file', 'deleted')) // Fix the file path - .then(() => !s3enabled && (req.file!.path = getLocalFilename(req))) // skipcq: JS-0090 + .then(() => !s3enabled && (req.file.path = getLocalFilename(req))) // skipcq: JS-0090 .then(() => next()) .catch((err) => next(err)); } diff --git a/src/utils.ts b/src/utils.ts index 481cc7c..0ab3589 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -110,9 +110,9 @@ module.exports = { verify, renameFile: (req: Request, newName: string) => new Promise((resolve: Function, reject) => { try { - const paths = [req.file!.destination, newName]; - fs.rename(path(req.file!.path), path(...paths)); - req.file!.path = Path.join(...paths); + const paths = [req.file.destination, newName]; + fs.rename(path(req.file.path), path(...paths)); + req.file.path = Path.join(...paths); resolve(); } catch (err) { reject(err);