mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-19 11:07:28 +00:00
fixed serving expofiles from test server
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
from urllib.parse import urljoin, unquote as urlunquote
|
||||
from urllib.request import urlopen
|
||||
|
||||
from django.shortcuts import render, redirect
|
||||
from django.http import HttpResponse, HttpResponseRedirect, Http404
|
||||
@@ -19,16 +21,44 @@ import troggle.settings as settings
|
||||
def expofiles_redirect(request, path):
|
||||
'''This is used only when running as a test system without a local copy of /expofiles/
|
||||
'''
|
||||
return redirect('http://expo.survex.com/expofiles/' + path)
|
||||
return redirect(urljoin('http://expo.survex.com/expofiles/', path))
|
||||
|
||||
def expofilessingle(request, filepath):
|
||||
'''sends a single binary file to the user,
|
||||
'''
|
||||
fn=urlunquote(filepath)
|
||||
fn = Path(settings.EXPOFILES,filepath)
|
||||
if fn.is_dir():
|
||||
return expofilesdir(request, Path(fn), Path(filepath))
|
||||
print(" - expofilessingle {}:{}:{}:".format(filepath, fn, getmimetype(fn)))
|
||||
return HttpResponse(content=open(fn, "rb"),content_type=getmimetype(filepath)) # any file
|
||||
|
||||
def expofilesdir(request, dirpath, filepath):
|
||||
'''does a directory display. If there is an index.html file we should display that.
|
||||
- dirpath is a Path() and it does not have /expofiles/ in it
|
||||
'''
|
||||
print(" - expofilesdir {}".format(dirpath))
|
||||
urlpath = 'expofiles' / Path(filepath)
|
||||
fileitems = []
|
||||
diritems = []
|
||||
for f in dirpath.iterdir():
|
||||
if f.is_dir():
|
||||
diritems.append((urlpath / f.parts[-1], str(f.parts[-1])))
|
||||
else:
|
||||
# if f.parts[-1].lower() == 'index.htm' or f.parts[-1].lower() == 'index.html': # css cwd problem
|
||||
# return HttpResponse(content=open(f, "rb"),content_type=getmimetype(filepath)) # any file
|
||||
# return expofilessingle(request, str(Path(filepath / f.parts[-1])))
|
||||
fileitems.append((Path(urlpath) / f.parts[-1], str(f.parts[-1]), getmimetype(f)))
|
||||
return render(request, 'dirdisplay.html', { 'filepath': urlpath, 'fileitems':fileitems, 'diritems': diritems,'settings': settings })
|
||||
|
||||
def flatpage(request, path):
|
||||
#print(" - FLATPAGES delivering the file: {} as MIME type: {}".format(path,getmimetype(path)))
|
||||
# try:
|
||||
# r = Redirect.objects.get(originalURL = path)
|
||||
# #print(" - FLATPAGES REDIRECT the file: {} as: {}".format(path,r))
|
||||
# return HttpResponseRedirect(r.newURL) # Redirect after POST
|
||||
# except Redirect.DoesNotExist:
|
||||
# pass
|
||||
'''Either renders an HTML page from expoweb with all the menus,
|
||||
or serves an unadorned binary file with mime type'''
|
||||
print(" - FLATPAGES delivering the file: {} as MIME type: {}".format(path,getmimetype(path)),flush=True)
|
||||
|
||||
if path.startswith("noinfo") and settings.PUBLIC_SITE and not request.user.is_authenticated():
|
||||
print((" - FLATPAGES redirect to logon: flat path noinfo", path))
|
||||
return HttpResponseRedirect(urljoin(reverse("auth_login"),'?next={}'.format(request.path)))
|
||||
|
||||
try:
|
||||
r = Cave.objects.get(url = path)
|
||||
@@ -37,6 +67,7 @@ def flatpage(request, path):
|
||||
pass
|
||||
except:
|
||||
#print(" ! FAILED to get only one cave per slug for: "+path)
|
||||
# we should do this proper;ly, not this hack that returns the first cave that matches
|
||||
caves = Cave.objects.all().filter(url = path)
|
||||
for c in caves:
|
||||
print(path, c.slug())
|
||||
@@ -44,20 +75,9 @@ def flatpage(request, path):
|
||||
return troggle.core.views_caves.caveSlug(request, c.slug())
|
||||
pass
|
||||
|
||||
# try:
|
||||
# r = EntranceRedirect.objects.get(originalURL = path)
|
||||
# return troggle.core.views_caves.entranceSlug(request, r.entrance.slug())
|
||||
# except EntranceRedirect.DoesNotExist:
|
||||
# pass
|
||||
|
||||
|
||||
if path.startswith("noinfo") and settings.PUBLIC_SITE and not request.user.is_authenticated():
|
||||
print(("flat path noinfo", path))
|
||||
return HttpResponseRedirect(reverse("auth_login") + '?next=%s' % request.path)
|
||||
|
||||
expowebpath = Path(settings.EXPOWEB)
|
||||
if path.endswith("/") or path == "":
|
||||
#print(" - FLATPAGES the file: {} ENDSWITH ...".format(path))
|
||||
print(" - FLATPAGES the file: {} ENDSWITH ...".format(path))
|
||||
|
||||
try:
|
||||
o = open(os.path.normpath(expowebpath / path / "index.html"), "rb")
|
||||
@@ -69,30 +89,34 @@ def flatpage(request, path):
|
||||
except IOError:
|
||||
return render(request, 'pagenotfound.html', {'path': path})
|
||||
else:
|
||||
print(" - FLATPAGES the file: '{}' ...".format(path))
|
||||
if path.startswith('site_media'):
|
||||
print(" - MEDIA_ROOT: {} ...{}".format(settings.MEDIA_ROOT, path))
|
||||
path = path.replace("site_media", settings.MEDIA_ROOT)
|
||||
filetobeopened = os.path.normpath(path)
|
||||
elif path.startswith("static"):
|
||||
print(" - STATIC_ROOT: {} ...{}".format(settings.MEDIA_ROOT, path))
|
||||
path = path.replace("static", settings.MEDIA_ROOT)
|
||||
filetobeopened = os.path.normpath(path)
|
||||
else:
|
||||
print(" - NO _ROOT: {} ...".format(expowebpath))
|
||||
filetobeopened = os.path.normpath(expowebpath / path)
|
||||
|
||||
print(" - FLATPAGES full path : {} ...".format(filetobeopened))
|
||||
try:
|
||||
#print(" - FLATPAGES the file: '{}' ...".format(path))
|
||||
#print(" - FLATPAGES MEDIA_ROOT: '{}' ...".format(settings.MEDIA_ROOT))
|
||||
if path.startswith('site_media'):
|
||||
#print(" - MEDIA_ROOT: {} ...".format(settings.MEDIA_ROOT))
|
||||
path = path.replace("site_media", settings.MEDIA_ROOT)
|
||||
filetobeopened = os.path.normpath(path)
|
||||
elif path.startswith("static"):
|
||||
#print(" - STATIC_ROOT: {} ...".format(settings.MEDIA_ROOT))
|
||||
path = path.replace("static", settings.MEDIA_ROOT)
|
||||
filetobeopened = os.path.normpath(path)
|
||||
else:
|
||||
#print(" - NO _ROOT: {} ...".format(expowebpath))
|
||||
filetobeopened = os.path.normpath(expowebpath / path)
|
||||
|
||||
#print(" - FLATPAGES full path : {} ...".format(filetobeopened))
|
||||
o = open(filetobeopened, "rb")
|
||||
#print(" - FLATPAGES full path no error: {} ...".format(filetobeopened))
|
||||
print(" - FLATPAGES full path no error: {} ...".format(filetobeopened))
|
||||
except IOError:
|
||||
#print(" - FLATPAGES ERROR: {} ...".format(filetobeopened))
|
||||
print(" - FLATPAGES ERROR: {} ...".format(filetobeopened))
|
||||
#o.close()
|
||||
return render(request, 'pagenotfound.html', {'path': path})
|
||||
|
||||
|
||||
if path.endswith(".htm") or path.endswith(".html"):
|
||||
html = o.read()
|
||||
|
||||
# add the menus etc.
|
||||
with open(os.path.normpath(expowebpath / path), "rb") as o:
|
||||
html = o.read()
|
||||
|
||||
m = re.search(rb'(.*)<\s*head([^>]*)>(.*)<\s*/head\s*>(.*)<\s*body([^>]*)>(.*)<\s*/body\s*>(.*)', html, re.DOTALL + re.IGNORECASE)
|
||||
if m:
|
||||
preheader, headerattrs, head, postheader, bodyattrs, body, postbody = m.groups()
|
||||
@@ -123,21 +147,24 @@ def flatpage(request, path):
|
||||
return render(request, 'flatpage.html', {'editable': editable, 'path': path, 'title': title,
|
||||
'body': body, 'homepage': (path == "index.htm"), 'has_menu': has_menu})
|
||||
else:
|
||||
#print(" - FLATPAGES delivering the file: {} as MIME type: {}".format(path,getmimetype(path)))
|
||||
return HttpResponse(o.read(), content_type=getmimetype(path))
|
||||
print(" - FLATPAGES delivering the file: {} as MIME type: {}".format(path,getmimetype(path)))
|
||||
return HttpResponse(content=open(filetobeopened, "rb"), content_type=getmimetype(path))
|
||||
#return HttpResponse(content=open(singlescan.ffile,"rb"), content_type=getmimetype(path))
|
||||
|
||||
def getmimetype(path):
|
||||
path = str(path)
|
||||
if path.lower().endswith(".css"): return "text/css"
|
||||
if path.lower().endswith(".txt"): return "text/css"
|
||||
if path.lower().endswith(".js"): return "application/javascript"
|
||||
if path.lower().endswith(".json"): return "application/javascript"
|
||||
if path.lower().endswith(".ico"): return "image/vnd.microsoft.icon"
|
||||
if path.lower().endswith(".png"): return "image/png"
|
||||
if path.lower().endswith(".tif"): return "image/tif"
|
||||
if path.lower().endswith(".gif"): return "image/gif"
|
||||
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 "image/xml"
|
||||
if path.lower().endswith("xml"): return "application/xml" # we use "text/xhtml" 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"
|
||||
@@ -146,6 +173,11 @@ def getmimetype(path):
|
||||
if path.lower().endswith(".err"): return "application/x-survex-err"
|
||||
if path.lower().endswith(".odt"): return "application/vnd.oasis.opendocument.text"
|
||||
if path.lower().endswith(".ods"): return "application/vnd.oasis.opendocument.spreadsheet"
|
||||
if path.lower().endswith(".docx"): return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
if path.lower().endswith(".xslx"): return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
if path.lower().endswith(".gz"): return "application/x-7z-compressed"
|
||||
if path.lower().endswith(".7z"): return "application/x-7z-compressed"
|
||||
if path.lower().endswith(".zip"): return "application/zip"
|
||||
return ""
|
||||
|
||||
@login_required_if_public
|
||||
|
||||
Reference in New Issue
Block a user