mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-04-27 03:00:12 -04:00
media providers: Allow "content" to be an open file-like object.
This commit is contained in:
@@ -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`.
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user