From fc6f48c42ad9dbc424bb0a4ef6ca33e0aeeff500 Mon Sep 17 00:00:00 2001 From: Safwan <62411302+safwansamsudeen@users.noreply.github.com> Date: Tue, 3 Feb 2026 16:48:19 +0530 Subject: [PATCH] fix: check file type before decoding content (#36647) Closes: #36592 --- frappe/core/doctype/file/file.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index d7b4b8e247..927e4c6a70 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -9,6 +9,7 @@ import shutil import zipfile from urllib.parse import quote, unquote +import filetype from PIL import Image, ImageFile, ImageOps import frappe @@ -609,16 +610,19 @@ class File(Document): encodings = FILE_ENCODING_OPTIONS with open(file_path, mode="rb") as f: self._content = f.read() - # looping will not result in slowdown, as the content is usually utf-8 or utf-8-sig - # encoded so the first iteration will be enough most of the time - for encoding in encodings: - try: - # read file with proper encoding - self._content = self._content.decode(encoding) - break - except UnicodeDecodeError: - # for .png, .jpg, etc - continue + # Only decode if not a binary file + kind = filetype.guess(self._content) + if not kind: + # looping will not result in slowdown, as the content is usually utf-8 or utf-8-sig + # encoded so the first iteration will be enough most of the time + for encoding in encodings: + try: + # read file with proper encoding + self._content = self._content.decode(encoding) + break + except UnicodeDecodeError: + # for .png, .jpg, etc + continue return self._content