copy_local_to_storage, both general and specialized-for-BasicFileStorage versions
authorChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 21 Nov 2011 04:01:43 +0000 (22:01 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Mon, 21 Nov 2011 04:01:43 +0000 (22:01 -0600)
This utility should allow for easy copying from a local filesystem to
the storage instance.

mediagoblin/storage/__init__.py
mediagoblin/storage/filestorage.py

index 9e592b9ef25855953641e800b6b03b95e8173c98..b76e18af9732defda8a2daf4ac7dbe7e10f6e4f4 100644 (file)
@@ -169,6 +169,14 @@ class StorageInterface(object):
                 with file(dest_path, 'wb') as dest_file:
                     dest_file.write(source_file.read())
 
+    def copy_local_to_storage(self, filename, filepath):
+        """
+        Copy this file from locally to the storage system.
+        """
+        with self.get_file(filepath, 'wb') as dest_file:
+            with file(filename, 'rb') as source_file:
+                dest_file.write(source_file.read())
+
 
 ###########
 # Utilities
index 22d6eb5a60ee9d27f05e40688778f891451101cd..a904865f7979b0a1a828083da76374571d7ef062 100644 (file)
@@ -20,6 +20,7 @@ from mediagoblin.storage import (
     NoWebServing)
 
 import os
+import shutil
 import urlparse
 
 
@@ -76,3 +77,16 @@ class BasicFileStorage(StorageInterface):
 
     def get_local_path(self, filepath):
         return self._resolve_filepath(filepath)
+
+    def copy_local_to_storage(self, filename, filepath):
+        """
+        Copy this file from locally to the storage system.
+        """
+        # Make directories if necessary
+        if len(filepath) > 1:
+            directory = self._resolve_filepath(filepath[:-1])
+            if not os.path.exists(directory):
+                os.makedirs(directory)
+
+        shutil.copy(
+            filename, self.get_local_path(filepath))