Merge pull request #593 from expsa/dev_base_attch_preview

[UPD] apply file max size restrication from (size_restriction_for_att…
This commit is contained in:
Tahir Hassan 2024-08-08 12:15:17 +04:00 committed by GitHub
commit be155754bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 23 deletions

View File

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

View File

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