Browse Source

Merge pull request #1191 from rafalp/make-theme-import-more-memory-efficient

Don't read imported asset into memory
Rafał Pitoń 6 years ago
parent
commit
afb4e4c827
1 changed files with 11 additions and 5 deletions
  1. 11 5
      misago/themes/admin/importer.py

+ 11 - 5
misago/themes/admin/importer.py

@@ -3,7 +3,7 @@ import os
 from tempfile import TemporaryDirectory
 from tempfile import TemporaryDirectory
 from zipfile import BadZipFile, ZipFile
 from zipfile import BadZipFile, ZipFile
 
 
-from django.core.files.uploadedfile import SimpleUploadedFile
+from django.core.files.uploadedfile import UploadedFile
 from django.utils.translation import gettext as _, gettext_lazy
 from django.utils.translation import gettext as _, gettext_lazy
 
 
 from ..models import Theme
 from ..models import Theme
@@ -196,8 +196,11 @@ def create_css_from_manifest(tmp_dir, theme, manifest):
             theme.css.create(**item, order=theme.css.count())
             theme.css.create(**item, order=theme.css.count())
         else:
         else:
             with open(item["path"], "rb") as fp:
             with open(item["path"], "rb") as fp:
-                file_obj = SimpleUploadedFile(
-                    item["name"], fp.read(), content_type="text/css"
+                file_obj = UploadedFile(
+                    fp,
+                    name=item["name"],
+                    content_type="text/css",
+                    size=os.path.getsize(item["path"]),
                 )
                 )
                 create_css(theme, file_obj)
                 create_css(theme, file_obj)
 
 
@@ -205,7 +208,10 @@ def create_css_from_manifest(tmp_dir, theme, manifest):
 def create_media_from_manifest(tmp_dir, theme, manifest):
 def create_media_from_manifest(tmp_dir, theme, manifest):
     for item in manifest:
     for item in manifest:
         with open(item["path"], "rb") as fp:
         with open(item["path"], "rb") as fp:
-            file_obj = SimpleUploadedFile(
-                item["name"], fp.read(), content_type=item["type"]
+            file_obj = UploadedFile(
+                fp,
+                name=item["name"],
+                content_type=item["type"],
+                size=os.path.getsize(item["path"]),
             )
             )
             create_media(theme, file_obj)
             create_media(theme, file_obj)