Skip to content

Commit

Permalink
fix utf-8 with bom, fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
Mate Laszlo Valko committed Apr 27, 2024
1 parent f5bfcfe commit 31e0238
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions frappe/core/doctype/file/file.py
Expand Up @@ -531,15 +531,18 @@ def get_content(self) -> bytes:
self.validate_file_url()
file_path = self.get_full_path()

# read the file
# read file with proper encoding
with open(file_path, mode="rb") as f:
self._content = f.read()
try:
# for plain text files
self._content = self._content.decode()
except UnicodeDecodeError:
# for .png, .jpg, etc
pass
encodings = ["utf-8-sig", "utf-8", "windows-1250", "windows-1252"]
for encoding in encodings:
try:
# for plain text files
self._content = self._content.decode(encoding)
break
except UnicodeDecodeError:
# for .png, .jpg, etc
continue

return self._content

Expand Down
4 changes: 2 additions & 2 deletions frappe/utils/csvutils.py
Expand Up @@ -40,7 +40,7 @@ def read_csv_content_from_attached_file(doc):
def read_csv_content(fcontent):
if not isinstance(fcontent, str):
decoded = False
for encoding in ["utf-8", "windows-1250", "windows-1252"]:
for encoding in ["utf-8-sig", "utf-8", "windows-1250", "windows-1252"]:
try:
fcontent = str(fcontent, encoding)
decoded = True
Expand Down Expand Up @@ -68,7 +68,7 @@ def read_csv_content(fcontent):
except csv.Error:
# if sniff fails, use default dialect
frappe.msgprint(
_("Delimiter detection failed. Try enable Custom d elimiters and adjust Delimiter options."),
_("Delimiter detection failed. Try enable Custom delimiters and adjust Delimiter options."),
indicator="orange",
alert=True,
)
Expand Down

0 comments on commit 31e0238

Please sign in to comment.