From 9305d2fc68f8499ee911ec20788220a8b7de14be Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Mon, 17 Feb 2014 22:56:41 -0800 Subject: [PATCH] media providers: Allow "content" to be an open file-like object. --- r2/r2/lib/providers/media/__init__.py | 3 ++- r2/r2/lib/providers/media/filesystem.py | 6 +++++- r2/r2/lib/providers/media/s3.py | 8 +++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/r2/r2/lib/providers/media/__init__.py b/r2/r2/lib/providers/media/__init__.py index 545c90601..eeb6a40f7 100644 --- a/r2/r2/lib/providers/media/__init__.py +++ b/r2/r2/lib/providers/media/__init__.py @@ -43,7 +43,8 @@ class MediaProvider(object): `name` must be a local filename including an extension. - `contents` is a byte string of the contents of the file. + `contents` is a byte string of the contents of the file or a file-like + object the contents of which will be read. The return value should be an absolute URL with the `http` scheme but should also work if accessed with `https`. diff --git a/r2/r2/lib/providers/media/filesystem.py b/r2/r2/lib/providers/media/filesystem.py index 5bd5339bb..b1853ed42 100644 --- a/r2/r2/lib/providers/media/filesystem.py +++ b/r2/r2/lib/providers/media/filesystem.py @@ -21,6 +21,7 @@ ############################################################################### import os +import shutil import urlparse from pylons import app_globals as g @@ -65,7 +66,10 @@ class FileSystemMediaProvider(MediaProvider): assert os.path.dirname(name) == "" path = os.path.join(g.media_fs_root, name) with open(path, "w") as f: - f.write(contents) + if isinstance(contents, basestring): + f.write(contents) + else: + shutil.copyfileobj(contents, f) return urlparse.urljoin(g.media_fs_base_url_http, name) def purge(self, url): diff --git a/r2/r2/lib/providers/media/s3.py b/r2/r2/lib/providers/media/s3.py index 4399db27f..b0e777199 100644 --- a/r2/r2/lib/providers/media/s3.py +++ b/r2/r2/lib/providers/media/s3.py @@ -124,7 +124,13 @@ class S3MediaProvider(MediaProvider): # send the key bucket = self._get_bucket(bucket_name, validate=False) key = bucket.new_key(name) - key.set_contents_from_string( + + if isinstance(contents, basestring): + set_fn = key.set_contents_from_string + else: + set_fn = key.set_contents_from_file + + set_fn( contents, headers={ "Content-Type": mime_type,