[UPD] apply file max size restrication from (size_restriction_for_attachments) module

This commit is contained in:
AHIDev 2024-08-08 12:11:10 +04:00
parent 5552b44fa4
commit f64924dc92
2 changed files with 58 additions and 23 deletions

View File

@ -11,7 +11,7 @@
'description': """ User can preview a document without downloading. """,
'price': 16,
'currency': 'USD',
'depends': [],
'depends': ['size_restriction_for_attachments'],
'data': [
'views/assets.xml'
],

View File

@ -7,7 +7,9 @@ var field_registry = require('web.field_registry');
var core = require('web.core');
var relational_fields = require('web.relational_fields');
var DocumentViewer = require('odx_m2m_attachment_preview.DocumentViewer');
var session = require('web.session');
var rpc = require('web.rpc');
var Dialog = require('web.Dialog');
var _t = core._t;
var _lt = core._lt;
@ -36,7 +38,7 @@ var FieldMany2ManyAttachmentPreview = AbstractField.extend({
/**
* @constructor
*/
init: function () {
init: function () {
this._super.apply(this, arguments);
if (this.field.type !== 'many2many' || this.field.relation !== 'ir.attachment') {
@ -49,8 +51,9 @@ var FieldMany2ManyAttachmentPreview = AbstractField.extend({
this.fileupload_id = _.uniqueId('oe_fileupload_temp');
this.accepted_file_extensions = (this.nodeOptions && this.nodeOptions.accepted_file_extensions) || this.accepted_file_extensions || '*';
$(window).on(this.fileupload_id, this._onFileLoaded.bind(this));
this.fileMaxSize = 0;
this.metadata = {};
this._getMaxFileSize();
},
destroy: function () {
@ -96,7 +99,6 @@ var FieldMany2ManyAttachmentPreview = AbstractField.extend({
_render: function () {
// render the attachments ; as the attachments will changes after each
// _setValue, we put the rendering here to ensure they will be updated
this._generatedMetadata();
this.$('.oe_placeholder_files, .o_attachments')
.replaceWith($(qweb.render(this.template_files, {
@ -159,6 +161,7 @@ var FieldMany2ManyAttachmentPreview = AbstractField.extend({
var files = ev.target.files;
var attachment_ids = this.value.res_ids;
var file_status = true;
// Don't create an attachment if the upload window is cancelled.
if(files.length === 0)
@ -168,30 +171,46 @@ var FieldMany2ManyAttachmentPreview = AbstractField.extend({
var record = _.find(self.value.data, function (attachment) {
return attachment.data.name === file.name;
});
if (record) {
var metadata = self.metadata[record.id];
if (!metadata || metadata.allowUnlink) {
// there is a existing attachment with the same name so we
// replace it
attachment_ids = _.without(attachment_ids, record.res_id);
self._rpc({
model: 'ir.attachment',
method: 'unlink',
args: [record.res_id],
// check max file size
if (self.fileMaxSize !== 0) {
if(file.size > self.fileMaxSize){
file_status = false;
Dialog.alert(this, _.str.sprintf(_t('The selected file exceeds the maximum file size of %s MB'),(self.fileMaxSize/1024/1024)), {
title: _t("Validation Error"),
});
return false;
}
}
self.uploadingFiles.push(file);
if(file_status){
if (record) {
var metadata = self.metadata[record.id];
if (!metadata || metadata.allowUnlink) {
// there is a existing attachment with the same name so we
// replace it
attachment_ids = _.without(attachment_ids, record.res_id);
self._rpc({
model: 'ir.attachment',
method: 'unlink',
args: [record.res_id],
});
}
}
self.uploadingFiles.push(file);
}
});
this._setValue({
operation: 'REPLACE_WITH',
ids: attachment_ids,
});
if(file_status){
this._setValue({
operation: 'REPLACE_WITH',
ids: attachment_ids,
});
this.$('form.o_form_binary_form').submit();
this.$('.oe_fileupload').hide();
ev.target.value = "";
this.$('form.o_form_binary_form').submit();
this.$('.oe_fileupload').hide();
ev.target.value = "";
}
},
/**
* @private
@ -242,6 +261,22 @@ var FieldMany2ManyAttachmentPreview = AbstractField.extend({
var attachmentViewer = new DocumentViewer(this, this.attachments, activeAttachmentID);
attachmentViewer.appendTo($('body'));
})
},
_getMaxFileSize: async function () {
var self = this;
await rpc.query({
model: 'res.users',
method: 'read',
args: [[session.uid], ['set_restriction','max_size']],
}).then(function (result) {
if(result[0].set_restriction){
self.fileMaxSize = result[0].max_size * 1024 * 1024;
}
});
}
});