diff --git a/Testy/19 Test download at once.py b/Testy/19 Test download at once.py new file mode 100644 index 0000000..fa93f9c --- /dev/null +++ b/Testy/19 Test download at once.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import pymysql +from pathlib import Path +import os +import re + +# ============================== +# ⚙️ CONFIGURATION +# ============================== +DB_CONFIG = { + "host": "192.168.1.76", + "port": 3307, + "user": "root", + "password": "Vlado9674+", + "database": "medevio", + "charset": "utf8mb4", +} + +OUTPUT = Path(r"u:\Dropbox\Ordinace\Dokumentace_ke_zpracování\ALL_BLOBS") +OUTPUT.mkdir(exist_ok=True) + + +def sanitize(name: str) -> str: + return re.sub(r'[<>:"/\\|?*\x00-\x1F]', "_", name).strip() + + +# ============================== +# 📥 LOAD EVERYTHING IN ONE QUERY +# ============================== +def load_all_files(): + conn = pymysql.connect(**DB_CONFIG) + cur = conn.cursor(pymysql.cursors.DictCursor) + + print("📥 Loading ALL medevio_downloads including BLOBs… (can take a few seconds)") + + cur.execute(""" + SELECT + d.id AS download_id, + d.request_id, + d.attachment_id, + d.filename, + d.content_type, + d.file_size, + d.created_at, + p.pacient_jmeno, + p.pacient_prijmeni, + d.file_content + FROM medevio_downloads d + JOIN pozadavky p ON d.request_id = p.id + ORDER BY d.created_at + """) + + rows = cur.fetchall() + conn.close() + + print(f"📦 Loaded {len(rows)} BLOB records.") + return rows + + +# ============================== +# 💾 SAVE ALL TO FILESYSTEM +# ============================== +def save_all(rows): + saved = 0 + + for r in rows: + req_id = r["request_id"] + jmeno = sanitize(r["pacient_jmeno"] or "") + prijmeni = sanitize(r["pacient_prijmeni"] or "") + filename = sanitize(r["filename"] or f"{r['download_id']}.bin") + + # Folder for each request + folder = OUTPUT / f"{prijmeni}, {jmeno} {req_id}" + folder.mkdir(exist_ok=True) + + dest = folder / filename + + # Skip existing + if dest.exists(): + continue + + data = r["file_content"] + if not data: + continue + + with open(dest, "wb") as f: + f.write(data) + + print(f"💾 Saved {dest}") + saved += 1 + + print(f"\n🎯 Done — {saved} files saved.") + + +# ============================== +# MAIN +# ============================== +if __name__ == "__main__": + rows = load_all_files() # ONE query + save_all(rows) # then write to disk diff --git a/Testy/19 Test.py b/Testy/19 Test.py index 8eb9f6a..0464f41 100644 --- a/Testy/19 Test.py +++ b/Testy/19 Test.py @@ -7,6 +7,7 @@ import pymysql import re from pathlib import Path from datetime import datetime +import time # ============================== # ⚙️ CONFIGURATION @@ -144,6 +145,7 @@ for r in rows: continue # fetch blob only now + start = time.perf_counter() cur_blob.execute( "SELECT file_content FROM medevio_downloads " "WHERE request_id=%s AND filename=%s", @@ -152,6 +154,8 @@ for r in rows: row = cur_blob.fetchone() if not row: continue + end = time.perf_counter() + print(f"⏱ Took {end - start:.4f} seconds") content = row[0] if not content: