Standardize header naming

This commit is contained in:
amcmanu3 2024-06-02 13:02:40 -04:00
parent 10091a38ee
commit 682999f7f6
2 changed files with 16 additions and 18 deletions

View File

@ -107,11 +107,11 @@ class ApiFilesUploadHandler(BaseApiHandler):
self.file_hash = self.request.headers.get("fileHash", 0) self.file_hash = self.request.headers.get("fileHash", 0)
self.chunk_hash = self.request.headers.get("chunkHash", 0) self.chunk_hash = self.request.headers.get("chunkHash", 0)
self.file_id = self.request.headers.get("fileId") self.file_id = self.request.headers.get("fileId")
self.chunked = self.request.headers.get("chunked", True) self.chunked = self.request.headers.get("chunked", False)
self.filename = self.request.headers.get("filename", None) self.filename = self.request.headers.get("fileName", None)
try: try:
file_size = int(self.request.headers.get("fileSize", None)) file_size = int(self.request.headers.get("fileSize", None))
total_chunks = int(self.request.headers.get("total_chunks", None)) total_chunks = int(self.request.headers.get("totalChunks", 0))
except TypeError: except TypeError:
return self.finish_json( return self.finish_json(
400, {"status": "error", "error": "TYPE ERROR", "data": {}} 400, {"status": "error", "error": "TYPE ERROR", "data": {}}
@ -182,19 +182,15 @@ class ApiFilesUploadHandler(BaseApiHandler):
if not self.chunked: if not self.chunked:
# Write the file directly to the upload dir # Write the file directly to the upload dir
with open(os.path.join(self.upload_dir, self.filename), "wb") as file: with open(os.path.join(self.upload_dir, self.filename), "wb") as file:
while True:
chunk = self.request.body chunk = self.request.body
if not chunk: if chunk:
break
file.write(chunk) file.write(chunk)
# We'll check the file hash against the sent hash once the file is # We'll check the file hash against the sent hash once the file is
# written. We cannot check this buffer. # written. We cannot check this buffer.
if ( calculated_hash = self.file_helper.calculate_file_hash(
self.file_helper.calculate_file_hash(
os.path.join(self.upload_dir, self.filename) os.path.join(self.upload_dir, self.filename)
) )
!= self.file_hash if calculated_hash != self.file_hash:
):
# If the hash is bad we'll delete the malformed file and send # If the hash is bad we'll delete the malformed file and send
# a warning # a warning
os.remove(os.path.join(self.upload_dir, self.filename)) os.remove(os.path.join(self.upload_dir, self.filename))
@ -209,7 +205,9 @@ class ApiFilesUploadHandler(BaseApiHandler):
"error": "INVALID HASH", "error": "INVALID HASH",
"data": { "data": {
"message": "Hash recieved does not" "message": "Hash recieved does not"
" match reported sent hash.", " match reported sent hash."
f"Recieved: {calculated_hash} "
f"Expected: {self.file_hash}",
}, },
}, },
) )

View File

@ -38,8 +38,8 @@ async function uploadFile(type, file = null, path = null, file_num = 0, _onProgr
'fileHash': file_hash, 'fileHash': file_hash,
'fileSize': file.size, 'fileSize': file.size,
'type': type, 'type': type,
'total_chunks': totalChunks, 'totalChunks': totalChunks,
'filename': file.name, 'fileName': file.name,
'location': path, 'location': path,
'fileId': fileId, 'fileId': fileId,
}, },
@ -74,8 +74,8 @@ async function uploadFile(type, file = null, path = null, file_num = 0, _onProgr
'chunkHash': chunk_hash, 'chunkHash': chunk_hash,
'chunked': true, 'chunked': true,
'type': type, 'type': type,
'total_chunks': totalChunks, 'totalChunks': totalChunks,
'filename': file.name, 'fileName': file.name,
'location': path, 'location': path,
'fileId': fileId, 'fileId': fileId,
'chunkId': i, 'chunkId': i,