Return the size (bytes) of an uploaded FileStorage if possible. We prefer stream seeking because Content-Length is not always available for multipart parts on all clients.
(file_storage)
| 462 | if buffered: |
| 463 | print(f"STARTUP ERRORS FOUND: {len(buffered)}") |
| 464 | for item in buffered: |
| 465 | print("\n[ButSystem ERROR] " + item) |
| 466 | else: |
| 467 | print("ERROR MONITOR: No errors detected during startup.") |
| 468 | sys.stdout.flush() |
| 469 | |
| 470 | # --------------------------- |
| 471 | # Crypto helpers (TEXT encryption only; binary files stored plaintext) |
| 472 | # --------------------------- |
| 473 | |
| 474 | MAGIC = b"BUTSYS1" |
| 475 | NONCE_LEN = 12 |
| 476 | TAG_LEN = 16 |
| 477 | HDR_LEN = len(MAGIC) + NONCE_LEN + TAG_LEN |
| 478 | CHUNK = 4 * 1024 * 1024 # 4 MiB chunks for fast encrypted streaming (low RAM + good throughput) |
| 479 | |
| 480 | def load_or_create_master_key() -> bytes: |
| 481 | """load_or_create_master_key. |
| 482 | |
| 483 | Internal helper function. |
| 484 | |
| 485 | This docstring was added automatically to improve maintainability. |
| 486 | |
| 487 | Returns: |
| 488 | Varies. |
| 489 | """ |
| 490 | if os.path.exists(MASTER_KEY_PATH): |
| 491 | raw = open(MASTER_KEY_PATH, "rb").read().strip() |
| 492 | key = base64.urlsafe_b64decode(raw) |
| 493 | if len(key) != 32: |
| 494 | raise RuntimeError("Invalid master key length.") |
| 495 | return key |
| 496 | key = os.urandom(32) |
| 497 | with open(MASTER_KEY_PATH, "wb") as f: |
| 498 | f.write(base64.urlsafe_b64encode(key)) |
| 499 | try: |
| 500 | os.chmod(MASTER_KEY_PATH, 0o600) |
| 501 | except Exception: |
| 502 | pass |
| 503 | return key |
| 504 | |
| 505 | MASTER_KEY = load_or_create_master_key() |
no outgoing calls
no test coverage detected