Skip to content
This repository was archived by the owner on Mar 2, 2024. It is now read-only.

Commit a0c7f5f

Browse files
committed
fix: allow space within file name
1 parent be7842d commit a0c7f5f

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/wikmd/utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
import os
2+
import unicodedata
23

4+
_windows_device_files = {
5+
"CON",
6+
"PRN",
7+
"AUX",
8+
"NUL",
9+
*(f"COM{i}" for i in range(10)),
10+
*(f"LPT{i}" for i in range(10)),
11+
}
12+
13+
14+
def secure_filename(filename: str) -> str:
15+
"""Convert your filename to be safe for the os.
16+
17+
Function from werkzeug. Changed to allow space in the file name.
18+
"""
19+
filename = unicodedata.normalize("NFKD", filename)
20+
filename = filename.encode("ascii", "ignore").decode("ascii")
21+
for sep in os.sep, os.path.altsep:
22+
if sep:
23+
filename = filename.replace(sep, "_")
24+
filename = filename.strip("._")
25+
# on nt a couple of special files are present in each folder. We
26+
# have to ensure that the target file is not such a filename. In
27+
# this case we prepend an underline
28+
if (
29+
os.name == "nt"
30+
and filename
31+
and filename.split(".")[0].upper() in _windows_device_files
32+
):
33+
filename = f"_{filename}"
34+
35+
return filename
336

437
def pathify(path1, path2):
538
"""

src/wikmd/wiki.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
url_for,
2323
)
2424
from lxml.html.clean import clean_html
25-
from werkzeug.utils import safe_join, secure_filename
25+
from werkzeug.utils import safe_join
2626
from wikmd import knowledge_graph
2727
from wikmd.cache import Cache
2828
from wikmd.config import WikmdConfig
2929
from wikmd.git_manager import WikiRepoManager
3030
from wikmd.image_manager import ImageManager
3131
from wikmd.plugins.load_plugins import PluginLoader
3232
from wikmd.search import Search, Watchdog
33-
from wikmd.utils import pathify
33+
from wikmd.utils import pathify, secure_filename
3434
from wikmd.web_dependencies import get_web_deps
3535

3636
SESSIONS = []

0 commit comments

Comments
 (0)