2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-01-31 15:32:35 +00:00

utf8 encoding checks when reading files in the view

This commit is contained in:
Philip Sargent 2022-06-23 19:01:25 +03:00
parent 6de9181390
commit 4fa8d18621

View File

@ -1,6 +1,9 @@
import os import os
import re import re
import subprocess import subprocess
from sys import getfilesystemencoding as sys_getfilesystemencoding
from pathlib import Path from pathlib import Path
from urllib.parse import urljoin, unquote as urlunquote from urllib.parse import urljoin, unquote as urlunquote
from urllib.request import urlopen from urllib.request import urlopen
@ -125,7 +128,12 @@ def expowebpage(request, expowebpath, path):
# Should not get here if the path has suffix "_edit" # Should not get here if the path has suffix "_edit"
print(f' - 404 error in expowebpage() {path}') print(f' - 404 error in expowebpage() {path}')
return render(request, 'pagenotfound.html', {'path': path}, status="404") return render(request, 'pagenotfound.html', {'path': path}, status="404")
# print(f' - {sys_getfilesystemencoding()=}')
if (sys_getfilesystemencoding() != "utf-8"):
return HttpResponse(default_head + '<h3>UTF-8 Parsing Failure:<br>Default file encoding on this Troggle installation is not UTF-8:<br>failure detected in expowebpage in views.expo.py</h3> Please Please reconfigure Debian/Apache/Django to fix this, i.e. contact Wookey. </body' )
# This next bit can be drastically simplified now that we know that the system encoding actually is utf-8
try: try:
with open(expowebpath / path, "r", encoding='utf-8') as o: with open(expowebpath / path, "r", encoding='utf-8') as o:
html = o.read() html = o.read()
@ -220,9 +228,13 @@ def expopage(request, path):
# do not redirect to a file path without the slash as we may get in a loop. Let the user fix it: # do not redirect to a file path without the slash as we may get in a loop. Let the user fix it:
return render(request, 'dirnotfound.html', {'path': path, 'subpath': path[0:-1]}) return render(request, 'dirnotfound.html', {'path': path, 'subpath': path[0:-1]})
# So it must be a file in /expoweb/ but not .htm or .html probably an image # So it must be a file in /expoweb/ but not .htm or .html probably an image, maybe a txt file
filetobeopened = expowebpath / path filetobeopened = expowebpath / path
# print(f' - {sys_getfilesystemencoding()=}')
if (sys_getfilesystemencoding() != "utf-8"):
return HttpResponse(default_head + '<h3>UTF-8 Parsing Failure:<br>Default file encoding on this Troggle installation is not UTF-8:<br>failure detected in expowebpage in views.expo.py</h3> Please Please reconfigure Debian/Apache/Django to fix this, i.e. contact Wookey. </body' )
try: try:
content = open(filetobeopened, "rb") content = open(filetobeopened, "rb")
content_type=getmimetype(path) content_type=getmimetype(path)
@ -278,6 +290,10 @@ def editexpopage(request, path):
except Cave.DoesNotExist: except Cave.DoesNotExist:
pass pass
print(f' - {sys_getfilesystemencoding()=}')
if (sys_getfilesystemencoding() != "utf-8"):
return HttpResponse(default_head + '<h3>UTF-8 Parsing Failure:<br>Default file encoding on this Troggle installation is not UTF-8:<br>failure detected in expowebpage in views.expo.py</h3> Please Please reconfigure Debian/Apache/Django to fix this, i.e. contact Wookey. </body' )
try: try:
filepath = Path(settings.EXPOWEB) / path filepath = Path(settings.EXPOWEB) / path
o = open(filepath, "r", encoding="utf8") o = open(filepath, "r", encoding="utf8")
@ -361,7 +377,7 @@ def write_and_commit(filepath, content):
cwd = filepath.parent cwd = filepath.parent
filename = filepath.name filename = filepath.name
git = settings.GIT git = settings.GIT
# GIT see also core/models/cave.py writetrogglefile() # GIT see also core/models/caves.py writetrogglefile()
# GIT see also core/views/uploads.py dwgupload() # GIT see also core/views/uploads.py dwgupload()
try: try: