From 60f7a86a45e32239f97c845864fbde7ab0104d7b Mon Sep 17 00:00:00 2001
From: Samuel Gaist <samuel.gaist@idiap.ch>
Date: Thu, 10 Sep 2020 14:20:06 +0200
Subject: [PATCH] [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.
---
 beat/web/common/storage.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/beat/web/common/storage.py b/beat/web/common/storage.py
index 8cd096574..0ce2d8c3e 100755
--- a/beat/web/common/storage.py
+++ b/beat/web/common/storage.py
@@ -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
 
     Parameters:
@@ -81,8 +81,12 @@ def get_file_content(instance, attr):
     if f is None:
         return ""
 
+    mode = "rt"
+    if binary:
+        mode = "rb"
+
     try:
-        f.open()
+        f.open(mode)
     except Exception:
         return ""
 
-- 
GitLab