|
7 | 7 | from urllib.parse import urlparse, parse_qs |
8 | 8 | from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler |
9 | 9 |
|
| 10 | +# SECRET is set dynamically at runtime with -s |
10 | 11 | SECRET = None |
11 | 12 |
|
| 13 | +# PART_SIZE is hardcoded; writes out larger files in chunks of this size to limit memory usage |
| 14 | +# This also significantly speeds up large transfers, but idk why |
| 15 | +PART_SIZE = 256 * 1024 * 1024 #256MB |
| 16 | + |
12 | 17 | class CustomHTTPRequestHandler(SimpleHTTPRequestHandler): |
13 | 18 |
|
14 | 19 | # Ignore all GETs |
@@ -54,13 +59,24 @@ def do_POST(self): |
54 | 59 | else: |
55 | 60 | filePath = f"{filePath}_{i}" |
56 | 61 |
|
57 | | - # Write the request body |
| 62 | + # Write the request body to a file |
58 | 63 | with open(filePath, 'wb') as output_file: |
59 | | - output_file.write(self.rfile.read(file_length)) |
| 64 | + print(f"Recieving file '{filename}' with size {file_length} from {self.client_address[0]}") |
| 65 | + |
| 66 | + numParts = file_length // PART_SIZE |
| 67 | + if file_length % PART_SIZE != 0: |
| 68 | + numParts += 1 |
| 69 | + |
| 70 | + for i in range(numParts): |
| 71 | + print(f"Writing part {i+1} of {numParts}") |
| 72 | + remainingSize = file_length - (i * PART_SIZE) |
| 73 | + output_file.write(self.rfile.read(min(PART_SIZE, remainingSize))) |
| 74 | + |
60 | 75 | self.send_response(201, 'Created') |
61 | 76 | self.end_headers() |
62 | 77 |
|
63 | 78 | print(f"File of size {file_length} bytes saved to {filePath}") |
| 79 | + print() |
64 | 80 |
|
65 | 81 | # Treat PUT just like POST |
66 | 82 | def do_PUT(self): |
|
0 commit comments