Skip to content

Commit f6c6e8f

Browse files
Jonathon GrafJonathon Graf
authored andcommitted
Refactored upload call to allow for both base64 image uploads as well as from file directory
1 parent 3a285f7 commit f6c6e8f

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

imgurpython/client.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,22 +579,25 @@ def get_image(self, image_id):
579579
image = self.make_request('GET', 'image/%s' % image_id)
580580
return Image(image)
581581

582+
def upload_from_base64(self, encoded_string, config=None, anon=True):
583+
return self.upload(encoded_string, config, anon)
584+
582585
def upload_from_path(self, path, config=None, anon=True):
583586
with open(path, 'rb') as fd:
584-
self.upload(fd, config, anon)
587+
contents = fd.read()
588+
b64 = base64.b64encode(contents)
589+
return self.upload(b64, config, anon)
585590

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

590-
contents = fd.read()
591-
b64 = base64.b64encode(contents)
592595
data = {
593-
'image': b64,
596+
'image': fd,
594597
'type': 'base64',
595598
}
596599
data.update({meta: config[meta] for meta in set(self.allowed_image_fields).intersection(config.keys())})
597-
600+
598601
return self.make_request('POST', 'upload', data, anon)
599602

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

0 commit comments

Comments
 (0)