← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands-website/fix_map_upload into lp:widelands-website

 

kaputtnik has proposed merging lp:~widelands-dev/widelands-website/fix_map_upload into lp:widelands-website.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1533789 in Widelands Website: "Upload of Map with German Umlaut fails"
  https://bugs.launchpad.net/widelands-website/+bug/1533789

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands-website/fix_map_upload/+merge/312300

This should fix bug 1533789 (Upload with german umlauts fails)

This is just a try, because the server error couldn't be reproduced here locally.

This fix uses Djangos get_valid_name() of the storage class to get safe filenames.

I have also renamed some vars.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/fix_map_upload into lp:widelands-website.
=== modified file 'wlmaps/forms.py'
--- wlmaps/forms.py	2016-02-15 14:06:09 +0000
+++ wlmaps/forms.py	2016-12-01 20:39:47 +0000
@@ -7,7 +7,6 @@
 from django import forms
 from django.forms import ModelForm
 from django.core.files.storage import default_storage
-from django.core.files.base import ContentFile
 
 from settings import MEDIA_ROOT
 from wlmaps.models import Map
@@ -22,26 +21,26 @@
     def clean(self):
         cleaned_data = super(UploadMapForm, self).clean()
 
-        file = cleaned_data.get('file')
-        if not file:
+        mem_file_obj = cleaned_data.get('file')
+        if not mem_file_obj:
             # no clean file => abort
             return cleaned_data
 
-        name = MEDIA_ROOT + 'wlmaps/maps/' + file.name
-        default_storage.save(name, ContentFile(file.read()))
+        file_path = MEDIA_ROOT + 'wlmaps/maps/' + default_storage.get_valid_name(mem_file_obj.name)
+        saved_file = default_storage.save(file_path, mem_file_obj)
         try:
             # call map info tool to generate minimap and json info file
-            check_call(['wl_map_info', name])
+            check_call(['wl_map_info', saved_file])
             # TODO(shevonar): delete file because it will be saved again when
             # the model is saved. File should not be saved twice
-            default_storage.delete(name)
+            default_storage.delete(saved_file)
         except CalledProcessError:
             self._errors['file'] = self.error_class(
                 ['The map file could not be processed.'])
             del cleaned_data['file']
             return cleaned_data
 
-        mapinfo = json.load(open(name + '.json'))
+        mapinfo = json.load(open(saved_file + '.json'))
 
         if Map.objects.filter(name=mapinfo['name']):
             self._errors['file'] = self.error_class(
@@ -60,10 +59,10 @@
 
         self.instance.world_name = mapinfo['world_name']
         # mapinfo["minimap"] is an absolute path and cannot be used.
-        self.instance.minimap = '/wlmaps/maps/' + file.name + '.png'
+        self.instance.minimap = '/wlmaps/maps/' + default_storage.get_valid_name(mem_file_obj.name) + '.png'
 
         # the json file is no longer needed
-        default_storage.delete(name + '.json')
+        default_storage.delete(saved_file + '.json')
         
         return cleaned_data
 


Follow ups