Tidy endpoint for receiving images

main
Bob Mottram 2020-08-30 20:37:44 +01:00
parent 5a44e82106
commit 1517456f32
1 changed files with 75 additions and 55 deletions

130
daemon.py
View File

@ -2139,6 +2139,72 @@ class PubServer(BaseHTTPRequestHandler):
cookie, callingDomain) cookie, callingDomain)
self.server.POSTbusy = False self.server.POSTbusy = False
def _receiveImage(self, length: int,
callingDomain: str, cookie: str,
authorized: bool, path: str,
baseDir: str, httpPrefix: str,
domain: str, domainFull: str,
onionDomain: str, i2pDomain: str, debug: bool):
"""Receives an image via POST
"""
if not self.outboxAuthenticated:
if debug:
print('DEBUG: unauthenticated attempt to ' +
'post image to outbox')
self.send_response(403)
self.end_headers()
self.server.POSTbusy = False
return
pathUsersSection = path.split('/users/')[1]
if '/' not in pathUsersSection:
self._404()
self.server.POSTbusy = False
return
self.postFromNickname = pathUsersSection.split('/')[0]
accountsDir = \
baseDir + '/accounts/' + \
self.postFromNickname + '@' + domain
if not os.path.isdir(accountsDir):
self._404()
self.server.POSTbusy = False
return
try:
mediaBytes = self.rfile.read(length)
except SocketError as e:
if e.errno == errno.ECONNRESET:
print('WARN: POST mediaBytes ' +
'connection reset by peer')
else:
print('WARN: POST mediaBytes socket error')
self.send_response(400)
self.end_headers()
self.server.POSTbusy = False
return
except ValueError as e:
print('ERROR: POST mediaBytes rfile.read failed')
print(e)
self.send_response(400)
self.end_headers()
self.server.POSTbusy = False
return
mediaFilenameBase = accountsDir + '/upload'
mediaFilename = mediaFilenameBase + '.png'
if self.headers['Content-type'].endswith('jpeg'):
mediaFilename = mediaFilenameBase + '.jpg'
if self.headers['Content-type'].endswith('gif'):
mediaFilename = mediaFilenameBase + '.gif'
if self.headers['Content-type'].endswith('webp'):
mediaFilename = mediaFilenameBase + '.webp'
with open(mediaFilename, 'wb') as avFile:
avFile.write(mediaBytes)
if debug:
print('DEBUG: image saved to ' + mediaFilename)
self.send_response(201)
self.end_headers()
self.server.POSTbusy = False
def _removeShare(self, callingDomain: str, cookie: str, def _removeShare(self, callingDomain: str, cookie: str,
authorized: bool, path: str, authorized: bool, path: str,
baseDir: str, httpPrefix: str, baseDir: str, httpPrefix: str,
@ -9271,61 +9337,15 @@ class PubServer(BaseHTTPRequestHandler):
# receive images to the outbox # receive images to the outbox
if self.headers['Content-type'].startswith('image/') and \ if self.headers['Content-type'].startswith('image/') and \
'/users/' in self.path: '/users/' in self.path:
if not self.outboxAuthenticated: self._receiveImage(length, callingDomain, cookie,
if self.server.debug: authorized, self.path,
print('DEBUG: unauthenticated attempt to ' + self.server.baseDir,
'post image to outbox') self.server.httpPrefix,
self.send_response(403) self.server.domain,
self.end_headers() self.server.domainFull,
self.server.POSTbusy = False self.server.onionDomain,
return self.server.i2pDomain,
pathUsersSection = self.path.split('/users/')[1] self.server.debug)
if '/' not in pathUsersSection:
self._404()
self.server.POSTbusy = False
return
self.postFromNickname = pathUsersSection.split('/')[0]
accountsDir = \
self.server.baseDir + '/accounts/' + \
self.postFromNickname + '@' + self.server.domain
if not os.path.isdir(accountsDir):
self._404()
self.server.POSTbusy = False
return
try:
mediaBytes = self.rfile.read(length)
except SocketError as e:
if e.errno == errno.ECONNRESET:
print('WARN: POST mediaBytes ' +
'connection reset by peer')
else:
print('WARN: POST mediaBytes socket error')
self.send_response(400)
self.end_headers()
self.server.POSTbusy = False
return
except ValueError as e:
print('ERROR: POST mediaBytes rfile.read failed')
print(e)
self.send_response(400)
self.end_headers()
self.server.POSTbusy = False
return
mediaFilenameBase = accountsDir + '/upload'
mediaFilename = mediaFilenameBase + '.png'
if self.headers['Content-type'].endswith('jpeg'):
mediaFilename = mediaFilenameBase + '.jpg'
if self.headers['Content-type'].endswith('gif'):
mediaFilename = mediaFilenameBase + '.gif'
if self.headers['Content-type'].endswith('webp'):
mediaFilename = mediaFilenameBase + '.webp'
with open(mediaFilename, 'wb') as avFile:
avFile.write(mediaBytes)
if self.server.debug:
print('DEBUG: image saved to ' + mediaFilename)
self.send_response(201)
self.end_headers()
self.server.POSTbusy = False
return return
# refuse to receive non-json content # refuse to receive non-json content