Source

flask-uploads / flaskext / uploads.py

Diff from to

flaskext/uploads.py

         return ((ext in self.config.allow) or
                 (ext in self.extensions and ext not in self.config.deny))
     
-    def save(self, storage, folder=None, name=None):
+    def save(self, storage, folder=None, destination=None):
         """
         This saves a `werkzeug.FileStorage` into this upload set. If the
         upload is not allowed, an `UploadNotAllowed` error will be raised.
         
         :param storage: The uploaded file to save.
         :param folder: The subfolder within the upload set to save to.
-        :param name: The name to save the file as. If it ends with a dot, the
-                     file's extension will be appended to the end. (If you
-                     are using `name`, you can include the folder in the
-                     `name` instead of explicitly using `folder`, i.e.
-                     ``uset.save(file, name="someguy/photo_123.")``
+        :param destination: The name to save the file as or a file object.
+                            If the name ends with a a dot, the
+                            file's extension will be appended to the end. (If you
+                            are using `name`, you can include the folder in the
+                            `name` instead of explicitly using `folder`, i.e.
+                            ``uset.save(file, name="someguy/photo_123.")``
         """
         if not isinstance(storage, FileStorage):
             raise TypeError("storage must be a werkzeug.FileStorage")
         
-        if folder is None and name is not None and "/" in name:
-            folder, name = name.rsplit("/", 1)
-        
         basename = lowercase_ext(secure_filename(storage.filename))
-        if name:
+        dst = None
+        if destination:
+            if isinstance(destination, basestring):
+                name = destination
+            else:
+                name = FileStorage(destination).filename
+                dst = destination
+     
+            if folder is None and name is not None and "/" in name:
+                folder, name = name.rsplit("/", 1)
+
             if name.endswith('.'):
                 basename = name + extension(basename)
             else:
                 basename = name
-        
+        else:
+            name = destination
+
         if not self.file_allowed(storage, basename):
             raise UploadNotAllowed()
         
             target_folder = self.config.destination
         if not os.path.exists(target_folder):
             os.makedirs(target_folder)
-        if os.path.exists(os.path.join(target_folder, basename)):
+        if os.path.exists(os.path.join(target_folder, basename)) and not dst:
             basename = self.resolve_conflict(target_folder, basename)
         
-        target = os.path.join(target_folder, basename)
+        target = dst or os.path.join(target_folder, basename)
         storage.save(target)
         if folder:
             return posixpath.join(folder, basename)
             content_type=content_type, content_length=content_length,
             headers=None)
         self.saved = None
+        self.saved_stream = False
     
     def save(self, dst, buffer_size=16384):
         """
             self.saved = dst
         else:
             self.saved = dst.name
+            self.saved_stream = True