check upload doesn't exceed ankiweb limits

better to abort prior to upload instead of giving a misleading error
This commit is contained in:
Damien Elmes 2018-04-30 16:58:00 +10:00
parent fc23241f0a
commit 693bb955ad

View File

@ -541,6 +541,7 @@ class HttpSyncer:
('Content-Disposition: form-data; name="%s"\r\n\r\n%s\r\n' % ('Content-Disposition: form-data; name="%s"\r\n\r\n%s\r\n' %
(key, value)).encode("utf8")) (key, value)).encode("utf8"))
# payload as raw data or json # payload as raw data or json
rawSize = 0
if fobj: if fobj:
# header # header
buf.write(bdry + b"\r\n") buf.write(bdry + b"\r\n")
@ -558,6 +559,7 @@ Content-Type: application/octet-stream\r\n\r\n""")
if comp: if comp:
tgt.close() tgt.close()
break break
rawSize += len(data)
tgt.write(data) tgt.write(data)
buf.write(b"\r\n") buf.write(b"\r\n")
buf.write(bdry + b'--\r\n') buf.write(bdry + b'--\r\n')
@ -568,6 +570,10 @@ Content-Type: application/octet-stream\r\n\r\n""")
'Content-Length': str(size), 'Content-Length': str(size),
} }
buf.seek(0) buf.seek(0)
if size >= 100*1024*1024 or rawSize >= 250*1024*1024:
raise Exception("Collection too large to upload to AnkiWeb.")
return headers, buf return headers, buf
def req(self, method, fobj=None, comp=6, badAuthRaises=True): def req(self, method, fobj=None, comp=6, badAuthRaises=True):