media providers: Allow "content" to be an open file-like object.

This commit is contained in:
Neil Williams
2014-02-17 22:56:41 -08:00
parent 26d42cc44a
commit 9305d2fc68
3 changed files with 14 additions and 3 deletions

View File

@@ -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`.

View File

@@ -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):

View File

@@ -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,