Merge pull request #3485 from thelounge/xpaw/upload-baseurl

Allow configuring base url for uploads
This commit is contained in:
Pavel Djundik 2019-11-01 13:16:13 +02:00 committed by GitHub
commit eb971a7d23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View File

@ -154,9 +154,16 @@ module.exports = {
// this limit will be prompted with an error message in their browser. A value of // this limit will be prompted with an error message in their browser. A value of
// `-1` disables the file size limit and allows files of any size. **Use at // `-1` disables the file size limit and allows files of any size. **Use at
// your own risk.** This value is set to `10240` kilobytes by default. // your own risk.** This value is set to `10240` kilobytes by default.
// - `baseUrl`: If you want change the URL where uploaded files are accessed,
// you can set this option to `"https://example.com/folder/"` and the final URL
// would look like `"https://example.com/folder/aabbccddeeff1234/name.png"`.
// If you use this option, you must have a reverse proxy configured,
// to correctly proxy the uploads URLs back to The Lounge.
// This value is set to `null` by default.
fileUpload: { fileUpload: {
enable: false, enable: false,
maxFileSize: 10240, maxFileSize: 10240,
baseUrl: null,
}, },
// ### `transports` // ### `transports`

View File

@ -135,6 +135,16 @@ function setHome(newPath) {
); );
} }
if (this.config.fileUpload.baseUrl) {
try {
new URL("test/file.png", this.config.fileUpload.baseUrl);
} catch (e) {
this.config.fileUpload.baseUrl = null;
log.warn(`The ${colors.bold("fileUpload.baseUrl")} you specified is invalid: ${e}`);
}
}
const manifestPath = path.resolve( const manifestPath = path.resolve(
path.join(__dirname, "..", "public", "thelounge.webmanifest") path.join(__dirname, "..", "public", "thelounge.webmanifest")
); );

View File

@ -179,7 +179,13 @@ class Uploader {
streamWriter.on("error", abortWithError); streamWriter.on("error", abortWithError);
busboyInstance.on("file", (fieldname, fileStream, filename) => { busboyInstance.on("file", (fieldname, fileStream, filename) => {
uploadUrl = `uploads/${randomName}/${encodeURIComponent(filename)}`; uploadUrl = `${randomName}/${encodeURIComponent(filename)}`;
if (Helper.config.fileUpload.baseUrl) {
uploadUrl = new URL(uploadUrl, Helper.config.fileUpload.baseUrl).toString();
} else {
uploadUrl = `uploads/${uploadUrl}`;
}
// if the busboy data stream errors out or goes over the file size limit // if the busboy data stream errors out or goes over the file size limit
// abort the processing with an error // abort the processing with an error