mirror of
https://codeberg.org/Reuh/feather.git
synced 2025-10-27 18:19:32 +00:00
feat: add hash to HTML filename in case of conflict
This commit is contained in:
parent
960e06252e
commit
0fd5ec6458
2 changed files with 27 additions and 16 deletions
|
|
@ -68,7 +68,6 @@ You need Python 3.12 or newer. Then pip it up, as the kids say.
|
||||||
- [ ] Write documentation
|
- [ ] Write documentation
|
||||||
- [ ] Use inotify for real-time article mark-as-read action
|
- [ ] Use inotify for real-time article mark-as-read action
|
||||||
- [ ] Share the fun somewhere
|
- [ ] Share the fun somewhere
|
||||||
- [ ] Actually think about the issues created by the duplicate warning
|
|
||||||
- [ ] Get article attachments
|
- [ ] Get article attachments
|
||||||
- [ ] Test with FreshRSS
|
- [ ] Test with FreshRSS
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,8 @@ class Article(ABC):
|
||||||
# no default value, computed by compute_fields
|
# no default value, computed by compute_fields
|
||||||
published_formatted: str # article publication time (text)
|
published_formatted: str # article publication time (text)
|
||||||
updated_formatted: str # article publication time (text)
|
updated_formatted: str # article publication time (text)
|
||||||
html_path: str # html path, relative to the html_root directory
|
# no default value, computed on save_html
|
||||||
|
html_path: str = None # html path, relative to the html_root directory (None will force it to be recomputed on next save_html)
|
||||||
# with default value
|
# with default value
|
||||||
unread: bool = True # if the article is unread
|
unread: bool = True # if the article is unread
|
||||||
title: str = "" # article title
|
title: str = "" # article title
|
||||||
|
|
@ -123,22 +124,17 @@ class Article(ABC):
|
||||||
config,
|
config,
|
||||||
config.article_filename_template.render(self._get_template_dict()),
|
config.article_filename_template.render(self._get_template_dict()),
|
||||||
)
|
)
|
||||||
|
html_path = category_directory / html_name
|
||||||
|
# Add hash if filename already exists
|
||||||
|
if html_path.exists():
|
||||||
|
id_hash = sha256(str(self.id).encode("utf-8")).hexdigest()[:8]
|
||||||
|
html_path = html_path.parent / sanitize_filename(
|
||||||
|
config, html_path.name, insert_before_suffix=f".{id_hash}"
|
||||||
|
)
|
||||||
|
|
||||||
return html_path.relative_to(config.html_root)
|
return html_path.relative_to(config.html_root)
|
||||||
|
|
||||||
def compute_fields(self):
|
def _get_template_dict(self) -> dict:
|
||||||
config = self.config
|
|
||||||
self.updated_formatted = format_datetime(config, self.updated)
|
|
||||||
self.published_formatted = format_datetime(config, self.published)
|
|
||||||
self.json_path = (
|
|
||||||
config.json_root
|
|
||||||
/ f"{sha256(str(self.id).encode('utf-8')).hexdigest()}.json"
|
|
||||||
)
|
|
||||||
self.html_path = str(
|
|
||||||
self.get_html_path().relative_to(config.html_root)
|
|
||||||
) # TODO: do this dynamically on write, handle overwrite conflict at the same time
|
|
||||||
|
|
||||||
def get_template_dict(self) -> dict:
|
|
||||||
template_fields = (
|
template_fields = (
|
||||||
"id",
|
"id",
|
||||||
"unread",
|
"unread",
|
||||||
|
|
@ -163,6 +159,16 @@ class Article(ABC):
|
||||||
d["category"] = self.category.asdict()
|
d["category"] = self.category.asdict()
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
def compute_fields(self):
|
||||||
|
config = self.config
|
||||||
|
self.updated_formatted = format_datetime(config, self.updated)
|
||||||
|
self.published_formatted = format_datetime(config, self.published)
|
||||||
|
self.json_path = (
|
||||||
|
config.json_root
|
||||||
|
/ f"{sha256(str(self.id).encode('utf-8')).hexdigest()}.json"
|
||||||
|
)
|
||||||
|
self.html_path = None
|
||||||
|
|
||||||
def write_json(self):
|
def write_json(self):
|
||||||
"""Write the JSON file associated with this article. Error if it already exists."""
|
"""Write the JSON file associated with this article. Error if it already exists."""
|
||||||
stored_fields = (
|
stored_fields = (
|
||||||
|
|
@ -208,8 +214,14 @@ class Article(ABC):
|
||||||
return html_path.exists()
|
return html_path.exists()
|
||||||
|
|
||||||
def write_html(self):
|
def write_html(self):
|
||||||
|
"""Write the HTML file associated with this article. Error if it already exists.
|
||||||
|
Note: this may recompute html_path, which is saved into the JSON - so make sure to save the JSON file _after_ the HTML file."""
|
||||||
# Write HTML file for a JSON object
|
# Write HTML file for a JSON object
|
||||||
config = self.config
|
config = self.config
|
||||||
|
|
||||||
|
if self.html_path is None:
|
||||||
|
self.html_path = str(self._get_html_path())
|
||||||
|
|
||||||
html_path = config.html_root / self.html_path
|
html_path = config.html_root / self.html_path
|
||||||
if html_path.exists():
|
if html_path.exists():
|
||||||
raise Exception(
|
raise Exception(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue