Skip to content

Commit ebdf82a

Browse files
committed
use an api call to get max_upload_size and max_upload_time
fix elabftw#5232 There was a problem with this setting being a build arg
1 parent 56c1658 commit ebdf82a

File tree

8 files changed

+27
-19
lines changed

8 files changed

+27
-19
lines changed

apidoc/v2/openapi.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -1715,13 +1715,23 @@ paths:
17151715
type: object
17161716
properties:
17171717
allowed_extensions:
1718+
description: allowlist of file extensions accepted for import
17181719
type: array
17191720
items:
17201721
type: string
17211722
example: [".eln", ".csv"]
17221723
max_filesize:
1724+
description: maximum size of an uploaded file in bytes
17231725
type: integer
17241726
example: 2147483648
1727+
max_upload_size:
1728+
description: value of MAX_UPLOAD_SIZE env parameter
1729+
type: string
1730+
example: 3G
1731+
max_upload_time:
1732+
description: value of MAX_UPLOAD_TIME env parameter
1733+
type: integer
1734+
example: 900000
17251735
post:
17261736
tags: ['Import']
17271737
summary: Send a file to import

builder.js

-5
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ module.exports = (env) => {
9494
],
9595
},
9696
plugins: [
97-
// https://webpack.js.org/plugins/define-plugin/
98-
new webpack.DefinePlugin({
99-
'__MAX_UPLOAD_SIZE__': JSON.stringify(env.MAX_UPLOAD_SIZE),
100-
'__MAX_UPLOAD_TIME__': JSON.stringify(env.MAX_UPLOAD_TIME),
101-
}),
10297
new MiniCssExtractPlugin(
10398
{
10499
filename: 'vendor.min.css',

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@
8181
"apidocv2": "docker run --name swagger --rm -e SWAGGER_JSON=/c/openapi.yaml -v $(pwd)/apidoc/v2/:/c -p 8085:8080 -d swaggerapi/swagger-ui && echo 'APIv2 Doc: http://localhost:8085' && echo 'Run docker stop swagger to remove it.'",
8282
"brotli": "brotli -vf web/assets/*.css web/assets/*.js web/assets/*.svg web/assets/images/*.svg",
8383
"zopfli": "zopfli -i1000 web/assets/*.css web/assets/*.js web/assets/*.svg web/assets/images/*.svg",
84-
"build": "webpack-cli --env MAX_UPLOAD_SIZE=$MAX_UPLOAD_SIZE --env MAX_UPLOAD_TIME=$MAX_UPLOAD_TIME --progress --config builder.js",
85-
"build:dev": "webpack-cli --env MAX_UPLOAD_SIZE=$MAX_UPLOAD_SIZE --env MAX_UPLOAD_TIME=$MAX_UPLOAD_TIME --config builder.js",
86-
"build:prod": "webpack-cli --env MAX_UPLOAD_SIZE=$MAX_UPLOAD_SIZE --env MAX_UPLOAD_TIME=$MAX_UPLOAD_TIME --mode production --env production --config builder.js",
84+
"build": "webpack-cli --progress --config builder.js",
85+
"build:dev": "webpack-cli --config builder.js",
86+
"build:prod": "webpack-cli --mode production --env production --config builder.js",
8787
"buildnode": "webpack-cli --progress --config node-builder.js",
8888
"buildnode:dev": "webpack-cli --config node-builder.js",
8989
"buildall": "CYPRESS_INSTALL_BINARY=0 yarn install --immutable && yarn buildjs && yarn buildcss && yarn buildparser && yarn brotli && yarn zopfli",
@@ -120,7 +120,7 @@
120120
"unit": "tests/run.sh unit",
121121
"unit-ci": "php vendor/bin/codecept run unit",
122122
"watchpdfcss": "sass --quiet-deps --watch src/scss/pdf.scss:web/assets/pdf.min.css",
123-
"watchjs": "webpack-cli --env MAX_UPLOAD_SIZE=$MAX_UPLOAD_SIZE --env MAX_UPLOAD_TIME=$MAX_UPLOAD_TIME --config builder.js --mode=development --watch --progress",
123+
"watchjs": "webpack-cli --config builder.js --mode=development --watch --progress",
124124
"watchnode": "webpack-cli --config node-builder.js --mode=development --watch --progress"
125125
},
126126
"repository": "git+https://github.com/elabftw/elabftw.git",

src/Import/Handler.php

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Elabftw\Interfaces\ImportInterface;
2222
use Elabftw\Interfaces\RestInterface;
2323
use Elabftw\Models\AuditLogs;
24+
use Elabftw\Models\Config;
2425
use Elabftw\Models\Users;
2526
use Psr\Log\LoggerInterface;
2627
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -41,6 +42,8 @@ public function readOne(): array
4142
return array(
4243
'allowed_extensions' => self::ALLOWED_EXTENSIONS,
4344
'max_filesize' => UploadedFile::getMaxFilesize(),
45+
'max_upload_size' => Config::fromEnv('MAX_UPLOAD_SIZE'),
46+
'max_upload_time' => (int) Config::fromEnv('MAX_UPLOAD_TIME'),
4447
);
4548
}
4649

src/ts/edit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ document.addEventListener('DOMContentLoaded', async () => {
4545

4646
// initialize the file uploader
4747
const uploader = new Uploader();
48-
const dropZone = uploader.init();
48+
const dropZone = await uploader.init();
4949

5050
////////////////
5151
// DATA RECOVERY

src/ts/global.d.ts

-2
This file was deleted.

src/ts/misc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ export function sizeToMb(size: string): number {
674674
};
675675

676676
const regex = /^(\d+(?:\.\d+)?)([BKMGTPE]?)$/i;
677-
const match = size.match(regex);
677+
const match = size.toString().match(regex);
678678

679679
if (!match) {
680680
throw new Error('Invalid size format');

src/ts/uploader.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@
88
import Dropzone from '@deltablot/dropzone';
99
import { reloadElements, sizeToMb } from './misc';
1010
import i18next from 'i18next';
11+
import { Api } from './Apiv2.class';
1112

1213
export class Uploader
1314
{
1415
// holds the resolve function of tinymce image handler
1516
tinyImageSuccess: (value: string | PromiseLike<string>) => void;
1617

17-
getOptions() {
18+
async getOptions() {
1819
/* eslint-disable-next-line @typescript-eslint/no-this-alias */
1920
const that = this;
20-
const sizeInMb = sizeToMb(__MAX_UPLOAD_SIZE__);
21+
const importInfo = await (new Api()).getJson('import');
22+
const sizeInMb = sizeToMb(importInfo.max_upload_size);
2123
return {
2224
// i18n message to user
2325
dictDefaultMessage: `<i class='fas fa-upload'></i> ${i18next.t('dropzone-upload-area')}<br> ${i18next.t('dropzone-filesize-limit')} ${sizeInMb} MB`,
2426
maxFilesize: sizeInMb,
25-
timeout: __MAX_UPLOAD_TIME__,
27+
timeout: importInfo.max_upload_time,
2628
init: function(): void {
2729
// once upload is finished
2830
this.on('complete', function() {
@@ -39,15 +41,15 @@ export class Uploader
3941
// The 'undefined' check is not enough. That is just for before any file was pasted.
4042
that.tinyImageSuccess = null;
4143
}
42-
that.init();
44+
this.init();
4345
});
4446
}
4547
});
4648
},
4749
};
4850
}
4951

50-
init(): Dropzone {
52+
async init(): Promise<Dropzone> {
5153
// FIXME just added "as any" for now
5254
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5355
const dropzoneEl = document.getElementById('elabftw-dropzone') as any;
@@ -56,7 +58,7 @@ export class Uploader
5658
if (Object.prototype.hasOwnProperty.call(dropzoneEl, 'dropzone')) {
5759
return dropzoneEl.dropzone;
5860
}
59-
return new Dropzone(dropzoneEl, this.getOptions());
61+
return new Dropzone(dropzoneEl, await this.getOptions());
6062
}
6163
}
6264
}

0 commit comments

Comments
 (0)