diff --git a/src/basic_memory/api/routers/resource_router.py b/src/basic_memory/api/routers/resource_router.py index 59cc9273..1b64c0c3 100644 --- a/src/basic_memory/api/routers/resource_router.py +++ b/src/basic_memory/api/routers/resource_router.py @@ -25,6 +25,17 @@ from datetime import datetime router = APIRouter(prefix="/resource", tags=["resources"]) +def _mtime_to_datetime(entity: EntityModel) -> datetime: + """Convert entity mtime (file modification time) to datetime. + + Returns the file's actual modification time, falling back to updated_at + if mtime is not available. + """ + if entity.mtime: + return datetime.fromtimestamp(entity.mtime).astimezone() + return entity.updated_at + + def get_entity_ids(item: SearchIndexRow) -> set[int]: match item.type: case SearchItemType.ENTITY: @@ -97,7 +108,7 @@ async def get_resource_content( # Read content for each entity content = await file_service.read_entity_content(result) memory_url = normalize_memory_url(result.permalink) - modified_date = result.updated_at.isoformat() + modified_date = _mtime_to_datetime(result).isoformat() checksum = result.checksum[:8] if result.checksum else "" # Prepare the delimited content diff --git a/src/basic_memory/services/directory_service.py b/src/basic_memory/services/directory_service.py index 3e504c83..90e6ee76 100644 --- a/src/basic_memory/services/directory_service.py +++ b/src/basic_memory/services/directory_service.py @@ -3,7 +3,9 @@ import fnmatch import logging import os +from datetime import datetime from typing import Dict, List, Optional, Sequence + import logfire from basic_memory.models import Entity @@ -13,6 +15,17 @@ from basic_memory.schemas.directory import DirectoryNode logger = logging.getLogger(__name__) +def _mtime_to_datetime(entity: Entity) -> datetime: + """Convert entity mtime (file modification time) to datetime. + + Returns the file's actual modification time, falling back to updated_at + if mtime is not available. + """ + if entity.mtime: + return datetime.fromtimestamp(entity.mtime).astimezone() + return entity.updated_at + + class DirectoryService: """Service for working with directory trees.""" @@ -79,7 +92,7 @@ class DirectoryService: entity_id=file.id, entity_type=file.entity_type, content_type=file.content_type, - updated_at=file.updated_at, + updated_at=_mtime_to_datetime(file), ) # Add to parent directory's children @@ -245,7 +258,7 @@ class DirectoryService: entity_id=file.id, entity_type=file.entity_type, content_type=file.content_type, - updated_at=file.updated_at, + updated_at=_mtime_to_datetime(file), ) # Add to parent directory's children diff --git a/src/basic_memory/services/search_service.py b/src/basic_memory/services/search_service.py index 5dd990ff..44c887fd 100644 --- a/src/basic_memory/services/search_service.py +++ b/src/basic_memory/services/search_service.py @@ -17,6 +17,17 @@ from basic_memory.schemas.search import SearchQuery, SearchItemType from basic_memory.services import FileService +def _mtime_to_datetime(entity: Entity) -> datetime: + """Convert entity mtime (file modification time) to datetime. + + Returns the file's actual modification time, falling back to updated_at + if mtime is not available. + """ + if entity.mtime: + return datetime.fromtimestamp(entity.mtime).astimezone() + return entity.updated_at + + class SearchService: """Service for search operations. @@ -200,7 +211,7 @@ class SearchService: "entity_type": entity.entity_type, }, created_at=entity.created_at, - updated_at=entity.updated_at, + updated_at=_mtime_to_datetime(entity), project_id=entity.project_id, ) ) @@ -279,7 +290,7 @@ class SearchService: "entity_type": entity.entity_type, }, created_at=entity.created_at, - updated_at=entity.updated_at, + updated_at=_mtime_to_datetime(entity), project_id=entity.project_id, ) ) @@ -313,7 +324,7 @@ class SearchService: "tags": obs.tags, }, created_at=entity.created_at, - updated_at=entity.updated_at, + updated_at=_mtime_to_datetime(entity), project_id=entity.project_id, ) ) @@ -343,7 +354,7 @@ class SearchService: to_id=rel.to_id, relation_type=rel.relation_type, created_at=entity.created_at, - updated_at=entity.updated_at, + updated_at=_mtime_to_datetime(entity), project_id=entity.project_id, ) )