2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-22 07:11:52 +00:00

Render tunnel files as XML in webpage, not just text

This commit is contained in:
Philip Sargent 2022-10-15 14:07:15 +03:00
parent e0ac09d5ec
commit da09bc7968
2 changed files with 47 additions and 19 deletions

View File

@ -31,11 +31,10 @@ def dwgallfiles(request):
def dwgfilesingle(request, path):
'''sends a single binary file to the user, We should have a renderer that syntax-colours this Tunnel xml
but it might be a Therion file. And it could be an old PNG, PDF or SVG for that matter,
so we should attempt to render it.
'''sends a single binary file to the user. It could be an old PNG, PDF or SVG
not just Tunnel or Therion
The db records created on datbase reset import are not use when we look for an individual drawing, only
The db records created on datbase reset import are not used when we look for an individual drawing, only
collections of them.
Note the infelicity that this will deliver files that exist, but are hidden on the previous
@ -44,22 +43,51 @@ def dwgfilesingle(request, path):
'''
tfile = Path(settings.DRAWINGS_DATA, path.replace(":","#"))
if not tfile.is_file():
message = f'Drawing file not found in filesystem at \'{path}\' .'
message = f'Drawing file not found in filesystem at \'{path}\' \n\t\tMaybe a new dataimport needs to be done to get up to date.'
return render(request, 'errors/generic.html', {'message': message})
if Path(tfile).suffix in ['.xml', 'th2', '.th']:
try: # for display not download
return HttpResponse(content=open(tfile, errors='strict'), content_type="text/xhtml")
except UnicodeDecodeError:
if Path(tfile).suffix in ['.xml']: # tunnel files are usually 'us-ascii' (!). And may not close all XML tags properly either.
for encoding in ['us-ascii', 'iso-8859-1', 'utf-8']:
try:
return HttpResponse(content=open(tfile,encoding='iso-8859-1'), content_type="text/xhtml")
#print(f'attempting {encoding} for {tfile}')
with open(tfile, encoding=encoding, errors='strict') as f:
print(f'- before reading any {encoding}')
lines = f.readlines()
#print(f'- finished reading {encoding}')
#print(f'- {len(lines)=}')
clean = []
for l in lines:
clean.append(l.replace('&','@').replace('&','&').replace('@', '&'))
#print(f'- Cleaned and stripped.')
try:
return HttpResponse(content=clean, content_type="text/xml")
except:
return HttpResponse(content=f"Render fail for this file: {tfile} Please report to a nerd. Probably Julian's fault.")
except:
return HttpResponse(content=open(tfile,mode='rb'), content_type="text/xhtml")
else:
return HttpResponse(content=open(tfile, errors='ignore'), content_type="text/xhtml")
else:
return HttpResponse(content="Unable to understand the encoding for this file: not UTF-8 nor iso-8859-1, or some other read error happened.")
else:
return HttpResponse(content=open(tfile,"rb"), content_type=getmimetype(tfile))
print(f'! Exception when reading {encoding}')
continue
print(f'! None of those encodings worked for {tfile}')
try:
return HttpResponse(content=open(tfile, errors='ignore'), content_type=getmimetype(tfile))
except:
return HttpResponse(content=f"Unable to understand the encoding for this file: {tfile} Please report to a nerd.")
if Path(tfile).suffix in ['th2', '.th']:
try:
return HttpResponse(content=open(tfile, errors='strict'), content_type="text/txt") # default utf-8
except:
return HttpResponse(content=f"Unable to understand the encoding for this file: {tfile} Please report to a nerd.")
else: # SVG, JPG etc
try:
return HttpResponse(content=open(tfile, mode='rb'), content_type=getmimetype(tfile)) # default utf-8
except:
try:
return HttpResponse(content=open(tfile, mode='rb'))
except:
return HttpResponse(content=f"Unable to understand the encoding '{getmimetype(tfile)}' for this file: {tfile} Note that Apache will do its own thing here. Please report to a nerd.")

View File

@ -279,7 +279,7 @@ def getmimetype(path):
if path.lower().endswith(".jpeg"): return "image/jpeg"
if path.lower().endswith(".jpg"): return "image/jpeg"
if path.lower().endswith("svg"): return "image/svg+xml"
if path.lower().endswith("xml"): return "application/xml" # we use "text/xhtml" for tunnel files
if path.lower().endswith("xml"): return "application/xml" # we use "text/xml" for tunnel files
if path.lower().endswith(".pdf"): return "application/pdf"
if path.lower().endswith(".ps"): return "application/postscript"
if path.lower().endswith(".svx"): return "application/x-survex-svx"