-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1326 lines (1140 loc) · 51.2 KB
/
Copy pathserver.py
File metadata and controls
1326 lines (1140 loc) · 51.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
ScanStation Local API Server
====================================================================
Lightweight local server that lets the browser UI trigger actions
like exporting documents without needing to open CMD.
RUNS ON: http://127.0.0.1:8765 (localhost only — not exposed to network)
ENDPOINTS:
GET /health → Check if server is running
GET /clients → List all client folders
POST /export → Export verified docs for a client
Body: { "client": "TestClient" }
GET /stats/<client> → Get document counts for a client
GET /docs/<client> → List all documents for a client (review.json data + counts)
POST /review/<client>/<doc_id> → Save review data to review.json
GET /pdf/<client>/<doc_id> → Serve PDF file for preview
HOW TO USE:
This runs automatically via Start_System_v2.bat
Or manually: python server.py
DEPENDENCIES:
pip install flask flask-cors
"""
import json
import os
import sqlite3
import threading
import shutil
import subprocess
import sys
from pathlib import Path
from datetime import datetime, UTC
from urllib.parse import quote
from flask import Flask, request, jsonify, send_file, abort
from flask_cors import CORS
from pdfminer.high_level import extract_text
from pypdf import PdfReader, PdfWriter
from export_client import run_export
from sync_to_portal import sync_portal_for_clients, sync_single_doc
from portal_new import document_config
# ──────────────────────────────────────────────
# CONFIGURATION
# ──────────────────────────────────────────────
# Filesystem root: same folder as this script (contains Clients/, Templates/).
# Override with MORPHIQ_BASE or SCANSTATION_BASE env vars.
_base_env = (os.environ.get("MORPHIQ_BASE") or os.environ.get("SCANSTATION_BASE") or "").strip()
BASE = Path(_base_env).resolve() if _base_env else Path(__file__).resolve().parent
HOST = "127.0.0.1" # Localhost only — never exposed to network
PORT = 8765
app = Flask(__name__)
# Allow requests from file:// origins (browsers send Origin: null for local HTML files)
CORS(app, origins=["null"], supports_credentials=False)
# Track if an export is currently running (prevent double-clicks)
export_lock = threading.Lock()
# Map doc_type display name to template file stem for rescan empty fields
DOC_TYPE_TO_TEMPLATE = {
"tenancy agreement": "tenancy_agreement",
"gas safety certificate": "gas_safety_certificate",
"gas safety (cp12)": "gas_safety_certificate",
"eicr": "eicr",
"epc": "epc",
"general document": "general_document",
}
ALLOWED_RAW_EXTENSIONS = {".jpg", ".jpeg", ".png", ".tiff", ".tif", ".bmp", ".pdf"}
# ──────────────────────────────────────────────
# SECURITY HARDENING (pre-launch)
# ──────────────────────────────────────────────
# This API is a LOCAL desktop companion bound to 127.0.0.1. Two server-side
# controls neutralise the high-risk findings without any UI change:
# 1. Loopback-only Host guard -> defeats DNS-rebinding / off-host access.
# 2. Path-segment sanitisation -> defeats path traversal (arbitrary file
# read/write) on every route that builds a filesystem path from the URL.
_LOOPBACK_HOSTS = {"127.0.0.1", "localhost", "::1", "[::1]"}
_PATH_SEGMENT_ARGS = {"client_name", "doc_id", "filename", "raw_name", "group_id", "export_folder"}
_SEGMENT_BLOCKLIST = set('/\\\x00:*?"<>|')
def _safe_segment(value) -> bool:
"""True only if value is a single, traversal-free path segment."""
if not isinstance(value, str) or not value.strip():
return False
if ".." in value:
return False
return not any(ch in _SEGMENT_BLOCKLIST for ch in value)
@app.before_request
def _enforce_local_and_safe_paths():
# 1) Loopback-only. The Host header must address the local machine; this
# blocks DNS-rebinding (which presents an attacker hostname) and any
# accidental network exposure.
host = (request.host or "").rsplit(":", 1)[0].strip().lower()
if host not in _LOOPBACK_HOSTS:
abort(403)
# 2) Reject cross-site state-changing requests (CSRF / rebind hardening).
if request.method not in ("GET", "HEAD", "OPTIONS"):
origin = (request.headers.get("Origin") or "").strip().lower()
if origin and origin != "null":
from urllib.parse import urlsplit
if (urlsplit(origin).hostname or "").lower() not in _LOOPBACK_HOSTS:
abort(403)
# 3) Every URL path component used to build a filesystem path must be a
# safe single segment (no "/", "\\", "..", drive letters, etc).
for name, value in (request.view_args or {}).items():
if name in _PATH_SEGMENT_ARGS and not _safe_segment(value):
abort(400)
# ──────────────────────────────────────────────
# HELPERS — merge / split
# ──────────────────────────────────────────────
def _get_next_doc_id(batch_folder: Path) -> str:
"""Return the next sequential DOC-XXXXX identifier inside a batch folder."""
existing = []
if batch_folder.exists():
for item in batch_folder.iterdir():
if item.is_dir() and item.name.startswith("DOC-"):
try:
num = int(item.name.split("-")[1])
existing.append(num)
except (IndexError, ValueError):
pass
return f"DOC-{max(existing, default=0) + 1:05d}"
def _run_ai_prefill(doc_folder: Path) -> None:
"""Best-effort subprocess call to ai_prefill.py for a DOC folder."""
script = BASE / "ai_prefill.py"
if not script.is_file():
return
try:
subprocess.run(
[sys.executable, str(script), str(doc_folder)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
timeout=180,
)
except Exception as e:
app.logger.warning(f"AI prefill error for {doc_folder.name}: {e}")
def _clients_dir() -> Path:
return BASE / "Clients"
def _client_dir(client_name: str) -> Path:
return _clients_dir() / client_name
def _raw_dir(client_name: str) -> Path:
raw_dir = _client_dir(client_name) / "raw"
raw_dir.mkdir(parents=True, exist_ok=True)
return raw_dir
def _sanitize_raw_filename(filename: str) -> str:
ext = (Path(filename or "").suffix or "").lower()
if ext not in ALLOWED_RAW_EXTENSIONS:
ext = ".jpg"
ts = datetime.now(UTC).strftime("%Y%m%d%H%M%S")
rand = os.urandom(2).hex()
return f"scan_{ts}_{rand}{ext}"
def _write_raw_meta(raw_dir: Path, raw_name: str, meta: dict) -> None:
meta_path = raw_dir / f"{raw_name}.meta.json"
with meta_path.open("w", encoding="utf-8") as handle:
json.dump(meta, handle, indent=2, ensure_ascii=False)
def _remove_doc_from_portal(client_name: str, doc_id: str) -> None:
"""Delete a document and its fields from portal.db by source_doc_id."""
db_path = str(BASE / "portal.db")
if not os.path.isfile(db_path):
return
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
try:
row = conn.execute(
"SELECT id FROM clients WHERE name = ?", (client_name,)
).fetchone()
if not row:
return
client_id = row["id"]
doc_row = conn.execute(
"SELECT id FROM documents WHERE source_doc_id = ? AND client_id = ?",
(doc_id, client_id),
).fetchone()
if not doc_row:
return
document_id = doc_row["id"]
conn.execute("DELETE FROM document_fields WHERE document_id = ?", (document_id,))
conn.execute("DELETE FROM documents WHERE id = ?", (document_id,))
conn.execute(
"""DELETE FROM properties WHERE id NOT IN (
SELECT DISTINCT property_id FROM documents WHERE property_id IS NOT NULL
)"""
)
conn.commit()
finally:
conn.close()
def _find_pdf(doc_folder: Path) -> Path | None:
"""Return the first PDF file inside a DOC folder, or None."""
for f in doc_folder.iterdir():
if f.is_file() and f.suffix.lower() == ".pdf":
return f
return None
# ──────────────────────────────────────────────
# ENDPOINTS
# ──────────────────────────────────────────────
@app.route("/health", methods=["GET"])
def health():
"""Check if the server is running."""
return jsonify({"status": "ok", "server": "ScanStation API", "port": PORT})
@app.route("/clients", methods=["GET"])
def list_clients():
"""List all client folders."""
clients_dir = _clients_dir()
if not clients_dir.exists():
return jsonify({"clients": []})
clients = [
d.name for d in sorted(clients_dir.iterdir())
if d.is_dir()
]
return jsonify({"clients": clients})
@app.route("/station-config", methods=["GET"])
def station_config():
"""Expose backend-driven station settings to keep UI and pipeline aligned."""
database_url = os.environ.get("DATABASE_URL", str(BASE / "portal.db"))
configs = document_config.get_document_configs(database_url)
return jsonify(
{
"base_path": str(BASE),
"clients_path": str(_clients_dir()),
"allowed_upload_extensions": sorted(ALLOWED_RAW_EXTENSIONS),
"document_types": [
{
"label": config["label"],
"document_key": config["document_key"],
"required_fields": config["required_fields"],
"field_definitions": config["extraction_fields"],
"show_in_upload": config["show_in_upload"],
"show_in_detection": config["show_in_detection"],
}
for config in configs
],
}
)
@app.route("/intake/<client_name>", methods=["POST"])
def intake_file(client_name: str):
"""
Accept a scanned/imported file and write it into the canonical raw folder that
the watcher and review pipeline already use.
"""
upload = request.files.get("file")
if upload is None or not upload.filename:
return jsonify({"success": False, "error": "No file provided"}), 400
ext = (Path(upload.filename).suffix or "").lower()
if ext not in ALLOWED_RAW_EXTENSIONS:
return jsonify({"success": False, "error": f"Unsupported file type: {ext or 'unknown'}"}), 400
raw_dir = _raw_dir(client_name)
raw_name = _sanitize_raw_filename(upload.filename)
raw_path = raw_dir / raw_name
try:
upload.save(str(raw_path))
except Exception as exc:
return jsonify({"success": False, "error": f"Failed to save upload: {exc}"}), 500
meta = {
"doc_name": (request.form.get("doc_name") or "").strip() or None,
"property_address": (request.form.get("property_address") or "").strip() or None,
}
group_id = (request.form.get("group_id") or "").strip()
if group_id:
meta["group_id"] = group_id
for key in ("page_number", "total_pages_so_far"):
raw_value = (request.form.get(key) or "").strip()
if raw_value:
try:
meta[key] = int(raw_value)
except ValueError:
meta[key] = raw_value
try:
_write_raw_meta(raw_dir, raw_name, meta)
except Exception as exc:
try:
if raw_path.exists():
raw_path.unlink()
except Exception:
pass
return jsonify({"success": False, "error": f"Failed to save metadata: {exc}"}), 500
return jsonify(
{
"success": True,
"client": client_name,
"raw_name": raw_name,
"raw_path": str(raw_path),
"kind": "pdf" if ext == ".pdf" else "image",
}
)
@app.route("/raw-meta/<client_name>/<raw_name>", methods=["POST"])
def update_raw_meta(client_name: str, raw_name: str):
"""Update the sidecar metadata for a raw file already saved in canonical intake."""
raw_dir = _raw_dir(client_name)
raw_path = raw_dir / raw_name
if not raw_path.exists():
return jsonify({"success": False, "error": "Raw file not found"}), 404
payload = request.get_json(silent=True) or {}
meta = {
"doc_name": (payload.get("doc_name") or "").strip() or None,
"property_address": (payload.get("property_address") or "").strip() or None,
}
if payload.get("group_id"):
meta["group_id"] = str(payload["group_id"]).strip()
for key in ("page_number", "total_pages_so_far"):
if payload.get(key) is not None:
meta[key] = payload[key]
try:
_write_raw_meta(raw_dir, raw_name, meta)
except Exception as exc:
return jsonify({"success": False, "error": f"Failed to update metadata: {exc}"}), 500
return jsonify({"success": True, "raw_name": raw_name})
@app.route("/raw-group-complete/<client_name>/<group_id>", methods=["POST"])
def mark_raw_group_complete(client_name: str, group_id: str):
raw_dir = _raw_dir(client_name)
marker_path = raw_dir / f"{group_id}.group_complete"
try:
marker_path.write_text("", encoding="utf-8")
except Exception as exc:
return jsonify({"success": False, "error": f"Failed to mark group complete: {exc}"}), 500
return jsonify({"success": True, "group_id": group_id})
@app.route("/export", methods=["POST"])
def export():
"""Export verified documents for a client."""
# Parse request
data = request.get_json(silent=True)
if not data or not data.get("client"):
return jsonify({"success": False, "error": "Missing 'client' in request body"}), 400
client_name = data["client"].strip()
if not _safe_segment(client_name):
return jsonify({"success": False, "error": "Invalid client name"}), 400
# Validate client exists
client_dir = BASE / "Clients" / client_name
if not client_dir.exists():
return jsonify({"success": False, "error": "Client not found"}), 404
# Prevent concurrent exports
if not export_lock.acquire(blocking=False):
return jsonify({"success": False, "error": "An export is already running. Please wait."}), 409
try:
result = run_export(client_name)
if result.get("success"):
# Keep the portal database in sync automatically for this client
try:
sync_summary = sync_portal_for_clients([client_name])
result["portal_sync"] = sync_summary
except Exception as sync_err:
# Don't fail the export if sync has an issue; just report it.
result["portal_sync_error"] = str(sync_err)
# After export, open the new MorphIQ portal (portal_new) instead of the legacy viewer.html
# The client name is passed as a query param for potential filtering, but the portal
# will still work even if it ignores this parameter.
result["viewer_url"] = f"http://127.0.0.1:5000/?client={quote(client_name)}"
return jsonify(result)
except Exception:
app.logger.exception("Export failed")
return jsonify({"success": False, "error": "Export failed"}), 500
finally:
export_lock.release()
@app.route("/open-folder", methods=["POST"])
def open_folder():
"""Open a folder in the system file manager (e.g. Explorer). Path must be under BASE."""
data = request.get_json(silent=True)
if not data or not data.get("path"):
return jsonify({"success": False, "error": "Missing 'path' in request body"}), 400
raw = data["path"].strip()
try:
folder = Path(raw).resolve()
base = BASE.resolve()
if not folder.is_dir():
return jsonify({"success": False, "error": "Path is not a folder"}), 400
try:
folder.relative_to(base)
except ValueError:
return jsonify({"success": False, "error": "Path must be inside the ScanSystem folder"}), 400
if sys.platform == "win32":
os.startfile(str(folder))
elif sys.platform == "darwin":
subprocess.run(["open", str(folder)], check=False)
else:
subprocess.run(["xdg-open", str(folder)], check=False)
return jsonify({"success": True})
except Exception:
app.logger.exception("open-folder failed")
return jsonify({"success": False, "error": "Could not open folder"}), 500
@app.route("/delivery/<client_name>/<export_folder>/", defaults={"filepath": ""})
@app.route("/delivery/<client_name>/<export_folder>/<path:filepath>")
def serve_delivery(client_name: str, export_folder: str, filepath: str):
"""Serve files from a client's Delivery folder so the viewer can load over HTTP (enables PDF search highlighting)."""
root = BASE / "Clients" / client_name / "Exports" / export_folder
root = root.resolve()
try:
root.relative_to(BASE.resolve())
except ValueError:
return jsonify({"error": "Invalid path"}), 404
if not root.is_dir():
return jsonify({"error": "Delivery folder not found"}), 404
if not filepath:
return jsonify({"error": "Specify a file path"}), 404
full = (root / filepath).resolve()
try:
full.relative_to(root)
except ValueError:
return jsonify({"error": "Invalid path"}), 404
if full.is_dir() or not full.is_file():
return jsonify({"error": "Not found"}), 404
return send_file(full, as_attachment=False, download_name=full.name)
@app.route("/stats/<client_name>", methods=["GET"])
def client_stats(client_name: str):
"""Get document counts for a client (total, verified, new, needs review, failed)."""
batches_path = BASE / "Clients" / client_name / "Batches"
if not batches_path.exists():
return jsonify({"error": f"Client not found: {client_name}"}), 404
counts = {"total": 0, "New": 0, "Verified": 0, "Needs Review": 0, "Failed": 0}
for date_folder in batches_path.iterdir():
if not date_folder.is_dir():
continue
for doc_folder in date_folder.iterdir():
if not doc_folder.is_dir():
continue
review_file = doc_folder / "review.json"
if not review_file.exists():
continue
try:
with review_file.open("r", encoding="utf-8") as f:
data = json.load(f)
status = data.get("status", "New")
counts["total"] += 1
if status in counts:
counts[status] += 1
else:
counts[status] = counts.get(status, 0) + 1
except Exception:
counts["total"] += 1
return jsonify({"client": client_name, "counts": counts})
def _find_doc_folder(client_name: str, doc_id: str):
"""Find the DOC-XXXXX folder for a client by scanning Batches/date/ folders. Returns Path or None."""
raw_doc_id = (doc_id or "").strip()
if len(raw_doc_id) >= 16 and raw_doc_id[10:16] == "__DOC-":
raw_doc_id = raw_doc_id[12:]
batches_path = BASE / "Clients" / client_name / "Batches"
if not batches_path.exists():
return None
for date_folder in batches_path.iterdir():
if not date_folder.is_dir():
continue
doc_folder = date_folder / raw_doc_id
if doc_folder.is_dir() and (doc_folder / "review.json").exists():
return doc_folder
return None
def _find_doc_folder_by_raw_source(client_name: str, raw_name: str):
"""Find a DOC folder whose review.json references the given raw source file."""
target = (raw_name or "").strip()
if not target:
return None
batches_path = _client_dir(client_name) / "Batches"
if not batches_path.exists():
return None
for date_folder in sorted(batches_path.iterdir(), reverse=True):
if not date_folder.is_dir():
continue
for doc_folder in sorted(date_folder.iterdir()):
if not doc_folder.is_dir() or not doc_folder.name.startswith("DOC-"):
continue
review_file = doc_folder / "review.json"
if not review_file.exists():
continue
try:
with review_file.open("r", encoding="utf-8") as f:
data = json.load(f)
except Exception:
continue
files = data.get("files") or {}
raw_candidates = []
for key in ("raw_source", "raw_image"):
value = files.get(key)
if value:
raw_candidates.append(str(value).strip())
for value in files.get("raw_images") or []:
if value:
raw_candidates.append(str(value).strip())
if target in raw_candidates:
return doc_folder, data
return None
def _review_needs_attention(review_data: dict, database_url: str) -> bool:
doc_type = (review_data.get("doc_type") or "").strip()
if not doc_type or doc_type.lower() == "unknown":
return True
config = document_config.find_document_config(doc_type, database_url)
if not config:
return True
fields = review_data.get("fields") or {}
required = config.get("required_fields") or []
if not (fields.get("property_address") or "").strip():
return True
if not required:
return False
filled = 0
for key in required:
value = fields.get(key, "")
if isinstance(value, str):
value = value.strip()
if value:
filled += 1
score = int((filled / len(required)) * 100) if required else 0
return score < 70
def _derive_intake_state(review_data: dict, database_url: str) -> str:
status = (review_data.get("status") or "").strip().casefold()
if status == "failed":
return "Failed"
if status in {"reprocessing", "processing"}:
return "Processing"
if _review_needs_attention(review_data, database_url):
return "Needs attention"
return "Ready for review"
@app.route("/docs/<client_name>", methods=["GET"])
def list_docs(client_name: str):
"""Return all documents for a client with review.json data and status counts."""
batches_path = _client_dir(client_name) / "Batches"
if not batches_path.exists():
return jsonify({"error": f"Client not found: {client_name}"}), 404
docs = []
counts = {"total": 0, "New": 0, "Verified": 0, "Needs Review": 0, "Failed": 0}
database_url = os.environ.get("DATABASE_URL", str(BASE / "portal.db"))
config_cache: dict[str, dict] = {}
for date_folder in sorted(batches_path.iterdir(), reverse=True):
if not date_folder.is_dir():
continue
batch_date = date_folder.name
for doc_folder in sorted(date_folder.iterdir()):
if not doc_folder.is_dir() or not doc_folder.name.startswith("DOC-"):
continue
review_file = doc_folder / "review.json"
if not review_file.exists():
continue
try:
with review_file.open("r", encoding="utf-8") as f:
data = json.load(f)
except Exception:
continue
status = data.get("status", "New")
review_meta = data.get("review", {}) or {}
doc_type = data.get("doc_type", "Unknown")
config = None
if doc_type:
config = config_cache.get(doc_type)
if config is None:
config = document_config.find_document_config(doc_type, database_url)
config_cache[doc_type] = config or {}
if config == {}:
config = None
counts["total"] += 1
if status in counts:
counts[status] += 1
else:
counts[status] = counts.get(status, 0) + 1
docs.append({
"doc_id": data.get("doc_id", doc_folder.name),
"doc_name": data.get("doc_name"),
"doc_type": doc_type,
"status": status,
"batch_date": batch_date,
"scanned_at": review_meta.get("scanned_at", ""),
"reviewed_at": review_meta.get("reviewed_at", ""),
"exported_at": review_meta.get("exported_at", ""),
"folder_path": str(doc_folder),
"fields": data.get("fields", {}),
"review": data.get("review", {}),
"page_count": data.get("page_count", 1),
"required_fields": (config or {}).get("required_fields", []),
"field_definitions": (config or {}).get("extraction_fields", []),
})
return jsonify({"docs": docs, "counts": counts})
@app.route("/intake-status/<client_name>/<raw_name>", methods=["GET"])
def intake_status(client_name: str, raw_name: str):
"""Resolve a raw imported file to its current intake/review state."""
database_url = os.environ.get("DATABASE_URL", str(BASE / "portal.db"))
resolved = _find_doc_folder_by_raw_source(client_name, raw_name)
if resolved:
doc_folder, review_data = resolved
return jsonify(
{
"success": True,
"raw_name": raw_name,
"intake_state": _derive_intake_state(review_data, database_url),
"doc_id": review_data.get("doc_id") or doc_folder.name,
"doc_type": review_data.get("doc_type") or "Unknown",
"status": review_data.get("status") or "New",
}
)
raw_path = _raw_dir(client_name) / raw_name
if raw_path.exists():
return jsonify(
{
"success": True,
"raw_name": raw_name,
"intake_state": "Processing",
"doc_id": "",
"doc_type": "Unknown",
"status": "",
}
)
return jsonify(
{
"success": False,
"raw_name": raw_name,
"intake_state": "Failed",
"doc_id": "",
"doc_type": "Unknown",
"status": "",
}
)
@app.route("/review/<client_name>/<doc_id>", methods=["POST"])
def save_review(client_name: str, doc_id: str):
"""Save review data (status, fields, review) to the document's review.json."""
doc_folder = _find_doc_folder(client_name, doc_id)
if not doc_folder:
return jsonify({"success": False, "error": f"Document not found: {client_name} / {doc_id}"}), 404
data = request.get_json(silent=True)
if not data:
return jsonify({"success": False, "error": "JSON body required"}), 400
review_path = doc_folder / "review.json"
try:
with review_path.open("r", encoding="utf-8") as f:
current = json.load(f)
except Exception as e:
return jsonify({"success": False, "error": f"Cannot read review.json: {e}"}), 500
if "status" in data:
current["status"] = data["status"]
if "doc_type" in data and data["doc_type"]:
current["doc_type"] = data["doc_type"]
if "fields" in data:
current["fields"] = data["fields"]
if "review" in data:
current["review"] = {**current.get("review", {}), **data["review"]}
try:
with review_path.open("w", encoding="utf-8") as f:
json.dump(current, f, indent=2, ensure_ascii=False)
except Exception as e:
return jsonify({"success": False, "error": f"Cannot write review.json: {e}"}), 500
# Auto-sync this document to portal (non-critical; failures are logged only)
try:
sync_single_doc(client_name, doc_id)
except Exception as e:
app.logger.warning(f"Portal sync failed for {doc_id}: {e}")
return jsonify({"success": True})
@app.route("/pdf/<client_name>/<doc_id>", methods=["GET"])
def serve_pdf(client_name: str, doc_id: str):
"""Serve the PDF file from the document folder for browser preview."""
doc_folder = _find_doc_folder(client_name, doc_id)
if not doc_folder:
return jsonify({"error": f"Document not found: {client_name} / {doc_id}"}), 404
pdf_path = None
for f in doc_folder.iterdir():
if f.is_file() and f.suffix.lower() == ".pdf":
pdf_path = f
break
if not pdf_path:
return jsonify({"error": "No PDF found in document folder"}), 404
return send_file(
pdf_path,
mimetype="application/pdf",
as_attachment=False,
download_name=pdf_path.name
)
@app.route("/raw-image/<client_name>/<filename>", methods=["GET"])
@app.route("/raw-file/<client_name>/<filename>", methods=["GET"])
def serve_raw_image(client_name: str, filename: str):
"""
Serve a raw source file from Clients/<client_name>/raw for ScanStation preview.
This lets the capture UI re-show images or PDFs from a previous session.
"""
raw_path = _raw_dir(client_name) / filename
if not raw_path.exists() or not raw_path.is_file():
return jsonify({"error": f"Raw file not found: {client_name} / {filename}"}), 404
return send_file(raw_path, as_attachment=False, download_name=raw_path.name)
@app.route("/raw-list/<client_name>", methods=["GET"])
def list_raw_images(client_name: str):
"""
List raw source files for a client so ScanStation can rebuild
its session queue after a restart purely from the filesystem.
"""
raw_dir = _client_dir(client_name) / "raw"
if not raw_dir.exists() or not raw_dir.is_dir():
return jsonify({"files": []})
exts = ALLOWED_RAW_EXTENSIONS
files = [
f.name
for f in sorted(raw_dir.iterdir())
if f.is_file() and f.suffix.lower() in exts
]
return jsonify({"files": files})
@app.route("/ocr-text/<client_name>/<doc_id>", methods=["GET"])
def ocr_text(client_name: str, doc_id: str):
"""Extract the OCR text layer from the document PDF and return it as plain text."""
doc_folder = _find_doc_folder(client_name, doc_id)
if not doc_folder:
return jsonify({"text": "", "error": f"Document not found: {client_name} / {doc_id}"}), 404
pdf_path = None
for f in doc_folder.iterdir():
if f.is_file() and f.suffix.lower() == ".pdf":
pdf_path = f
break
if not pdf_path:
return jsonify({"text": "", "error": "No PDF found in document folder"}), 404
try:
text = extract_text(str(pdf_path)) or ""
return jsonify({"text": text, "error": None})
except Exception as e:
return jsonify({"text": "", "error": f"Extraction failed: {e}"})
@app.route("/doc-image/<client_name>/<doc_id>", methods=["GET"])
def doc_image(client_name: str, doc_id: str):
"""Serve the raw image from a DOC folder for preview (e.g. rescan panel thumbnail)."""
doc_folder = _find_doc_folder(client_name, doc_id)
if not doc_folder:
return jsonify({"error": "Document not found"}), 404
for f in doc_folder.iterdir():
if f.is_file() and f.suffix.lower() in (".jpg", ".jpeg", ".png", ".tiff", ".tif", ".bmp"):
return send_file(f, as_attachment=False, download_name=f.name)
return jsonify({"error": "Image not found"}), 404
@app.route("/rescan-replace/<client_name>/<doc_id>", methods=["POST"])
def rescan_replace(client_name: str, doc_id: str):
"""
Accept a new image to replace the faulty scan for an existing document.
Saves the new image into the DOC folder, deletes old image/PDF,
updates review.json to Reprocessing, writes .reprocess trigger for watcher.
"""
doc_folder = _find_doc_folder(client_name, doc_id)
if not doc_folder:
return jsonify({"error": "Document not found"}), 404
if "image" not in request.files:
return jsonify({"error": "No image provided"}), 400
image_file = request.files["image"]
review_path = doc_folder / "review.json"
review_data = {}
if review_path.exists():
try:
with review_path.open("r", encoding="utf-8") as f:
review_data = json.load(f)
except Exception as e:
return jsonify({"error": f"Cannot read review.json: {e}"}), 500
# Delete old image and PDF files from the DOC folder
for f in list(doc_folder.iterdir()):
if f.is_file() and f.suffix.lower() in (".jpg", ".jpeg", ".png", ".tiff", ".tif", ".bmp", ".pdf"):
try:
f.unlink()
except Exception:
pass
ext = (Path(image_file.filename).suffix or ".jpeg").lower()
if ext not in (".jpg", ".jpeg", ".png", ".tiff", ".tif", ".bmp"):
ext = ".jpeg"
new_image_name = "rescan" + ext
new_image_path = doc_folder / new_image_name
try:
image_file.save(str(new_image_path))
except Exception as e:
return jsonify({"error": f"Failed to save image: {e}"}), 500
empty_fields = {}
database_url = os.environ.get("DATABASE_URL", str(BASE / "portal.db"))
config = document_config.find_document_config((review_data.get("doc_type") or "").strip(), database_url)
if config:
for item in config.get("extraction_fields") or []:
key = item.get("field_key")
if key:
empty_fields[key] = ""
else:
# Fallback for legacy template-based documents.
doc_type_raw = (review_data.get("doc_type") or "").strip().lower()
doc_type_template = DOC_TYPE_TO_TEMPLATE.get(doc_type_raw) or (review_data.get("doc_type_template") or "tenancy_agreement").strip().lower().replace(" ", "_").replace("(", "").replace(")", "")
if doc_type_template not in ("tenancy_agreement", "gas_safety_certificate", "eicr", "epc", "general_document"):
doc_type_template = "tenancy_agreement"
template_path = BASE / "Templates" / f"{doc_type_template}.json"
if template_path.exists():
try:
with template_path.open("r", encoding="utf-8") as f:
template = json.load(f)
for item in template.get("fields", []):
if isinstance(item, dict) and "key" in item:
empty_fields[item["key"]] = ""
except Exception:
pass
old_property = (review_data.get("fields") or {}).get("property_address", "")
if old_property:
empty_fields["property_address"] = old_property
review_meta = review_data.get("review") or {}
review_data["status"] = "Reprocessing"
review_data["fields"] = empty_fields
review_data["files"] = {"raw_image": new_image_name, "raw_source": new_image_name, "pdf": ""}
review_data["review"] = {
"reviewed_by": review_meta.get("reviewed_by", ""),
"reviewed_at": review_meta.get("reviewed_at", ""),
"scanned_at": "",
"notes": review_meta.get("notes", ""),
"exported_at": review_meta.get("exported_at", ""),
}
review_data["rescan_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
with review_path.open("w", encoding="utf-8") as f:
json.dump(review_data, f, indent=2, ensure_ascii=False)
except Exception as e:
return jsonify({"error": f"Cannot write review.json: {e}"}), 500
trigger_path = doc_folder / ".reprocess"
try:
trigger_path.write_text(new_image_name, encoding="utf-8")
except Exception as e:
return jsonify({"error": f"Cannot write trigger: {e}"}), 500
return jsonify({
"success": True,
"doc_id": doc_id,
"message": "New image saved. Waiting for reprocessing.",
})
@app.route("/reprocess/<client_name>/<doc_id>", methods=["POST"])
def reprocess_doc(client_name: str, doc_id: str):
"""
Mark a document for rescan. Does NOT copy to raw — document stays in DOC folder
waiting for replacement via /rescan-replace. Sets status to Sent to Rescan and
records reason in review.json and rescan_queue.json.
"""
doc_folder = _find_doc_folder(client_name, doc_id)
if not doc_folder:
return jsonify({"success": False, "error": f"Document not found: {client_name} / {doc_id}"}), 404
review_path = doc_folder / "review.json"
if not review_path.exists():
return jsonify({"success": False, "error": "review.json not found"}), 404
try:
with review_path.open("r", encoding="utf-8") as f:
data = json.load(f)
except Exception as e:
return jsonify({"success": False, "error": f"Cannot read review.json: {e}"}), 500
data_req = request.get_json(silent=True) or {}
reason = (data_req.get("reason") or "").strip() or "No reason given"
data["status"] = "Sent to Rescan"
review_meta = data.get("review") or {}
review_meta["notes"] = f"Rescan requested: {reason}"
review_meta["reviewed_by"] = review_meta.get("reviewed_by", "")
review_meta["reviewed_at"] = review_meta.get("reviewed_at", "")
data["review"] = review_meta
data["rescan_requested_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
with review_path.open("w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
except Exception as e:
return jsonify({"success": False, "error": f"Cannot write review.json: {e}"}), 500
queue_dir = BASE / "Clients" / client_name
queue_dir.mkdir(parents=True, exist_ok=True)
queue_path = queue_dir / "rescan_queue.json"
queue = []
if queue_path.exists():
try:
with queue_path.open("r", encoding="utf-8") as f:
queue = json.load(f) or []
except Exception:
queue = []
if not any(item.get("doc_id") == doc_id for item in queue):
queue.append({
"doc_id": doc_id,
"requested_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"reason": reason,
})
try:
with queue_path.open("w", encoding="utf-8") as f:
json.dump(queue, f, indent=2, ensure_ascii=False)
except Exception:
pass
return jsonify({"success": True, "doc_id": doc_id, "message": "Marked for rescan"})
@app.route("/exports/<client_name>", methods=["GET"])
def list_exports(client_name: str):
"""List previous export deliveries for a client."""
exports_dir = BASE / "Clients" / client_name / "Exports"
if not exports_dir.exists():