chore: get host from req instead of config
This commit is contained in:
parent
3358cf6939
commit
9b28e56e09
|
@ -15,7 +15,7 @@ class filesGET extends Route {
|
|||
.select('id', 'username', 'enabled', 'createdAt', 'editedAt', 'apiKeyEditedAt', 'isAdmin')
|
||||
.where({ id: file.userId })
|
||||
.first();
|
||||
file = Util.constructFilePublicLink(file);
|
||||
file = Util.constructFilePublicLink(req, file);
|
||||
|
||||
// Additional relevant data
|
||||
const filesFromUser = await db.table('files').where({ userId: user.id }).select('id');
|
||||
|
|
|
@ -37,7 +37,7 @@ class usersGET extends Route {
|
|||
}
|
||||
|
||||
for (let file of files) {
|
||||
file = Util.constructFilePublicLink(file);
|
||||
file = Util.constructFilePublicLink(req, file);
|
||||
}
|
||||
|
||||
return res.json({
|
||||
|
|
|
@ -43,7 +43,7 @@ class albumGET extends Route {
|
|||
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (let file of files) {
|
||||
file = Util.constructFilePublicLink(file);
|
||||
file = Util.constructFilePublicLink(req, file);
|
||||
}
|
||||
|
||||
return res.json({
|
||||
|
|
|
@ -44,7 +44,7 @@ class albumGET extends Route {
|
|||
}
|
||||
|
||||
for (let file of files) {
|
||||
file = Util.constructFilePublicLink(file);
|
||||
file = Util.constructFilePublicLink(req, file);
|
||||
}
|
||||
|
||||
// Add 1 more view to the link
|
||||
|
|
|
@ -37,7 +37,7 @@ class albumsGET extends Route {
|
|||
|
||||
// Fetch thumbnails and stuff
|
||||
for (let file of files) {
|
||||
file = Util.constructFilePublicLink(file);
|
||||
file = Util.constructFilePublicLink(req, file);
|
||||
}
|
||||
|
||||
album.fileCount = fileCount[0].count;
|
||||
|
|
|
@ -20,16 +20,6 @@ class linkPOST extends Route {
|
|||
.first();
|
||||
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;
|
||||
if (identifier) {
|
||||
if (!user.isAdmin) return res.status(401).json({ message: 'Only administrators can create custom links' });
|
||||
|
|
|
@ -16,7 +16,7 @@ class fileGET extends Route {
|
|||
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' });
|
||||
|
||||
file = Util.constructFilePublicLink(file);
|
||||
file = Util.constructFilePublicLink(req, file);
|
||||
|
||||
/*
|
||||
Fetch the albums
|
||||
|
|
|
@ -30,7 +30,7 @@ class filesGET extends Route {
|
|||
|
||||
// For each file, create the public link to be able to display the file
|
||||
for (let file of files) {
|
||||
file = Util.constructFilePublicLink(file);
|
||||
file = Util.constructFilePublicLink(req, file);
|
||||
}
|
||||
|
||||
return res.json({
|
||||
|
|
|
@ -53,7 +53,7 @@ class configGET extends Route {
|
|||
|
||||
// For each file, create the public link to be able to display the file
|
||||
for (let file of files) {
|
||||
file = Util.constructFilePublicLink(file);
|
||||
file = Util.constructFilePublicLink(req, file);
|
||||
}
|
||||
|
||||
return res.json({
|
||||
|
|
|
@ -11,7 +11,6 @@ class configGET extends Route {
|
|||
config: {
|
||||
serviceName: process.env.SERVICE_NAME,
|
||||
uploadFolder: process.env.UPLOAD_FOLDER,
|
||||
linksPerAlbum: parseInt(process.env.MAX_LINKS_PER_ALBUM, 10),
|
||||
maxUploadSize: parseInt(process.env.MAX_SIZE, 10),
|
||||
filenameLength: parseInt(process.env.GENERATED_FILENAME_LENGTH, 10),
|
||||
albumLinkLength: parseInt(process.env.GENERATED_ALBUM_LENGTH, 10),
|
||||
|
|
|
@ -282,8 +282,8 @@ class uploadPOST extends Route {
|
|||
|
||||
if (albumId) await Util.saveFileToAlbum(db, albumId, result.id);
|
||||
|
||||
result.file = Util.constructFilePublicLink(result.file);
|
||||
result.deleteUrl = `${process.env.DOMAIN}/api/file/${result.id[0]}`;
|
||||
result.file = Util.constructFilePublicLink(req, result.file);
|
||||
result.deleteUrl = `${Util.getHost(req)}/api/file/${result.id[0]}`;
|
||||
|
||||
return res.status(201).send({
|
||||
message: 'Sucessfully uploaded the file.',
|
||||
|
|
|
@ -47,13 +47,11 @@ class Util {
|
|||
rateLimitMax: process.env.RATE_LIMIT_MAX || 5,
|
||||
secret: process.env.SECRET || randomstring.generate(64),
|
||||
serviceName: process.env.SERVICE_NAME || 'change-me',
|
||||
domain: process.env.DOMAIN || `http://localhost:${process.env.SERVER_PORT}`,
|
||||
chunkSize: process.env.CHUNK_SIZE || 90,
|
||||
maxSize: process.env.MAX_SIZE || 5000,
|
||||
generateZips: process.env.GENERATE_ZIPS == undefined ? true : false,
|
||||
generatedFilenameLength: process.env.GENERATED_FILENAME_LENGTH || 12,
|
||||
generatedAlbumLength: process.env.GENERATED_ALBUM_LENGTH || 6,
|
||||
maxLinksPerAlbum: process.env.MAX_LINKS_PER_ALBUM || 5,
|
||||
uploadFolder: process.env.UPLOAD_FOLDER || 'uploads',
|
||||
blockedExtensions: process.env.BLOCKED_EXTENSIONS || ['.jar', '.exe', '.msi', '.com', '.bat', '.cmd', '.scr', '.ps1', '.sh'],
|
||||
publicMode: process.env.PUBLIC_MODE == undefined ? true : false,
|
||||
|
@ -92,17 +90,18 @@ class Util {
|
|||
return fileTypeMimeObj ? fileTypeMimeObj.mime : undefined;
|
||||
}
|
||||
|
||||
static constructFilePublicLink(file) {
|
||||
static constructFilePublicLink(req, file) {
|
||||
/*
|
||||
TODO: This wont work without a reverse proxy serving both
|
||||
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) || {};
|
||||
if (thumb) {
|
||||
file.thumb = `${process.env.DOMAIN}/thumbs/${thumb}`;
|
||||
file.thumbSquare = `${process.env.DOMAIN}/thumbs/square/${thumb}`;
|
||||
file.preview = preview && `${process.env.DOMAIN}/thumbs/preview/${preview}`;
|
||||
file.thumb = `${host}/thumbs/${thumb}`;
|
||||
file.thumbSquare = `${host}/thumbs/square/${thumb}`;
|
||||
file.preview = preview && `${host}/thumbs/preview/${preview}`;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
@ -265,8 +264,8 @@ class Util {
|
|||
|
||||
static generateThumbnails = ThumbUtil.generateThumbnails;
|
||||
|
||||
static async fileExists(res, exists, filename) {
|
||||
exists = Util.constructFilePublicLink(exists);
|
||||
static async fileExists(req, res, exists, filename) {
|
||||
exists = Util.constructFilePublicLink(req, exists);
|
||||
res.json({
|
||||
message: 'Successfully uploaded the file.',
|
||||
name: exists.name,
|
||||
|
@ -274,7 +273,7 @@ class Util {
|
|||
size: exists.size,
|
||||
url: exists.url,
|
||||
thumb: exists.thumb,
|
||||
deleteUrl: `${process.env.DOMAIN}/api/file/${exists.id}`,
|
||||
deleteUrl: `${this.getHost(req)}/api/file/${exists.id}`,
|
||||
repeated: true
|
||||
});
|
||||
|
||||
|
@ -298,7 +297,7 @@ class Util {
|
|||
.first();
|
||||
|
||||
if (dbFile) {
|
||||
await this.fileExists(res, dbFile, file.data.filename);
|
||||
await this.fileExists(req, res, dbFile, file.data.filename);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -406,6 +405,10 @@ class Util {
|
|||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
static getHost(req) {
|
||||
return `${req.protocol}://${req.headers.host}`;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Util;
|
||||
|
|
Loading…
Reference in New Issue