diff --git a/app/forms/forum.py b/app/forms/forum.py index ebaf849..c9ab038 100644 --- a/app/forms/forum.py +++ b/app/forms/forum.py @@ -8,7 +8,7 @@ class CommentForm(FlaskForm): message = TextAreaField('Message', validators=[DataRequired()]) attachments = MultipleFileField('Pièces-jointes', validators=[vd.file.optional, vd.file.count, vd.file.extension, - vd.file.size]) + vd.file.size, vd.file.namelength]) submit = SubmitField('Commenter') preview = SubmitField('Prévisualiser') diff --git a/app/utils/validators/file.py b/app/utils/validators/file.py index 747ca0e..fd1f743 100644 --- a/app/utils/validators/file.py +++ b/app/utils/validators/file.py @@ -49,3 +49,13 @@ def size(form, files): else: if size > 500e3: # 500 ko per comment for a guest raise ValidationError("Fichiers trop lourds (max 500 ko)") + +def namelength(form, files): + errors = [] + for f in files.data: + name = secure_filename(f.filename) + if len(name) > 64: + errors.append(f.filename) + if len(errors) > 0: + raise ValidationError(f"Noms trop longs, 64 caractères max " \ + f"({', '.join(errors)})")