Skip to content

Commit

Permalink
Fix base64 encoding
Browse files Browse the repository at this point in the history
b64encode takes and returns only bytes, so the credentials need to be byte-encoded first and the returned bytes object decoded afterwards (ASCII works for base64 strings).

Additionally some %-formattings have been replaced with f-strings, fix some syntax errors and satisfy pre-commit black checks.
  • Loading branch information
MichaIng committed Apr 12, 2022
1 parent e098224 commit 8810709
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions motioneye/uploadservices.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,16 +895,18 @@ def __init__(self, camera_id):
UploadService.__init__(self, camera_id)

def _request(self, url, method, body=None):
base64string = b64encode(f'{self._username}:{self._password}')
headers = {'Authorization': 'Basic %s' % base64string}
base64string = b64encode(f'{self._username}:{self._password}'.encode()).decode(
'ascii'
)
headers = {'Authorization': f'Basic {base64string}'}
if body is not None:
headers.update('Content-Length', '%d' % len(body))
self.debug('request: ' + method + ' ' + url)
headers.update({'Content-Length': len(body)})
self.debug(f'request: {method} {url}')
request = urllib.request.Request(url, data=body, headers=headers)
request.get_method = lambda: method
try:
utils.urlopen(request)
except urllib.HTTPError as e:
except urllib.error.HTTPError as e:
if method == 'MKCOL' and e.code == 405:
self.debug(
'MKCOL failed with code 405, this is normal if the folder exists'
Expand Down Expand Up @@ -999,7 +1001,7 @@ def upload_data(self, filename, mime_type, data, ctime, camera_name):
conn.cwd(path)

self.debug(f'uploading {filename} of {len(data)} bytes')
conn.storbinary('STOR %s' % filename, io.StringIO(data))
conn.storbinary(f'STOR {filename}', io.StringIO(data))

self.debug('upload done')

Expand Down

0 comments on commit 8810709

Please sign in to comment.