first push message
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
from odoo import models, _, api, fields
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class IrAttachment(models.Model):
|
||||
_inherit = 'ir.attachment'
|
||||
|
||||
def create(self, vals_list):
|
||||
# 1. Get the limit from system parameters (set via res.config.settings)
|
||||
# Default to 2048 MB (2 GB) if not set in settings
|
||||
max_size_mb = float(self.env['ir.config_parameter'].sudo().get_param(
|
||||
'attachment_size.max_attachment_size', default=2048.0
|
||||
))
|
||||
|
||||
# 2. Convert MB to bytes
|
||||
max_size_bytes = max_size_mb * 1024 * 1024
|
||||
|
||||
# 3. Validate file size
|
||||
for vals in vals_list:
|
||||
if vals.get('datas'):
|
||||
# Base64 increases size by ~33%, so we check against max_size_bytes * 4 / 3
|
||||
if len(vals['datas']) > (max_size_bytes * 4 / 3):
|
||||
raise UserError(_(
|
||||
"File size exceeds the maximum allowed limit of %(max_size)s MB.",
|
||||
max_size=max_size_mb
|
||||
))
|
||||
|
||||
return super().create(vals_list)
|
||||
|
||||
|
||||
class FileSizeConfigSetting(models.TransientModel):
|
||||
_inherit = 'res.config.settings'
|
||||
|
||||
max_attachment_size = fields.Float(
|
||||
string="Maximum Attachment Size (MB)",
|
||||
config_parameter='attachment_size.max_attachment_size',
|
||||
help="Set the maximum size for attachments in MB. Default is 2048 MB (2 GB)."
|
||||
)
|
||||
|
||||
|
||||
class Http(models.AbstractModel):
|
||||
_inherit = "ir.http"
|
||||
|
||||
@api.model
|
||||
def session_info(self):
|
||||
session_info = super().session_info()
|
||||
|
||||
# Get the limit from system parameters, default to 2048 MB (2 GB)
|
||||
max_size_mb = float(self.env['ir.config_parameter'].sudo().get_param(
|
||||
'attachment_size.max_attachment_size', default=2048.0
|
||||
))
|
||||
|
||||
max_attachment_size_bytes = max_size_mb * 1024 * 1024 # Convert MB to Bytes
|
||||
|
||||
# Always update session_info so JS always has a value (no more undefined)
|
||||
session_info.update({
|
||||
"max_attachment_size": max_attachment_size_bytes,
|
||||
"max_size": max_size_mb,
|
||||
"max_file_upload_size": max_attachment_size_bytes,
|
||||
})
|
||||
|
||||
return session_info
|
||||
Reference in New Issue
Block a user