From da09bc7968f0c57029f6044484d511244eb00812 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sat, 15 Oct 2022 14:07:15 +0300 Subject: [PATCH] Render tunnel files as XML in webpage, not just text --- core/views/drawings.py | 64 ++++++++++++++++++++++++++++++------------ core/views/expo.py | 2 +- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/core/views/drawings.py b/core/views/drawings.py index 93a5573..e922433 100644 --- a/core/views/drawings.py +++ b/core/views/drawings.py @@ -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: + #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: + print(f'! Exception when reading {encoding}') + continue + + print(f'! None of those encodings worked for {tfile}') try: - return HttpResponse(content=open(tfile,encoding='iso-8859-1'), content_type="text/xhtml") - 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)) + 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.") + diff --git a/core/views/expo.py b/core/views/expo.py index 4dca1a1..021415f 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -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"