[FIX] web: prevent upload of large files

This commit ensures that files uploaded using the FileInput, upload
service or AttachDocumentWidget will not exceed the maximum file size of
the context in size before sending them to the server. It also moves
some utils from the formatters file to the numbers file so that one
can use them without requiring the view assets.

task-3390746

closes odoo/odoo#126914

Related: odoo/enterprise#45629
Signed-off-by: Samuel Degueldre (sad) <sad@odoo.com>
This commit is contained in:
Julien Carion (juca)
2023-08-11 14:32:00 +02:00
parent cc5a14b6a9
commit 2a72028ce6
8 changed files with 196 additions and 112 deletions
@@ -3,14 +3,15 @@
import { registry } from '@web/core/registry';
import { UploadProgressToast } from './upload_progress_toast';
import utils from '@web/legacy/js/core/utils';
import { checkFileSize } from "@web/core/utils/files";
import { humanNumber } from "@web/core/utils/numbers";
import { reactive } from "@odoo/owl";
export const AUTOCLOSE_DELAY = 3000;
export const uploadService = {
dependencies: ['rpc'],
start(env, { rpc }) {
dependencies: ['rpc', 'notification'],
start(env, { rpc, notification }) {
let fileId = 0;
const progressToast = reactive({
files: {},
@@ -69,14 +70,13 @@ export const uploadService = {
const sortedFiles = Array.from(files).sort((a, b) => a.size - b.size);
for (const file of sortedFiles) {
let fileSize = file.size;
if (!checkFileSize(fileSize, notification)) {
return null;
}
if (!fileSize) {
fileSize = null;
} else if (fileSize < 1024) {
fileSize = fileSize.toFixed(2) + " bytes";
} else if (fileSize < 1048576) {
fileSize = (fileSize / 1024).toFixed(2) + " KB";
} else {
fileSize = (fileSize / 1048576).toFixed(2) + " MB";
fileSize = humanNumber(fileSize) + "B";
}
const id = ++fileId;