Files
ObsidianAI/fix_sizes.py

77 lines
2.1 KiB
Python
Raw Normal View History

2026-01-03 10:23:05 -06:00
#!/usr/bin/env python3
"""
Fix LiveSync corruption by recalculating file sizes
The corruption happens when size field doesn't match actual chunk content size
"""
import couchdb
import time
from urllib.parse import quote
USER = "admin"
PASSWORD = "DonCucarach0!?"
IP_ADDRESS = "100.100.112.48"
PORT = "5984"
DB_NAME = "obsidiandb"
def fix_sizes():
safe_password = quote(PASSWORD, safe="")
safe_username = quote(USER, safe="")
url = f"http://{safe_username}:{safe_password}@{IP_ADDRESS}:{PORT}/"
server = couchdb.Server(url)
db = server[DB_NAME]
print("Scanning for size mismatches...")
fixed = []
correct = []
for row in db.view('_all_docs', include_docs=True):
doc = row.doc
# Only process file metadata docs
if 'children' not in doc or not isinstance(doc.get('children'), list):
continue
path = doc.get('path', doc['_id'])
stored_size = doc.get('size', 0)
# Calculate actual size from chunks
actual_size = 0
for chunk_id in doc['children']:
if chunk_id in db:
chunk = db[chunk_id]
data = chunk.get('data', chunk.get('content', ''))
actual_size += len(str(data))
if stored_size != actual_size:
print(f"{path}")
print(f" Stored: {stored_size}, Actual: {actual_size}")
# Fix it
doc['size'] = actual_size
doc['mtime'] = int(time.time() * 1000) # Trigger sync
try:
db.save(doc)
fixed.append(path)
print(f" ✓ Fixed")
except Exception as e:
print(f" ✗ Failed: {e}")
else:
correct.append(path)
print(f"\n{'='*60}")
print(f"✅ Correct: {len(correct)} files")
print(f"🔧 Fixed: {len(fixed)} files")
if fixed:
print(f"\nFixed files:")
for f in fixed:
print(f" - {f}")
print(f"\n✅ All sizes corrected! Obsidian should now sync properly.")
if __name__ == "__main__":
fix_sizes()