|
|
@ -1,6 +1,8 @@
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
import { FileData, IsPossible, AssRequest, AssResponse } from '../definitions';
|
|
|
|
const escape = require('escape-html');
|
|
|
|
|
|
|
|
const fetch = require('node-fetch');
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
|
|
|
|
import escape from 'escape-html';
|
|
|
|
|
|
|
|
import fetch, { Response } from 'node-fetch';
|
|
|
|
const { deleteS3 } = require('../storage');
|
|
|
|
const { deleteS3 } = require('../storage');
|
|
|
|
const { diskFilePath, s3enabled, viewDirect } = require('../../config.json');
|
|
|
|
const { diskFilePath, s3enabled, viewDirect } = require('../../config.json');
|
|
|
|
const { path, log, getTrueHttp, getTrueDomain, formatBytes, formatTimestamp, getS3url, getDirectUrl, getResourceColor, replaceholder } = require('../utils');
|
|
|
|
const { path, log, getTrueHttp, getTrueDomain, formatBytes, formatTimestamp, getS3url, getDirectUrl, getResourceColor, replaceholder } = require('../utils');
|
|
|
@ -8,23 +10,23 @@ const { CODE_UNAUTHORIZED, CODE_NOT_FOUND, } = require('../../MagicNumbers.json'
|
|
|
|
const data = require('../data');
|
|
|
|
const data = require('../data');
|
|
|
|
const users = require('../auth');
|
|
|
|
const users = require('../auth');
|
|
|
|
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
import express from 'express';
|
|
|
|
const router = express.Router();
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
|
|
// Middleware for parsing the resource ID and handling 404
|
|
|
|
// Middleware for parsing the resource ID and handling 404
|
|
|
|
router.use((req, res, next) => {
|
|
|
|
router.use((req: AssRequest, res: AssResponse, next) => {
|
|
|
|
// Parse the resource ID
|
|
|
|
// Parse the resource ID
|
|
|
|
req.ass = { resourceId: escape(req.resourceId || '').split('.')[0] };
|
|
|
|
req.ass = { resourceId: escape(req.resourceId || '').split('.')[0] };
|
|
|
|
|
|
|
|
|
|
|
|
// If the ID is invalid, return 404. Otherwise, continue normally
|
|
|
|
// If the ID is invalid, return 404. Otherwise, continue normally
|
|
|
|
data.has(req.ass.resourceId)
|
|
|
|
data.has(req.ass.resourceId)
|
|
|
|
.then((has) => has ? next() : res.sendStatus(CODE_NOT_FOUND)) // skipcq: JS-0229
|
|
|
|
.then((has: boolean) => has ? next() : res.sendStatus(CODE_NOT_FOUND)) // skipcq: JS-0229
|
|
|
|
.catch(next);
|
|
|
|
.catch(next);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// View file
|
|
|
|
// View file
|
|
|
|
router.get('/', (req, res, next) => data.get(req.ass.resourceId).then((fileData) => {
|
|
|
|
router.get('/', (req: AssRequest, res: AssResponse, next) => data.get(req.ass?.resourceId).then((fileData: FileData) => {
|
|
|
|
const { resourceId } = req.ass;
|
|
|
|
const resourceId = req.ass?.resourceId;
|
|
|
|
|
|
|
|
|
|
|
|
// Build OpenGraph meta tags
|
|
|
|
// Build OpenGraph meta tags
|
|
|
|
const og = fileData.opengraph, ogs = [''];
|
|
|
|
const og = fileData.opengraph, ogs = [''];
|
|
|
@ -53,19 +55,19 @@ router.get('/', (req, res, next) => data.get(req.ass.resourceId).then((fileData)
|
|
|
|
}).catch(next));
|
|
|
|
}).catch(next));
|
|
|
|
|
|
|
|
|
|
|
|
// Direct resource
|
|
|
|
// Direct resource
|
|
|
|
router.get('/direct*', (req, res, next) => data.get(req.ass.resourceId).then((fileData) => {
|
|
|
|
router.get('/direct*', (req: AssRequest, res: AssResponse, next) => data.get(req.ass?.resourceId).then((fileData: FileData) => {
|
|
|
|
// Send file as an attachement for downloads
|
|
|
|
// Send file as an attachement for downloads
|
|
|
|
if (req.query.download)
|
|
|
|
if (req.query.download)
|
|
|
|
res.header('Content-Disposition', `attachment; filename="${fileData.originalname}"`);
|
|
|
|
res.header('Content-Disposition', `attachment; filename="${fileData.originalname}"`);
|
|
|
|
|
|
|
|
|
|
|
|
// Return the file differently depending on what storage option was used
|
|
|
|
// Return the file differently depending on what storage option was used
|
|
|
|
const uploaders = {
|
|
|
|
const uploaders = {
|
|
|
|
s3: () => fetch(getS3url(fileData.randomId, fileData.ext)).then((file) => {
|
|
|
|
s3: () => fetch(getS3url(fileData.randomId, fileData.ext)).then((file: Response) => {
|
|
|
|
file.headers.forEach((value, header) => res.setHeader(header, value));
|
|
|
|
file.headers.forEach((value, header) => res.setHeader(header, value));
|
|
|
|
file.body.pipe(res);
|
|
|
|
file.body?.pipe(res);
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
local: () => {
|
|
|
|
local: () => {
|
|
|
|
res.header('Accept-Ranges', 'bytes').header('Content-Length', fileData.size).type(fileData.mimetype);
|
|
|
|
res.header('Accept-Ranges', 'bytes').header('Content-Length', `${fileData.size}`).type(fileData.mimetype);
|
|
|
|
fs.createReadStream(fileData.path).pipe(res);
|
|
|
|
fs.createReadStream(fileData.path).pipe(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -74,33 +76,33 @@ router.get('/direct*', (req, res, next) => data.get(req.ass.resourceId).then((fi
|
|
|
|
}).catch(next));
|
|
|
|
}).catch(next));
|
|
|
|
|
|
|
|
|
|
|
|
// Thumbnail response
|
|
|
|
// Thumbnail response
|
|
|
|
router.get('/thumbnail', (req, res, next) =>
|
|
|
|
router.get('/thumbnail', (req: AssRequest, res: AssResponse, next) =>
|
|
|
|
data.get(req.ass.resourceId)
|
|
|
|
data.get(req.ass?.resourceId)
|
|
|
|
.then(({ is, thumbnail }) => fs.readFile((!is || (is.image || is.video)) ? path(diskFilePath, 'thumbnails/', thumbnail) : is.audio ? 'views/ass-audio-icon.png' : 'views/ass-file-icon.png'))
|
|
|
|
.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) => res.type('jpg').send(fileData))
|
|
|
|
.then((fileData: Buffer) => res.type('jpg').send(fileData))
|
|
|
|
.catch(next));
|
|
|
|
.catch(next));
|
|
|
|
|
|
|
|
|
|
|
|
// oEmbed response for clickable authors/providers
|
|
|
|
// oEmbed response for clickable authors/providers
|
|
|
|
// https://oembed.com/
|
|
|
|
// https://oembed.com/
|
|
|
|
// https://old.reddit.com/r/discordapp/comments/82p8i6/a_basic_tutorial_on_how_to_get_the_most_out_of/
|
|
|
|
// https://old.reddit.com/r/discordapp/comments/82p8i6/a_basic_tutorial_on_how_to_get_the_most_out_of/
|
|
|
|
router.get('/oembed', (req, res, next) =>
|
|
|
|
router.get('/oembed', (req: AssRequest, res: AssResponse, next) =>
|
|
|
|
data.get(req.ass.resourceId)
|
|
|
|
data.get(req.ass?.resourceId)
|
|
|
|
.then(({ opengraph, is, size, timestamp, originalname }) =>
|
|
|
|
.then((fileData: FileData) =>
|
|
|
|
res.type('json').send({
|
|
|
|
res.type('json').send({
|
|
|
|
version: '1.0',
|
|
|
|
version: '1.0',
|
|
|
|
type: is.video ? 'video' : is.image ? 'photo' : 'link',
|
|
|
|
type: fileData.is.video ? 'video' : fileData.is.image ? 'photo' : 'link',
|
|
|
|
author_url: opengraph.authorUrl,
|
|
|
|
author_url: fileData.opengraph.authorUrl,
|
|
|
|
provider_url: opengraph.providerUrl,
|
|
|
|
provider_url: fileData.opengraph.providerUrl,
|
|
|
|
author_name: replaceholder(opengraph.author || '', size, timestamp, originalname),
|
|
|
|
author_name: replaceholder(fileData.opengraph.author || '', fileData.size, fileData.timestamp, fileData.originalname),
|
|
|
|
provider_name: replaceholder(opengraph.provider || '', size, timestamp, originalname)
|
|
|
|
provider_name: replaceholder(fileData.opengraph.provider || '', fileData.size, fileData.timestamp, fileData.originalname)
|
|
|
|
}))
|
|
|
|
}))
|
|
|
|
.catch(next));
|
|
|
|
.catch(next));
|
|
|
|
|
|
|
|
|
|
|
|
// Delete file
|
|
|
|
// Delete file
|
|
|
|
router.get('/delete/:deleteId', (req, res, next) => {
|
|
|
|
router.get('/delete/:deleteId', (req: AssRequest, res: AssResponse, next) => {
|
|
|
|
let oldName, oldType; // skipcq: JS-0119
|
|
|
|
let oldName: string, oldType: string; // skipcq: JS-0119
|
|
|
|
data.get(req.ass.resourceId)
|
|
|
|
data.get(req.ass?.resourceId)
|
|
|
|
.then((fileData) => {
|
|
|
|
.then((fileData: FileData) => {
|
|
|
|
// Extract info for logs
|
|
|
|
// Extract info for logs
|
|
|
|
oldName = fileData.originalname;
|
|
|
|
oldName = fileData.originalname;
|
|
|
|
oldType = fileData.mimetype;
|
|
|
|
oldType = fileData.mimetype;
|
|
|
@ -117,7 +119,7 @@ router.get('/delete/:deleteId', (req, res, next) => {
|
|
|
|
(!fileData.is || (fileData.is.image || fileData.is.video)) && fs.existsSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail))
|
|
|
|
(!fileData.is || (fileData.is.image || fileData.is.video)) && fs.existsSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail))
|
|
|
|
? fs.rmSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail)) : () => Promise.resolve()]);
|
|
|
|
? 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
|
|
|
|
.then(() => (log.success('Deleted', oldName, oldType), res.type('text').send('File has been deleted!'))) // skipcq: JS-0090
|
|
|
|
.catch(next);
|
|
|
|
.catch(next);
|
|
|
|
});
|
|
|
|
});
|