Initial commit

This commit is contained in:
Will Miao
2025-01-25 19:22:02 +08:00
commit ad6137d355
13 changed files with 1069 additions and 0 deletions

48
utils/file_utils.py Normal file
View File

@@ -0,0 +1,48 @@
import os
import hashlib
import json
from typing import Dict, Optional
async def calculate_sha256(file_path: str) -> str:
"""Calculate SHA256 hash of a file"""
sha256_hash = hashlib.sha256()
with open(file_path, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
async def get_file_info(file_path: str) -> Dict:
"""Get basic file information"""
return {
"name": os.path.splitext(os.path.basename(file_path))[0],
"file_path": file_path,
"size": os.path.getsize(file_path),
"modified": os.path.getmtime(file_path),
"sha256": await calculate_sha256(file_path)
}
async def save_metadata(file_path: str, metadata: Dict) -> None:
"""Save metadata to .metadata.json file"""
metadata_path = f"{os.path.splitext(file_path)[0]}.metadata.json"
try:
with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(metadata, f, indent=2, ensure_ascii=False)
except Exception as e:
print(f"Error saving metadata to {metadata_path}: {str(e)}")
async def load_metadata(file_path: str) -> Dict:
"""Load metadata from .metadata.json file"""
metadata_path = f"{os.path.splitext(file_path)[0]}.metadata.json"
try:
if os.path.exists(metadata_path):
with open(metadata_path, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f"Error loading metadata from {metadata_path}: {str(e)}")
return {}
async def update_civitai_metadata(file_path: str, civitai_data: Dict) -> None:
"""Update metadata file with Civitai data"""
metadata = await load_metadata(file_path)
metadata['civitai'] = civitai_data
await save_metadata(file_path, metadata)