Skip to content

Commit c1b87cc

Browse files
committed
feat: start storing date_created and date_modified
1 parent ba7e130 commit c1b87cc

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

tagstudio/src/core/library/alchemy/enums.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class ItemType(enum.Enum):
6363

6464
class SortingModeEnum(enum.Enum):
6565
DATE_ADDED = "file.date_added"
66+
Date_CREATED = "file.date_created"
67+
DATE_MODIFIED = "file.date_modified"
6668

6769

6870
@dataclass

tagstudio/src/core/library/alchemy/library.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,15 @@ def get_entry_full_by_path(self, path: Path) -> Entry | None:
517517
make_transient(entry)
518518
return entry
519519

520+
def get_entry_by_path(self, path: Path) -> Entry | None:
521+
"""Get the entry with the corresponding path."""
522+
with Session(self.engine) as session:
523+
entry = session.scalar(select(Entry).where(Entry.path == path))
524+
if entry:
525+
session.expunge(entry)
526+
make_transient(entry)
527+
return entry
528+
520529
@property
521530
def entries_count(self) -> int:
522531
with Session(self.engine) as session:

tagstudio/src/core/utils/refresh_dir.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import datetime as dt
1+
import datetime as dt, timezone
22
from collections.abc import Iterator
33
from dataclasses import dataclass, field
44
from pathlib import Path
55
from time import time
6+
import platform
67

78
import structlog
89
from src.core.constants import TS_FOLDER_NAME
@@ -34,18 +35,39 @@ class RefreshDirTracker:
3435
def files_count(self) -> int:
3536
return len(self.files_not_in_library)
3637

38+
def get_file_times(self, file_path: Path):
39+
"""Get the creation and modification times of a file."""
40+
stat = file_path.stat()
41+
system = platform.system()
42+
if system == 'Windows': # Windows
43+
date_created = dt.fromtimestamp(stat.st_ctime, timezone.utc)
44+
elif system == 'Darwin': # macOS
45+
date_created = dt.fromtimestamp(stat.st_birthtime, timezone.utc)
46+
else: # Linux and other systems
47+
try:
48+
date_created = dt.fromtimestamp(stat.st_birthtime, timezone.utc)
49+
except AttributeError:
50+
# st_birthtime is not available on some Linux filesystems
51+
date_created = dt.fromtimestamp(stat.st_ctime, timezone.utc)
52+
date_modified = dt.fromtimestamp(stat.st_mtime, timezone.utc)
53+
return date_created, date_modified
54+
3755
def save_new_files(self):
3856
"""Save the list of files that are not in the library."""
3957
if self.files_not_in_library:
40-
entries = [
41-
Entry(
42-
path=entry_path,
43-
folder=self.library.folder,
44-
fields=[],
45-
date_added=dt.datetime.now(),
58+
entries = []
59+
for entry_path in self.files_not_in_library:
60+
date_created, date_modified = self.get_file_times(entry_path)
61+
entries.append(
62+
Entry(
63+
path=entry_path,
64+
folder=self.library.folder,
65+
fields=[],
66+
date_added=dt.now(timezone.utc),
67+
date_created=date_created,
68+
date_modified=date_modified,
69+
)
4670
)
47-
for entry_path in self.files_not_in_library
48-
]
4971
self.library.add_entries(entries)
5072

5173
self.files_not_in_library = []
@@ -97,6 +119,15 @@ def refresh_dir(self, lib_path: Path) -> Iterator[int]:
97119
# TODO - load these in batch somehow
98120
if not self.library.has_path_entry(relative_path):
99121
self.files_not_in_library.append(relative_path)
122+
else:
123+
# Update date_modified for existing entries if it has changed
124+
entry = self.library.get_entry_by_path(relative_path)
125+
if entry:
126+
date_modified = datetime.fromtimestamp(f.stat().st_mtime, timezone.utc)
127+
if entry.date_modified != date_modified:
128+
entry.date_modified = date_modified
129+
self.library.update_entry(entry)
130+
100131

101132
end_time_total = time()
102133
yield dir_file_count

0 commit comments

Comments
 (0)