From 5d4ad93c5113a7895e98556f741714e2652f05b9 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sat, 10 Apr 2021 15:30:29 +0100 Subject: Better FileNotFound in expofiles --- core/views/expo.py | 60 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 19 deletions(-) (limited to 'core/views/expo.py') diff --git a/core/views/expo.py b/core/views/expo.py index d74120f..0a95011 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -50,27 +50,40 @@ default_head = ''' def expofiles_redirect(request, path): '''This is used only when running as a test system without a local copy of /expofiles/ + when settings.EXPOFILESREMOTE is True ''' return redirect(urljoin('http://expo.survex.com/expofiles/', path)) def expofilessingle(request, filepath): - '''sends a single binary file to the user, + '''sends a single binary file to the user, if not found, show the parent directory + If the path actually is a directory, then show that. ''' 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 + if fn.is_file(): + return HttpResponse(content=open(fn, "rb"),content_type=getmimetype(filepath)) # any file + else: + # not a file, so show parent directory + return expofilesdir(request, Path(fn).parent, Path(filepath).parent) 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 + - dirpath is a full Path() resolved including lcoal machine /expofiles/ + - filepath is a Path() and it does not have /expofiles/ in it ''' - # print(" - expofilesdir {}".format(dirpath)) + # print(f' - expofilesdir {dirpath}') urlpath = 'expofiles' / Path(filepath) + try: + for f in dirpath.iterdir(): + pass + except FileNotFoundError: + print(f' - expofilesdir {dirpath}') + return expofilesdir(request, dirpath.parent, filepath.parent) + fileitems = [] - diritems = [] + diritems = [] for f in dirpath.iterdir(): if f.is_dir(): diritems.append((urlpath / f.parts[-1], str(f.parts[-1]))) @@ -113,7 +126,7 @@ def expowebpage(request, expowebpath, path): menumatch = re.match(r'(.*)