179 lines
6.1 KiB
Python
179 lines
6.1 KiB
Python
"""
|
|
Path Fix & Diagnostic Script
|
|
Fixes backslash paths in database and checks file existence
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, script_dir)
|
|
|
|
def find_database():
|
|
"""Find the database file in common locations."""
|
|
possible_paths = [
|
|
os.path.join(script_dir, 'instance', 'lego_instructions.db'),
|
|
os.path.join(script_dir, 'lego_instructions.db'),
|
|
os.path.join(script_dir, 'app', 'lego_instructions.db'),
|
|
]
|
|
|
|
for path in possible_paths:
|
|
if os.path.exists(path):
|
|
return path
|
|
|
|
return None
|
|
|
|
def main():
|
|
# Find database
|
|
db_path = find_database()
|
|
|
|
if not db_path:
|
|
print("=" * 70)
|
|
print("❌ Database not found!")
|
|
print("=" * 70)
|
|
print()
|
|
print("Searched in:")
|
|
print(" - E:\\LIM\\instance\\lego_instructions.db")
|
|
print(" - E:\\LIM\\lego_instructions.db")
|
|
print(" - E:\\LIM\\app\\lego_instructions.db")
|
|
print()
|
|
return
|
|
|
|
print(f"Found database: {db_path}")
|
|
print()
|
|
|
|
# Set database URL
|
|
db_uri = 'sqlite:///' + db_path.replace('\\', '/')
|
|
os.environ['DATABASE_URL'] = db_uri
|
|
|
|
from app import create_app, db
|
|
from app.models.instruction import Instruction
|
|
from app.models.set import Set
|
|
from flask import current_app
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
print("=" * 70)
|
|
print("LEGO Instructions Manager - Path Fix & Diagnostics")
|
|
print("=" * 70)
|
|
print()
|
|
|
|
upload_folder = current_app.config['UPLOAD_FOLDER']
|
|
print(f"Upload folder: {upload_folder}")
|
|
print()
|
|
|
|
# Check instructions
|
|
instructions = Instruction.query.all()
|
|
print(f"Found {len(instructions)} instruction files")
|
|
print()
|
|
|
|
fixed_paths = 0
|
|
fixed_thumbnails = 0
|
|
missing_files = []
|
|
|
|
for instruction in instructions:
|
|
# Fix file_path backslashes
|
|
if '\\' in instruction.file_path:
|
|
old_path = instruction.file_path
|
|
instruction.file_path = instruction.file_path.replace('\\', '/')
|
|
fixed_paths += 1
|
|
print(f"Fixed path: {old_path} → {instruction.file_path}")
|
|
|
|
# Fix thumbnail_path backslashes
|
|
if instruction.thumbnail_path and '\\' in instruction.thumbnail_path:
|
|
old_thumb = instruction.thumbnail_path
|
|
instruction.thumbnail_path = instruction.thumbnail_path.replace('\\', '/')
|
|
fixed_thumbnails += 1
|
|
print(f"Fixed thumbnail: {old_thumb} → {instruction.thumbnail_path}")
|
|
|
|
# Check if file exists
|
|
full_path = os.path.join(upload_folder, instruction.file_path)
|
|
if not os.path.exists(full_path):
|
|
missing_files.append({
|
|
'id': instruction.id,
|
|
'file_name': instruction.file_name,
|
|
'path': instruction.file_path,
|
|
'full_path': full_path,
|
|
'set_id': instruction.set_id
|
|
})
|
|
|
|
# Fix cover images
|
|
sets = Set.query.all()
|
|
fixed_covers = 0
|
|
|
|
for lego_set in sets:
|
|
if lego_set.cover_image and '\\' in lego_set.cover_image:
|
|
old_cover = lego_set.cover_image
|
|
lego_set.cover_image = lego_set.cover_image.replace('\\', '/')
|
|
fixed_covers += 1
|
|
print(f"Fixed cover: {old_cover} → {lego_set.cover_image}")
|
|
|
|
# Commit changes
|
|
if fixed_paths or fixed_thumbnails or fixed_covers:
|
|
db.session.commit()
|
|
print()
|
|
print("=" * 70)
|
|
print("✅ Database Updates:")
|
|
print("=" * 70)
|
|
print(f" • Fixed {fixed_paths} instruction file paths")
|
|
print(f" • Fixed {fixed_thumbnails} thumbnail paths")
|
|
print(f" • Fixed {fixed_covers} cover image paths")
|
|
print()
|
|
else:
|
|
print()
|
|
print("✅ No path fixes needed - all paths are correct!")
|
|
print()
|
|
|
|
# Report missing files
|
|
if missing_files:
|
|
print("=" * 70)
|
|
print("⚠️ WARNING: Missing Files Detected")
|
|
print("=" * 70)
|
|
print()
|
|
for item in missing_files:
|
|
lego_set = Set.query.get(item['set_id'])
|
|
print(f"Missing: {item['file_name']}")
|
|
print(f" Set: {lego_set.set_number if lego_set else 'Unknown'}")
|
|
print(f" DB Path: {item['path']}")
|
|
print(f" Full Path: {item['full_path']}")
|
|
print(f" Instruction ID: {item['id']}")
|
|
print()
|
|
|
|
print(f"Total missing: {len(missing_files)} files")
|
|
print()
|
|
print("These files may have been:")
|
|
print(" • Deleted manually from disk")
|
|
print(" • Never uploaded properly")
|
|
print(" • Moved to wrong location")
|
|
print()
|
|
print("To fix:")
|
|
print(" 1. Re-upload the files, or")
|
|
print(" 2. Delete the database records:")
|
|
print(f" DELETE FROM instructions WHERE id IN ({','.join(str(x['id']) for x in missing_files)});")
|
|
print()
|
|
else:
|
|
print("=" * 70)
|
|
print("✅ All Files Verified")
|
|
print("=" * 70)
|
|
print(" All instruction files exist on disk!")
|
|
print()
|
|
|
|
# Summary
|
|
print("=" * 70)
|
|
print("Summary")
|
|
print("=" * 70)
|
|
print(f" Total instructions: {len(instructions)}")
|
|
print(f" Paths fixed: {fixed_paths + fixed_thumbnails + fixed_covers}")
|
|
print(f" Files verified: {len(instructions) - len(missing_files)}")
|
|
print(f" Missing files: {len(missing_files)}")
|
|
print()
|
|
|
|
if fixed_paths or fixed_thumbnails or fixed_covers:
|
|
print("✅ Restart Flask to see changes!")
|
|
|
|
print()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|