diff options
| author | Alexandre Flament <alex@al-f.net> | 2020-10-03 15:02:44 +0200 |
|---|---|---|
| committer | Alexandre FLAMENT <alexandre.flament@hesge.ch> | 2022-03-05 10:50:48 +0000 |
| commit | 1c7d8815fb54dd37ed6765560c223cb93976b6f4 (patch) | |
| tree | 14cdc2f8f9f7988d0026a92eab9098774db5b578 /searx/webutils.py | |
| parent | 927aa71133839ce5713b742b43285953301adf36 (diff) | |
[mod] URL for the static file contains the sha1
* allow to cache the static file forever
* avoid bugs when the static files are updated but not reloaded
Diffstat (limited to 'searx/webutils.py')
| -rw-r--r-- | searx/webutils.py | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/searx/webutils.py b/searx/webutils.py index 0c0854dfc..5be721eec 100644 --- a/searx/webutils.py +++ b/searx/webutils.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- import os +import pathlib import csv import hashlib import hmac import re import inspect import itertools -from typing import Iterable, List, Tuple +from typing import Iterable, List, Tuple, Dict from io import StringIO from codecs import getincrementalencoder @@ -58,13 +59,29 @@ def get_themes(templates_path): return themes -def get_static_files(static_path): - static_files = set() - static_path_length = len(static_path) + 1 - for directory, _, files in os.walk(static_path): - for filename in files: - f = os.path.join(directory[static_path_length:], filename) - static_files.add(f) +def get_hash_for_file(file: pathlib.Path) -> str: + m = hashlib.sha1() + with file.open('rb') as f: + m.update(f.read()) + return m.hexdigest() + + +def get_static_files(static_path: str) -> Dict[str, str]: + static_files: Dict[str, str] = {} + static_path_path = pathlib.Path(static_path) + + def walk(path: pathlib.Path): + for file in path.iterdir(): + if file.name.startswith('.'): + # ignore hidden file + continue + if file.is_file(): + static_files[str(file.relative_to(static_path_path))] = get_hash_for_file(file) + if file.is_dir() and file.name not in ('node_modules', 'src'): + # ignore "src" and "node_modules" directories + walk(file) + + walk(static_path_path) return static_files |