chore: get host from req instead of config

This commit is contained in:
Pitu 2021-06-08 00:33:01 +09:00
parent 3358cf6939
commit 9b28e56e09
12 changed files with 24 additions and 32 deletions

View File

@ -15,7 +15,7 @@ class filesGET extends Route {
.select('id', 'username', 'enabled', 'createdAt', 'editedAt', 'apiKeyEditedAt', 'isAdmin') .select('id', 'username', 'enabled', 'createdAt', 'editedAt', 'apiKeyEditedAt', 'isAdmin')
.where({ id: file.userId }) .where({ id: file.userId })
.first(); .first();
file = Util.constructFilePublicLink(file); file = Util.constructFilePublicLink(req, file);
// Additional relevant data // Additional relevant data
const filesFromUser = await db.table('files').where({ userId: user.id }).select('id'); const filesFromUser = await db.table('files').where({ userId: user.id }).select('id');

View File

@ -37,7 +37,7 @@ class usersGET extends Route {
} }
for (let file of files) { for (let file of files) {
file = Util.constructFilePublicLink(file); file = Util.constructFilePublicLink(req, file);
} }
return res.json({ return res.json({

View File

@ -43,7 +43,7 @@ class albumGET extends Route {
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
for (let file of files) { for (let file of files) {
file = Util.constructFilePublicLink(file); file = Util.constructFilePublicLink(req, file);
} }
return res.json({ return res.json({

View File

@ -44,7 +44,7 @@ class albumGET extends Route {
} }
for (let file of files) { for (let file of files) {
file = Util.constructFilePublicLink(file); file = Util.constructFilePublicLink(req, file);
} }
// Add 1 more view to the link // Add 1 more view to the link

View File

@ -37,7 +37,7 @@ class albumsGET extends Route {
// Fetch thumbnails and stuff // Fetch thumbnails and stuff
for (let file of files) { for (let file of files) {
file = Util.constructFilePublicLink(file); file = Util.constructFilePublicLink(req, file);
} }
album.fileCount = fileCount[0].count; album.fileCount = fileCount[0].count;

View File

@ -20,16 +20,6 @@ class linkPOST extends Route {
.first(); .first();
if (!exists) return res.status(400).json({ message: 'Album doesn\t exist' }); if (!exists) return res.status(400).json({ message: 'Album doesn\t exist' });
/*
Count the amount of links created for that album already and error out if max was reached
*/
const count = await db
.table('links')
.where('albumId', albumId)
.count({ count: 'id' })
.first();
if (count >= parseInt(process.env.MAX_LINKS_PER_ALBUM, 10)) return res.status(400).json({ message: 'Maximum links per album reached' });
let { identifier } = req.body; let { identifier } = req.body;
if (identifier) { if (identifier) {
if (!user.isAdmin) return res.status(401).json({ message: 'Only administrators can create custom links' }); if (!user.isAdmin) return res.status(401).json({ message: 'Only administrators can create custom links' });

View File

@ -16,7 +16,7 @@ class fileGET extends Route {
let file = await db.table('files').where({ id, userId: user.id }).first(); let file = await db.table('files').where({ id, userId: user.id }).first();
if (!file) return res.status(400).json({ message: 'The file doesn\'t exist or doesn\'t belong to the user' }); if (!file) return res.status(400).json({ message: 'The file doesn\'t exist or doesn\'t belong to the user' });
file = Util.constructFilePublicLink(file); file = Util.constructFilePublicLink(req, file);
/* /*
Fetch the albums Fetch the albums

View File

@ -30,7 +30,7 @@ class filesGET extends Route {
// For each file, create the public link to be able to display the file // For each file, create the public link to be able to display the file
for (let file of files) { for (let file of files) {
file = Util.constructFilePublicLink(file); file = Util.constructFilePublicLink(req, file);
} }
return res.json({ return res.json({

View File

@ -53,7 +53,7 @@ class configGET extends Route {
// For each file, create the public link to be able to display the file // For each file, create the public link to be able to display the file
for (let file of files) { for (let file of files) {
file = Util.constructFilePublicLink(file); file = Util.constructFilePublicLink(req, file);
} }
return res.json({ return res.json({

View File

@ -11,7 +11,6 @@ class configGET extends Route {
config: { config: {
serviceName: process.env.SERVICE_NAME, serviceName: process.env.SERVICE_NAME,
uploadFolder: process.env.UPLOAD_FOLDER, uploadFolder: process.env.UPLOAD_FOLDER,
linksPerAlbum: parseInt(process.env.MAX_LINKS_PER_ALBUM, 10),
maxUploadSize: parseInt(process.env.MAX_SIZE, 10), maxUploadSize: parseInt(process.env.MAX_SIZE, 10),
filenameLength: parseInt(process.env.GENERATED_FILENAME_LENGTH, 10), filenameLength: parseInt(process.env.GENERATED_FILENAME_LENGTH, 10),
albumLinkLength: parseInt(process.env.GENERATED_ALBUM_LENGTH, 10), albumLinkLength: parseInt(process.env.GENERATED_ALBUM_LENGTH, 10),

View File

@ -282,8 +282,8 @@ class uploadPOST extends Route {
if (albumId) await Util.saveFileToAlbum(db, albumId, result.id); if (albumId) await Util.saveFileToAlbum(db, albumId, result.id);
result.file = Util.constructFilePublicLink(result.file); result.file = Util.constructFilePublicLink(req, result.file);
result.deleteUrl = `${process.env.DOMAIN}/api/file/${result.id[0]}`; result.deleteUrl = `${Util.getHost(req)}/api/file/${result.id[0]}`;
return res.status(201).send({ return res.status(201).send({
message: 'Sucessfully uploaded the file.', message: 'Sucessfully uploaded the file.',

View File

@ -47,13 +47,11 @@ class Util {
rateLimitMax: process.env.RATE_LIMIT_MAX || 5, rateLimitMax: process.env.RATE_LIMIT_MAX || 5,
secret: process.env.SECRET || randomstring.generate(64), secret: process.env.SECRET || randomstring.generate(64),
serviceName: process.env.SERVICE_NAME || 'change-me', serviceName: process.env.SERVICE_NAME || 'change-me',
domain: process.env.DOMAIN || `http://localhost:${process.env.SERVER_PORT}`,
chunkSize: process.env.CHUNK_SIZE || 90, chunkSize: process.env.CHUNK_SIZE || 90,
maxSize: process.env.MAX_SIZE || 5000, maxSize: process.env.MAX_SIZE || 5000,
generateZips: process.env.GENERATE_ZIPS == undefined ? true : false, generateZips: process.env.GENERATE_ZIPS == undefined ? true : false,
generatedFilenameLength: process.env.GENERATED_FILENAME_LENGTH || 12, generatedFilenameLength: process.env.GENERATED_FILENAME_LENGTH || 12,
generatedAlbumLength: process.env.GENERATED_ALBUM_LENGTH || 6, generatedAlbumLength: process.env.GENERATED_ALBUM_LENGTH || 6,
maxLinksPerAlbum: process.env.MAX_LINKS_PER_ALBUM || 5,
uploadFolder: process.env.UPLOAD_FOLDER || 'uploads', uploadFolder: process.env.UPLOAD_FOLDER || 'uploads',
blockedExtensions: process.env.BLOCKED_EXTENSIONS || ['.jar', '.exe', '.msi', '.com', '.bat', '.cmd', '.scr', '.ps1', '.sh'], blockedExtensions: process.env.BLOCKED_EXTENSIONS || ['.jar', '.exe', '.msi', '.com', '.bat', '.cmd', '.scr', '.ps1', '.sh'],
publicMode: process.env.PUBLIC_MODE == undefined ? true : false, publicMode: process.env.PUBLIC_MODE == undefined ? true : false,
@ -92,17 +90,18 @@ class Util {
return fileTypeMimeObj ? fileTypeMimeObj.mime : undefined; return fileTypeMimeObj ? fileTypeMimeObj.mime : undefined;
} }
static constructFilePublicLink(file) { static constructFilePublicLink(req, file) {
/* /*
TODO: This wont work without a reverse proxy serving both TODO: This wont work without a reverse proxy serving both
the site and the API under the same domain. Pls fix. the site and the API under the same domain. Pls fix.
*/ */
file.url = `${process.env.DOMAIN}/${file.name}`; const host = this.getHost(req);
file.url = `${host}/${file.name}`;
const { thumb, preview } = ThumbUtil.getFileThumbnail(file.name) || {}; const { thumb, preview } = ThumbUtil.getFileThumbnail(file.name) || {};
if (thumb) { if (thumb) {
file.thumb = `${process.env.DOMAIN}/thumbs/${thumb}`; file.thumb = `${host}/thumbs/${thumb}`;
file.thumbSquare = `${process.env.DOMAIN}/thumbs/square/${thumb}`; file.thumbSquare = `${host}/thumbs/square/${thumb}`;
file.preview = preview && `${process.env.DOMAIN}/thumbs/preview/${preview}`; file.preview = preview && `${host}/thumbs/preview/${preview}`;
} }
return file; return file;
} }
@ -265,8 +264,8 @@ class Util {
static generateThumbnails = ThumbUtil.generateThumbnails; static generateThumbnails = ThumbUtil.generateThumbnails;
static async fileExists(res, exists, filename) { static async fileExists(req, res, exists, filename) {
exists = Util.constructFilePublicLink(exists); exists = Util.constructFilePublicLink(req, exists);
res.json({ res.json({
message: 'Successfully uploaded the file.', message: 'Successfully uploaded the file.',
name: exists.name, name: exists.name,
@ -274,7 +273,7 @@ class Util {
size: exists.size, size: exists.size,
url: exists.url, url: exists.url,
thumb: exists.thumb, thumb: exists.thumb,
deleteUrl: `${process.env.DOMAIN}/api/file/${exists.id}`, deleteUrl: `${this.getHost(req)}/api/file/${exists.id}`,
repeated: true repeated: true
}); });
@ -298,7 +297,7 @@ class Util {
.first(); .first();
if (dbFile) { if (dbFile) {
await this.fileExists(res, dbFile, file.data.filename); await this.fileExists(req, res, dbFile, file.data.filename);
return; return;
} }
@ -406,6 +405,10 @@ class Util {
console.error(error); console.error(error);
} }
} }
static getHost(req) {
return `${req.protocol}://${req.headers.host}`;
}
} }
module.exports = Util; module.exports = Util;