Drawings uploads git works

This commit is contained in:
Philip Sargent
2022-03-05 22:16:03 +00:00
parent a3a65524b8
commit 7a58aac08e
4 changed files with 54 additions and 21 deletions

View File

@@ -32,25 +32,30 @@ 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
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
'''
try:
dwgfile = DrawingFile.objects.get(dwgpath=urlunquote(path))
except:
message = f'Drawing file error or not found \'{path}\' .'
return render(request, 'errors/generic.html', {'message': message})
tfile = Path(settings.DRAWINGS_DATA, dwgfile.dwgpath)
try: # for display not download
return HttpResponse(content=open(tfile, errors='strict'), content_type="text/xhtml")
except UnicodeDecodeError:
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")
if Path(dwgfile.dwgpath).suffix in ['.xml', 'th2', '.th']:
try: # for display not download
return HttpResponse(content=open(tfile, errors='strict'), content_type="text/xhtml")
except UnicodeDecodeError:
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=open(tfile, errors='ignore'), content_type="text/xhtml")
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="Unable to understand the encoding for this file: not UTF-8 nor iso-8859-1, or some other read error happened.")
return HttpResponse(content=open(tfile,"rb"), content_type=getmimetype(tfile))

View File

@@ -114,10 +114,19 @@ def dwgupload(request, folder=None, gitdisable='no'):
def dwgvalid(name):
if name in [ '.gitignore', ]:
return False
if Path(name).suffix.lower() in ['.xml', '.th', '.th2', '', '.svg', '.jpg', '.pdf', '.jpeg', '.txt']:
if Path(name).suffix.lower() in ['.xml', '.th', '.th2', '', '.svg', '.txt']:
return True # dangerous, we should check the actual file binary signature
return False
def dwgvaliddisp(name):
'''OK to display, even if we are not going to allow a new one to be uploaded
'''
if name in [ '.gitignore', ]:
return False
if Path(name).suffix.lower() in ['.xml', '.th', '.th2', '', '.svg', '.txt', '.jpg', '.jpeg', '.png', '.pdf']:
return True # dangerous, we should check the actual file binary signature
return False
filesaved = False
actual_saved = []
refused = []
@@ -158,7 +167,11 @@ def dwgupload(request, folder=None, gitdisable='no'):
saved_filename = fs.save(f.name, content=f)
actual_saved.append(saved_filename)
if gitdisable != 'yes':
subprocess.call([git, "add", saved_filename], cwd=dirpath)
dr_add = subprocess.run([git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True)
if dr_add.returncode != 0:
msgdata = 'Ask a nerd to fix this.\n\n' + dr_add.stderr + '\n\n' + dr_add.stdout + '\n\nreturn code: ' + str(dr_add.returncode)
message = f'CANNOT git on server for this file {saved_filename}. Edits saved but not added to git.\n\n' + msgdata
return render(request,'errors/generic.html', {'message': message})
dwgfile, created = DrawingFile.objects.get_or_create(dwgpath=saved_filename, dwgname=Path(f.name).stem, filesize=f.size)
dwgfile.save()
else:
@@ -166,9 +179,17 @@ def dwgupload(request, folder=None, gitdisable='no'):
print(f'REFUSED {f.name}')
if actual_saved: # maybe all were refused by the suffix test in dwgvalid()
filesaved = True
if len(actual_saved) > 1:
dots = "..."
else:
dots = ""
if gitdisable != 'yes':
subprocess.call([git, "commit", "-m", f'Drawings upload - {list(multiple)}'], cwd=dirpath)
dr_commit = subprocess.run([git, "commit", "-m", f'Drawings upload - {actual_saved[0]}{dots}'], cwd=dirpath, capture_output=True, text=True)
# This produces return code = 1 if it commits OK
if dr_commit.returncode != 0:
msgdata = 'Ask a nerd to fix this.\n\n' + dr_commit.stderr + '\n\n' + dr_commit.stdout + '\n\nreturn code: ' + str(dr_commit.returncode)
message = f'Error code with git on server for this {actual_saved[0]}{dots}. Edits saved, added to git, but NOT committed.\n\n' + msgdata
return render(request,'errors/generic.html', {'message': message})
files = []
dirs = []
@@ -180,7 +201,7 @@ def dwgupload(request, folder=None, gitdisable='no'):
dirs.append(f.name)
continue
if f.is_file():
if dwgvalid(f.name):
if dwgvaliddisp(f.name):
files.append(f.name)
continue
except FileNotFoundError: