-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
355 lines (311 loc) · 13.1 KB
/
Copy pathapp.py
File metadata and controls
355 lines (311 loc) · 13.1 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
import os
import logging
import threading
import time
from flask import Flask, request, jsonify, render_template, Response, stream_with_context
from werkzeug.exceptions import HTTPException
from config import HYDRUS_API_URL, HYDRUS_ACCESS_KEY, DB_PATH, SYNC_INTERVAL_MINUTES, HOST, PORT
from hydrus_client import HydrusClient
from indexer import NoteIndexer
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
logger = logging.getLogger("hynotes.app")
app = Flask(__name__, static_folder="static", template_folder="templates")
# Initialize client and indexer
hydrus = HydrusClient(api_url=HYDRUS_API_URL, access_key=HYDRUS_ACCESS_KEY)
indexer = NoteIndexer(db_path=DB_PATH, hydrus_client=hydrus)
# Background sync worker with lock prevention
sync_lock = threading.Lock()
sync_state_lock = threading.Lock()
sync_state = {
"running": False,
"last_started_at": None,
"last_completed_at": None,
"last_result": None,
"last_error": None,
}
background_thread_lock = threading.Lock()
background_thread_started = False
def run_sync_safely() -> bool:
"""Run note sync if no sync is currently in progress. Returns True if sync ran."""
if not sync_lock.acquire(blocking=False):
logger.info("Sync skipped: sync already in progress.")
return False
try:
with sync_state_lock:
sync_state.update({
"running": True,
"last_started_at": int(time.time()),
"last_error": None,
})
result = indexer.sync_notes_from_hydrus()
with sync_state_lock:
sync_state["last_result"] = result
return True
except Exception as e:
logger.exception("Sync error")
with sync_state_lock:
sync_state["last_error"] = str(e)
return False
finally:
with sync_state_lock:
sync_state["running"] = False
sync_state["last_completed_at"] = int(time.time())
sync_lock.release()
def background_sync():
"""Background thread to periodically sync notes from Hydrus."""
logger.info("Background sync thread started.")
while True:
try:
run_sync_safely()
except Exception as e:
logger.error(f"Background sync worker error: {e}")
time.sleep(SYNC_INTERVAL_MINUTES * 60)
def start_background_sync_once():
"""Start the periodic worker once when the application is actually run."""
global background_thread_started
with background_thread_lock:
if background_thread_started:
return
thread = threading.Thread(target=background_sync, daemon=True, name="hynotes-sync")
thread.start()
background_thread_started = True
@app.after_request
def add_headers(response):
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
response.headers["Access-Control-Allow-Methods"] = "*"
path = request.path
if path.startswith("/thumbnail/") or path.startswith("/file/"):
response.headers["Cache-Control"] = "private, max-age=86400"
elif path.startswith("/static/"):
response.headers["Cache-Control"] = "public, max-age=3600"
else:
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
return response
@app.errorhandler(Exception)
def handle_exception(e):
if isinstance(e, HTTPException):
return jsonify({"error": e.description}), e.code
logger.error(f"Unhandled server error: {e}", exc_info=True)
return jsonify({"error": "The server could not complete this request."}), 500
# --- Routes ---
@app.route("/")
def index():
return render_template("index.html")
@app.route("/api/status")
def status():
"""Check API connection status and indexer database metrics."""
try:
ver = hydrus.get_api_version()
connection_ok = True
hydrus_version = ver.get("hydrus_version", "unknown")
except Exception as e:
connection_ok = False
hydrus_version = str(e)
# Get local stats
with indexer._get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM files WHERE has_notes = 1")
indexed_files = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM notes")
total_notes = cursor.fetchone()[0]
with sync_state_lock:
current_sync_state = dict(sync_state)
return jsonify({
"status": "ok",
"hydrus_url": HYDRUS_API_URL,
"hydrus_connected": connection_ok,
"hydrus_version": hydrus_version,
"indexed_files": indexed_files,
"total_notes": total_notes,
"sync": current_sync_state,
})
@app.route("/api/sync", methods=["POST"])
def trigger_sync():
"""Manually trigger a note sync pass."""
if sync_lock.locked():
return jsonify({"message": "Sync is already in progress", "status": "busy"}), 409
def run_async():
run_sync_safely()
thread = threading.Thread(target=run_async, daemon=True, name="hynotes-manual-sync")
thread.start()
return jsonify({"message": "Sync started in background", "status": "started"})
@app.route("/api/search/unified")
def search_unified():
"""Unified Tag search + FTS Note text filter endpoint with notes_only mode support."""
tags_param = request.args.get("tags", "").strip()
word_query = request.args.get("q", "").strip()
notes_only = request.args.get("notes_only", "true").lower() == "true"
limit = min(max(int(request.args.get("limit", 60)), 1), 120)
offset = max(int(request.args.get("offset", 0)), 0)
try:
if notes_only:
# Mode A: Notes only mode
effective_tags = tags_param if tags_param else "system:has notes"
tags = [t.strip() for t in effective_tags.split(",") if t.strip()]
matching_hashes = hydrus.search_files(tags=tags) if tags else None
res = indexer.search_note_files(query=word_query, hashes=matching_hashes, limit=limit, offset=offset)
for r in res.get("results", []):
r["has_notes"] = True
r["notes"] = indexer.get_notes_for_file(r["hash"])
return jsonify(res)
else:
# Mode B: All Hydrus Files search
effective_tags = tags_param if tags_param else "system:everything"
tags = [t.strip() for t in effective_tags.split(",") if t.strip()]
all_hashes = hydrus.search_files(tags=tags)
total = len(all_hashes)
paged_hashes = all_hashes[offset:offset + limit]
hashes_with_notes = indexer.get_hashes_with_notes(paged_hashes)
results = []
for h in paged_hashes:
has_n = h in hashes_with_notes
local_notes = indexer.get_notes_for_file(h) if has_n else {}
note_name = next(iter(local_notes.keys())) if local_notes else None
first_text = local_notes[note_name] if note_name else None
snippet = (first_text[:200] + ("..." if len(first_text) > 200 else "")) if first_text else None
results.append({
"hash": h,
"note_name": note_name,
"snippet": snippet,
"has_notes": has_n,
"notes": local_notes
})
return jsonify({"results": results, "total": total, "limit": limit, "offset": offset})
except Exception as e:
logger.error(f"Unified search error: {e}")
return jsonify({"error": str(e)}), 500
@app.route("/api/file/<file_hash>/metadata")
def get_metadata(file_hash):
"""Fetch metadata and notes for a specific file hash cleanly merging Hydrus and local SQLite."""
clean_hash = file_hash.strip().lower()
try:
meta = {}
try:
meta_list = hydrus.get_file_metadata(hashes=[clean_hash], include_notes=True)
if meta_list:
meta = meta_list[0]
except Exception as e:
logger.warning(f"Hydrus get_file_metadata failed for {clean_hash}: {e}")
# Always fetch local notes and file record from SQLite
local_notes = indexer.get_notes_for_file(clean_hash)
local_info = indexer.get_file_info(clean_hash)
merged_notes = {}
if meta and isinstance(meta.get("notes"), dict):
merged_notes.update(meta["notes"])
if local_notes:
merged_notes.update(local_notes)
meta["hash"] = clean_hash
meta["notes"] = merged_notes
if local_info:
meta["mime"] = meta.get("mime") or local_info.get("mime") or "unknown"
meta["width"] = meta.get("width") or local_info.get("width") or 0
meta["height"] = meta.get("height") or local_info.get("height") or 0
meta["size"] = meta.get("size") or local_info.get("size") or 0
return jsonify(meta)
except Exception as e:
logger.error(f"Error fetching metadata for {clean_hash}: {e}")
return jsonify({"error": str(e)}), 500
@app.route("/api/file/<file_hash>/notes", methods=["GET", "POST", "DELETE"])
def handle_notes(file_hash):
"""GET, POST, or DELETE notes for a file."""
if request.method == "GET":
notes = indexer.get_notes_for_file(file_hash)
return jsonify({"notes": notes})
elif request.method == "POST":
data = request.get_json() or {}
note_name = data.get("name", "").strip()
note_text = data.get("text", "")
if not note_name:
return jsonify({"error": "Note name is required"}), 400
if not note_text.strip():
return jsonify({"error": "Note text is required"}), 400
try:
notes = indexer.update_note_local(file_hash, note_name, note_text)
return jsonify({
"status": "success",
"note_name": note_name,
"note_text": note_text,
"notes": notes,
})
except Exception as e:
logger.exception("Hydrus rejected note write for %s", file_hash)
return jsonify({
"error": "Hydrus did not accept the note. Nothing was changed in the local index.",
"detail": str(e),
}), 502
elif request.method == "DELETE":
data = request.get_json() or {}
note_name = data.get("name", "").strip()
if not note_name:
return jsonify({"error": "Note name is required"}), 400
try:
notes = indexer.delete_note_local(file_hash, note_name)
return jsonify({"status": "success", "notes": notes})
except Exception as e:
logger.exception("Hydrus rejected note deletion for %s", file_hash)
return jsonify({
"error": "Hydrus did not accept the deletion. Nothing was changed in the local index.",
"detail": str(e),
}), 502
@app.route("/thumbnail/<file_hash>")
def proxy_thumbnail(file_hash):
"""Proxy thumbnail binary stream from Hydrus."""
try:
upstream = hydrus.get_thumbnail_stream(file_hash)
return proxy_stream(
upstream,
chunk_size=8192,
allowed_headers={"content-type", "content-length"},
)
except Exception as e:
logger.error(f"Error streaming thumbnail for {file_hash}: {e}")
return jsonify({"error": str(e)}), 500
@app.route("/file/<file_hash>")
def proxy_file(file_hash):
"""Proxy full file binary stream from Hydrus."""
try:
upstream = hydrus.get_file_stream(file_hash)
return proxy_stream(
upstream,
chunk_size=16384,
allowed_headers={"content-type", "content-length", "content-disposition"},
)
except Exception as e:
logger.error(f"Error streaming file for {file_hash}: {e}")
return jsonify({"error": str(e)}), 500
def proxy_stream(upstream, chunk_size: int, allowed_headers: set[str]) -> Response:
"""Relay a Hydrus stream and always release its socket/file descriptor."""
try:
upstream.raise_for_status()
except Exception:
upstream.close()
raise
headers = [
(name, value)
for name, value in upstream.headers.items()
if name.lower() in allowed_headers
]
def generate():
try:
yield from upstream.iter_content(chunk_size=chunk_size)
finally:
upstream.close()
response = Response(
stream_with_context(generate()),
status=upstream.status_code,
headers=headers,
)
# Also cover clients that disconnect before Flask iterates or exhausts the
# generator. requests.Response.close() is safe to call more than once.
response.call_on_close(upstream.close)
return response
if __name__ == "__main__":
start_background_sync_once()
print(f"==================================================")
print(f" HyNotes Server starting on http://{HOST}:{PORT}")
print(f" Hydrus API Target: {HYDRUS_API_URL}")
print(f"==================================================")
app.run(host=HOST, port=PORT, debug=False)