Factorize response, compute content length
This commit is contained in:
parent
74ec28e2be
commit
45401e3255
72
archive.py
72
archive.py
|
|
@ -4,6 +4,8 @@ from flask import Response
|
||||||
import functools
|
import functools
|
||||||
import os
|
import os
|
||||||
import zlib
|
import zlib
|
||||||
|
from typing import Optional
|
||||||
|
import time
|
||||||
|
|
||||||
# Chunks for crc 32 computation
|
# Chunks for crc 32 computation
|
||||||
CRC32_CHUNK_SIZE = 65_536
|
CRC32_CHUNK_SIZE = 65_536
|
||||||
|
|
@ -71,9 +73,31 @@ class ArchiveSender:
|
||||||
def add_file(self, filename: str, filepath: str):
|
def add_file(self, filename: str, filepath: str):
|
||||||
self.files[filename] = filepath
|
self.files[filename] = filepath
|
||||||
|
|
||||||
def response(self):
|
def content_length(self) -> Optional[int]:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def generator(self):
|
||||||
raise NotImplementedError("Abstract method")
|
raise NotImplementedError("Abstract method")
|
||||||
|
|
||||||
|
def mime_type(self) -> str:
|
||||||
|
raise NotImplementedError("Abstract method")
|
||||||
|
|
||||||
|
def archive_name(self) -> str:
|
||||||
|
raise NotImplementedError("Abstract method")
|
||||||
|
|
||||||
|
def response(self):
|
||||||
|
headers = {'Content-Disposition': f'attachment; filename="{self.archive_name()}"'}
|
||||||
|
|
||||||
|
length = self.content_length()
|
||||||
|
if length is not None:
|
||||||
|
headers['Content-Length'] = str(length)
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
self.generator(),
|
||||||
|
mimetype=self.mime_type(),
|
||||||
|
headers=headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TarSender(ArchiveSender):
|
class TarSender(ArchiveSender):
|
||||||
def generator(self):
|
def generator(self):
|
||||||
|
|
@ -98,12 +122,22 @@ class TarSender(ArchiveSender):
|
||||||
yield b'\x00' * (512 - bytes_sent % 512)
|
yield b'\x00' * (512 - bytes_sent % 512)
|
||||||
return generate()
|
return generate()
|
||||||
|
|
||||||
def response(self):
|
def mime_type(self) -> str:
|
||||||
return Response(
|
return 'application/x-tar'
|
||||||
self.generator(),
|
|
||||||
mimetype='application/x-tar',
|
def archive_name(self) -> str:
|
||||||
headers={'Content-Disposition': 'attachment; filename="archive.tar"'}
|
return 'archive.tar'
|
||||||
)
|
|
||||||
|
def content_length(self) -> int:
|
||||||
|
length = 0
|
||||||
|
|
||||||
|
for file in self.files.values():
|
||||||
|
stat = os.stat(file)
|
||||||
|
|
||||||
|
# Add size of header, and size of content ceiled to 512 bytes
|
||||||
|
length += 512 + stat.st_size + (512 - stat.st_size % 512)
|
||||||
|
|
||||||
|
return length
|
||||||
|
|
||||||
|
|
||||||
def crc32(filename) -> int:
|
def crc32(filename) -> int:
|
||||||
|
|
@ -263,6 +297,7 @@ class ZipSender(ArchiveSender):
|
||||||
|
|
||||||
current_byte += len(bytes)
|
current_byte += len(bytes)
|
||||||
yield bytes
|
yield bytes
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
central_directory_size = 0
|
central_directory_size = 0
|
||||||
centra_directory_offset = current_byte
|
centra_directory_offset = current_byte
|
||||||
|
|
@ -277,9 +312,20 @@ class ZipSender(ArchiveSender):
|
||||||
|
|
||||||
return generate()
|
return generate()
|
||||||
|
|
||||||
def response(self):
|
def content_length(self) -> int:
|
||||||
return Response(
|
length = 0
|
||||||
self.generator(),
|
|
||||||
mimetype='application/zip',
|
for name, file in self.files.items():
|
||||||
headers={'Content-Disposition': 'attachment; filename="archive.zip"'}
|
stat = os.stat(file)
|
||||||
)
|
|
||||||
|
# Add size of local file header, central directory file header and file size
|
||||||
|
length += 76 + 2 * len(name) + stat.st_size
|
||||||
|
|
||||||
|
# Add size of end of central directory
|
||||||
|
return length + 22
|
||||||
|
|
||||||
|
def mime_type(self) -> str:
|
||||||
|
return 'application/zip'
|
||||||
|
|
||||||
|
def archive_name(self) -> str:
|
||||||
|
return 'archive.zip'
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue