File size: 5,928 Bytes
522afb4
 
5ad6746
522afb4
 
 
5ad6746
20067fd
522afb4
 
 
 
 
5ad6746
20067fd
 
5ad6746
522afb4
 
 
 
 
 
 
20067fd
 
 
 
 
 
 
 
 
 
5ad6746
 
 
20067fd
 
5ad6746
 
 
 
 
20067fd
 
 
 
 
 
 
 
 
 
 
 
 
 
fa81fa1
 
 
 
 
20067fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ad6746
 
 
 
 
 
 
 
fa81fa1
 
 
 
 
5ad6746
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20067fd
 
 
5ad6746
 
 
522afb4
 
20067fd
522afb4
5ad6746
 
522afb4
 
 
 
5ad6746
 
522afb4
5ad6746
 
 
 
 
 
 
 
 
 
20067fd
5ad6746
 
522afb4
5ad6746
522afb4
5ad6746
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522afb4
20067fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import random
from pathlib import Path
from typing import Any, Dict, List

from fastapi import APIRouter, HTTPException, Query
from fastapi.responses import FileResponse

from src.app.settings import settings

router = APIRouter(prefix="/documents", tags=["documents"])

TEXT_SUFFIXES = {".txt", ".md", ".json", ".csv", ".xml", ".yaml", ".yml"}
WORD_SUFFIXES = {".doc", ".docx"}
SUPPORTED_SUFFIXES = TEXT_SUFFIXES | {".pdf"} | WORD_SUFFIXES
MAX_PREVIEW_CHARS = 5000


def _display_name(path: Path) -> str:
    stem = path.stem.replace("_", " ").replace("-", " ").strip()
    return stem or path.name


def _normalize_stem(path: Path) -> str:
    return path.stem.lower()


def _preferred_file(candidates: List[Path]) -> Path:
    priority = {".docx": 0, ".doc": 1, ".pdf": 2}
    return sorted(candidates, key=lambda fp: (priority.get(fp.suffix.lower(), 99), fp.name.lower()))[0]


def _raw_files(deduplicate: bool = False) -> List[Path]:
    raw_dir = Path(settings.raw_dir)
    if not raw_dir.exists():
        return []

    files = [
        fp
        for fp in raw_dir.glob("*")
        if fp.is_file() and fp.suffix.lower() in SUPPORTED_SUFFIXES
    ]

    if not deduplicate:
        return files

    grouped: Dict[str, List[Path]] = {}
    for fp in files:
        grouped.setdefault(_normalize_stem(fp), []).append(fp)

    return [_preferred_file(candidates) for candidates in grouped.values()]


def _read_word_preview(path: Path) -> str:
    if path.suffix.lower() == ".doc":
        return "(Định dạng .doc (Word 97-2003) chưa hỗ trợ render trực tiếp. Vui lòng dùng file .docx cùng tên nếu có.)"

    try:
        from docx import Document
    except ImportError:
        return "(Thiếu thư viện python-docx nên chưa thể trích xuất xem nhanh file .docx.)"

    doc = Document(str(path))
    chunks: List[str] = []
    total = 0

    for paragraph in doc.paragraphs:
        text = paragraph.text.strip()
        if not text:
            continue
        remain = MAX_PREVIEW_CHARS - total
        if remain <= 0:
            break
        snippet = text[:remain]
        chunks.append(snippet)
        total += len(snippet)

    return "\n".join(chunks)


def _read_preview(path: Path) -> str:
    suffix = path.suffix.lower()
    if suffix in TEXT_SUFFIXES:
        text = path.read_text(encoding="utf-8", errors="ignore")
        return text[:MAX_PREVIEW_CHARS]

    if suffix == ".pdf":
        try:
            from pypdf import PdfReader
        except ImportError:
            return "(Thiếu thư viện pypdf nên chưa thể trích xuất xem nhanh file PDF.)"

        reader = PdfReader(str(path))
        chunks: List[str] = []
        total = 0
        for page in reader.pages:
            page_text = (page.extract_text() or "").strip()
            if not page_text:
                continue
            remain = MAX_PREVIEW_CHARS - total
            if remain <= 0:
                break
            snippet = page_text[:remain]
            chunks.append(snippet)
            total += len(snippet)
        return "\n\n".join(chunks)

    if suffix in WORD_SUFFIXES:
        return _read_word_preview(path)

    return ""


@router.get("")
def list_documents() -> Dict[str, Any]:
    files = sorted(_raw_files(deduplicate=True))
    raw_dir = Path(settings.raw_dir)
    return {
        "items": [
            {
                "id": fp.stem,
                "filename": fp.name,
                "title": _display_name(fp),
                "size_bytes": fp.stat().st_size,
                "suffix": fp.suffix.lower(),
            }
            for fp in files
        ],
        "count": len(files),
        "raw_dir": str(raw_dir),
        "exists": raw_dir.exists(),
    }


@router.get("/random")
def random_documents(limit: int = Query(default=5, ge=1, le=20)) -> Dict[str, Any]:
    files = _raw_files(deduplicate=True)
    if not files:
        return {"items": [], "count": 0}

    picked = random.sample(files, k=min(limit, len(files)))
    return {
        "items": [
            {
                "filename": fp.name,
                "title": _display_name(fp),
                "suffix": fp.suffix.lower(),
                "size_bytes": fp.stat().st_size,
            }
            for fp in picked
        ],
        "count": len(picked),
    }


@router.get("/preview")
def preview_document(filename: str = Query(..., min_length=1)) -> Dict[str, Any]:
    raw_dir = Path(settings.raw_dir)
    file_path = (raw_dir / filename).resolve()

    if not raw_dir.exists() or raw_dir.resolve() not in file_path.parents:
        raise HTTPException(status_code=400, detail="Invalid document path")

    if not file_path.exists() or not file_path.is_file():
        raise HTTPException(status_code=404, detail="File not found")

    if file_path.suffix.lower() not in SUPPORTED_SUFFIXES:
        raise HTTPException(status_code=400, detail="Unsupported file type")

    preview_text = _read_preview(file_path).strip()
    return {
        "filename": file_path.name,
        "title": _display_name(file_path),
        "suffix": file_path.suffix.lower(),
        "preview": preview_text or "(Không trích xuất được nội dung xem nhanh)",
    }


@router.get('/file')
def serve_document_file(filename: str = Query(..., min_length=1)) -> FileResponse:
    raw_dir = Path(settings.raw_dir)
    file_path = (raw_dir / filename).resolve()

    if not raw_dir.exists() or raw_dir.resolve() not in file_path.parents:
        raise HTTPException(status_code=400, detail='Invalid document path')

    if not file_path.exists() or not file_path.is_file():
        raise HTTPException(status_code=404, detail='File not found')

    if file_path.suffix.lower() not in SUPPORTED_SUFFIXES:
        raise HTTPException(status_code=400, detail='Unsupported file type')

    return FileResponse(path=file_path, filename=file_path.name)