|
1 | | -import datetime as dt |
| 1 | +import datetime as dt, timezone |
2 | 2 | from collections.abc import Iterator |
3 | 3 | from dataclasses import dataclass, field |
4 | 4 | from pathlib import Path |
5 | 5 | from time import time |
| 6 | +import platform |
6 | 7 |
|
7 | 8 | import structlog |
8 | 9 | from src.core.constants import TS_FOLDER_NAME |
@@ -34,18 +35,39 @@ class RefreshDirTracker: |
34 | 35 | def files_count(self) -> int: |
35 | 36 | return len(self.files_not_in_library) |
36 | 37 |
|
| 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 | + |
37 | 55 | def save_new_files(self): |
38 | 56 | """Save the list of files that are not in the library.""" |
39 | 57 | 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 | + ) |
46 | 70 | ) |
47 | | - for entry_path in self.files_not_in_library |
48 | | - ] |
49 | 71 | self.library.add_entries(entries) |
50 | 72 |
|
51 | 73 | self.files_not_in_library = [] |
@@ -97,6 +119,15 @@ def refresh_dir(self, lib_path: Path) -> Iterator[int]: |
97 | 119 | # TODO - load these in batch somehow |
98 | 120 | if not self.library.has_path_entry(relative_path): |
99 | 121 | 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 | + |
100 | 131 |
|
101 | 132 | end_time_total = time() |
102 | 133 | yield dir_file_count |
|
0 commit comments