2011-07-11 02:10:22 +01:00
|
|
|
from django.conf import settings
|
2020-06-04 23:38:57 +01:00
|
|
|
#from . import fileAbstraction
|
2011-07-11 02:10:22 +01:00
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.http import HttpResponse, Http404
|
|
|
|
import os, stat
|
|
|
|
import re
|
2020-05-28 04:54:53 +01:00
|
|
|
from troggle.core.models_survex import SurvexScansFolder, SurvexScanSingle, SurvexBlock, TunnelFile
|
2011-07-11 02:10:22 +01:00
|
|
|
import parsers.surveys
|
2020-05-24 01:57:06 +01:00
|
|
|
import urllib.request, urllib.parse, urllib.error
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2020-06-04 23:38:57 +01:00
|
|
|
def fa_readFile(*path):
|
|
|
|
try:
|
|
|
|
f = open(os.path.join(settings.FILES, *path))
|
|
|
|
except:
|
2020-06-18 21:50:16 +01:00
|
|
|
f = urllib.request.urlopen(settings.FILES+"download/")
|
2020-06-04 23:38:57 +01:00
|
|
|
return f.read()
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def getMimeType(extension):
|
|
|
|
try:
|
|
|
|
return {"txt": "text/plain",
|
|
|
|
"html": "text/html",
|
|
|
|
}[extension]
|
|
|
|
except:
|
2019-02-24 19:50:45 +00:00
|
|
|
print("unknown file type")
|
2011-07-11 02:10:22 +01:00
|
|
|
return "text/plain"
|
|
|
|
|
|
|
|
def upload(request, path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def download(request, path):
|
|
|
|
#try:
|
|
|
|
|
2020-06-04 23:38:57 +01:00
|
|
|
return HttpResponse(fa_readFile(path), content_type=getMimeType(path.split(".")[-1]))
|
2011-07-11 02:10:22 +01:00
|
|
|
#except:
|
|
|
|
# raise Http404
|
|
|
|
|
|
|
|
def UniqueFile(fname):
|
|
|
|
while True:
|
|
|
|
if not os.path.exists(fname):
|
|
|
|
break
|
|
|
|
mname = re.match("(.*?)(?:-(\d+))?\.(png|jpg|jpeg)$(?i)", fname)
|
|
|
|
if mname:
|
|
|
|
fname = "%s-%d.%s" % (mname.group(1), int(mname.group(2) or "0") + 1, mname.group(3))
|
|
|
|
return fname
|
|
|
|
|
|
|
|
|
|
|
|
# join it all up and then split them off for the directories that don't exist
|
|
|
|
# anyway, this mkdir doesn't work
|
|
|
|
def SaveImageInDir(name, imgdir, project, fdata, bbinary):
|
2020-05-24 01:57:06 +01:00
|
|
|
print(("hihihihi", fdata, settings.SURVEYS))
|
2011-07-11 02:10:22 +01:00
|
|
|
fimgdir = os.path.join(settings.SURVEYS, imgdir)
|
|
|
|
if not os.path.isdir(fimgdir):
|
2020-05-24 01:57:06 +01:00
|
|
|
print("*** Making directory", fimgdir)
|
2011-07-11 02:10:22 +01:00
|
|
|
os.path.mkdir(fimgdir)
|
|
|
|
fprojdir = os.path.join(fimgdir, project)
|
|
|
|
if not os.path.isdir(fprojdir):
|
2020-05-24 01:57:06 +01:00
|
|
|
print("*** Making directory", fprojdir)
|
2011-07-11 02:10:22 +01:00
|
|
|
os.path.mkdir(fprojdir)
|
2020-05-24 01:57:06 +01:00
|
|
|
print("hhh")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
fname = os.path.join(fprojdir, name)
|
2020-05-24 01:57:06 +01:00
|
|
|
print(fname, "fff")
|
2011-07-11 02:10:22 +01:00
|
|
|
fname = UniqueFile(fname)
|
|
|
|
|
|
|
|
p2, p1 = os.path.split(fname)
|
|
|
|
p3, p2 = os.path.split(p2)
|
|
|
|
p4, p3 = os.path.split(p3)
|
|
|
|
res = os.path.join(p3, p2, p1)
|
|
|
|
|
2020-05-24 01:57:06 +01:00
|
|
|
print("saving file", fname)
|
2011-07-11 02:10:22 +01:00
|
|
|
fout = open(fname, (bbinary and "wb" or "w"))
|
|
|
|
fout.write(fdata.read())
|
|
|
|
fout.close()
|
|
|
|
res = os.path.join(imgdir, name)
|
|
|
|
return res.replace("\\", "/")
|
|
|
|
|
|
|
|
def surveyscansfolder(request, path):
|
|
|
|
#print [ s.walletname for s in SurvexScansFolder.objects.all() ]
|
2020-05-24 01:57:06 +01:00
|
|
|
survexscansfolder = SurvexScansFolder.objects.get(walletname=urllib.parse.unquote(path))
|
2011-07-11 02:10:22 +01:00
|
|
|
return render_to_response('survexscansfolder.html', { 'survexscansfolder':survexscansfolder, 'settings': settings })
|
|
|
|
|
|
|
|
def surveyscansingle(request, path, file):
|
2020-05-24 01:57:06 +01:00
|
|
|
survexscansfolder = SurvexScansFolder.objects.get(walletname=urllib.parse.unquote(path))
|
2011-07-11 02:10:22 +01:00
|
|
|
survexscansingle = SurvexScanSingle.objects.get(survexscansfolder=survexscansfolder, name=file)
|
2020-05-31 19:00:44 +01:00
|
|
|
return HttpResponse(content=open(survexscansingle.ffile,"rb"), content_type=getMimeType(path.split(".")[-1]))
|
2011-07-11 02:10:22 +01:00
|
|
|
#return render_to_response('survexscansfolder.html', { 'survexscansfolder':survexscansfolder, 'settings': settings })
|
2020-06-18 21:50:16 +01:00
|
|
|
|
|
|
|
def expofilessingle(request, filepath):
|
2020-06-20 15:43:28 +01:00
|
|
|
# defaults to content_type="text/pain" needs fixing for PDFs
|
2020-06-18 21:50:16 +01:00
|
|
|
fn=urllib.parse.unquote(filepath)
|
|
|
|
return HttpResponse(content=open(settings.EXPOFILES+fn,"rb"))
|
|
|
|
|
|
|
|
def cssfilessingle(request, filepath):
|
|
|
|
fn=urllib.parse.unquote(filepath)
|
|
|
|
return HttpResponse(content=open(settings.MEDIA_ROOT+fn,"rb"),content_type="text/css")
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def surveyscansfolders(request):
|
|
|
|
survexscansfolders = SurvexScansFolder.objects.all()
|
|
|
|
return render_to_response('survexscansfolders.html', { 'survexscansfolders':survexscansfolders, 'settings': settings })
|
|
|
|
|
|
|
|
|
|
|
|
def tunneldata(request):
|
|
|
|
tunnelfiles = TunnelFile.objects.all()
|
|
|
|
return render_to_response('tunnelfiles.html', { 'tunnelfiles':tunnelfiles, 'settings': settings })
|
|
|
|
|
|
|
|
|
|
|
|
def tunnelfile(request, path):
|
2020-05-24 01:57:06 +01:00
|
|
|
tunnelfile = TunnelFile.objects.get(tunnelpath=urllib.parse.unquote(path))
|
2011-07-11 02:10:22 +01:00
|
|
|
tfile = os.path.join(settings.TUNNEL_DATA, tunnelfile.tunnelpath)
|
2019-02-24 16:46:02 +00:00
|
|
|
return HttpResponse(content=open(tfile), content_type="text/plain")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
def tunnelfileupload(request, path):
|
2020-05-24 01:57:06 +01:00
|
|
|
tunnelfile = TunnelFile.objects.get(tunnelpath=urllib.parse.unquote(path))
|
2011-07-11 02:10:22 +01:00
|
|
|
tfile = os.path.join(settings.TUNNEL_DATA, tunnelfile.tunnelpath)
|
|
|
|
|
|
|
|
project, user, password, tunnelversion = request.POST["tunnelproject"], request.POST["tunneluser"], request.POST["tunnelpassword"], request.POST["tunnelversion"]
|
2020-05-24 01:57:06 +01:00
|
|
|
print((project, user, tunnelversion))
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
|
2020-05-24 01:57:06 +01:00
|
|
|
assert len(list(request.FILES.values())) == 1, "only one file to upload"
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2020-05-24 01:57:06 +01:00
|
|
|
uploadedfile = list(request.FILES.values())[0]
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
if uploadedfile.field_name != "sketch":
|
2019-02-24 16:46:02 +00:00
|
|
|
return HttpResponse(content="Error: non-sketch file uploaded", content_type="text/plain")
|
2011-07-11 02:10:22 +01:00
|
|
|
if uploadedfile.content_type != "text/plain":
|
2019-02-24 16:46:02 +00:00
|
|
|
return HttpResponse(content="Error: non-plain content type", content_type="text/plain")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
# could use this to add new files
|
|
|
|
if os.path.split(path)[1] != uploadedfile.name:
|
2019-02-24 16:46:02 +00:00
|
|
|
return HttpResponse(content="Error: name disagrees", content_type="text/plain")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
orgsize = tunnelfile.filesize # = os.stat(tfile)[stat.ST_SIZE]
|
|
|
|
|
|
|
|
ttext = uploadedfile.read()
|
|
|
|
|
|
|
|
# could check that the user and projects agree here
|
|
|
|
|
|
|
|
fout = open(tfile, "w")
|
|
|
|
fout.write(ttext)
|
|
|
|
fout.close()
|
|
|
|
|
|
|
|
# redo its settings of
|
|
|
|
parsers.surveys.SetTunnelfileInfo(tunnelfile)
|
|
|
|
tunnelfile.save()
|
|
|
|
|
|
|
|
uploadedfile.close()
|
|
|
|
message = "File size %d overwritten with size %d" % (orgsize, tunnelfile.filesize)
|
2019-02-24 16:46:02 +00:00
|
|
|
return HttpResponse(content=message, content_type="text/plain")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|