Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Refactored upload to allow for both base64 image uploads and directory #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions imgurpython/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,22 +579,25 @@ def get_image(self, image_id):
image = self.make_request('GET', 'image/%s' % image_id)
return Image(image)

def upload_from_base64(self, encoded_string, config=None, anon=True):
return self.upload(encoded_string, config, anon)

def upload_from_path(self, path, config=None, anon=True):
with open(path, 'rb') as fd:
self.upload(fd, config, anon)
contents = fd.read()
b64 = base64.b64encode(contents)
return self.upload(b64, config, anon)

def upload(self, fd, config=None, anon=True):
if not config:
config = dict()

contents = fd.read()
b64 = base64.b64encode(contents)
data = {
'image': b64,
'image': fd,
'type': 'base64',
}
data.update({meta: config[meta] for meta in set(self.allowed_image_fields).intersection(config.keys())})

return self.make_request('POST', 'upload', data, anon)

def upload_from_url(self, url, config=None, anon=True):
Expand Down