Skip to content
Snippets Groups Projects
Commit 60f7a86a authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[common][storage] Ensure get_file_content returns what is expected

The method is written and documented to return the file
content as a string. However it's also used to read the
content of C++ binary blob.

Therefore, a parameter has been added to be able to
select opening mode and by default it's text.
parent 69c7b757
No related branches found
No related tags found
2 merge requests!358File field reading,!342Django 3 migration
...@@ -61,7 +61,7 @@ class OverwriteStorage(FileSystemStorage): ...@@ -61,7 +61,7 @@ class OverwriteStorage(FileSystemStorage):
# ---------------------------------------------------------- # ----------------------------------------------------------
def get_file_content(instance, attr): def get_file_content(instance, attr, binary=False):
"""Reads the contents of a Django-FileField attribute """Reads the contents of a Django-FileField attribute
Parameters: Parameters:
...@@ -81,8 +81,12 @@ def get_file_content(instance, attr): ...@@ -81,8 +81,12 @@ def get_file_content(instance, attr):
if f is None: if f is None:
return "" return ""
mode = "rt"
if binary:
mode = "rb"
try: try:
f.open() f.open(mode)
except Exception: except Exception:
return "" return ""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment