def examinezip(request, tarpath, fpath=None, document_root=None, show_indexes=False): if fpath: # Got a valid fpath (inside the zipfile) zippath = document_root + zippath + "/" + fpath else: # No valid fpath (zipfile itself) zippath = document_root + zippath if fpath == None: try: t = loader.get_template( '/u/savagev/valtest/realtest/templates/data/static/directory_index.html' ) except TemplateDoesNotExist: t = loader.get_template( '/u/kamwoods/Sudoc/svp/realtest/templates/data/static/directory_index.html' ) zip = zipfile.open(tarpath, 'r') files = [] for zipinfo in zip: f = zippath.split('/')[-1] + '/' + zipinfo.filename filefp = os.path.join(zippath, f) mimetype = mimefinder.find_type(filefp) fmodtime = zipinfo.date_time if not tarinfo.file_size == 0: fgsize = static.greek(zipinfo.file_size) else: fgsize = "-" files.append([ "-", "", mimetype, f, fmodtime, fgsize, ]) zip.close() c = Context({ 'directory': zippath + '/', 'file_list': files, 'hinst': request.session['hinst'], 'hexe': request.session['hexe'], 'hdriver': request.session['hdriver'], 'hlib': request.session['hlib'], 'hetc': request.session['hetc'], 'user': request.user.username, }) return HttpResponse(t.render(c))
def examinezip(request, tarpath, fpath=None, document_root=None, show_indexes=False): if fpath: # Got a valid fpath (inside the zipfile) zippath = document_root + zippath + "/" + fpath else: # No valid fpath (zipfile itself) zippath = document_root + zippath if fpath == None: try: t = loader.get_template('/u/savagev/valtest/realtest/templates/data/static/directory_index.html') except TemplateDoesNotExist: t = loader.get_template('/u/kamwoods/Sudoc/svp/realtest/templates/data/static/directory_index.html') zip = zipfile.open(tarpath, 'r') files = [] for zipinfo in zip: f = zippath.split('/')[-1] +'/'+ zipinfo.filename filefp = os.path.join(zippath, f) mimetype = mimefinder.find_type(filefp) fmodtime = zipinfo.date_time if not tarinfo.file_size == 0: fgsize = static.greek(zipinfo.file_size) else: fgsize = "-" files.append(["-", "", mimetype, f, fmodtime, fgsize,]) zip.close() c = Context({ 'directory' : zippath+'/', 'file_list' : files, 'hinst': request.session['hinst'], 'hexe': request.session['hexe'], 'hdriver': request.session['hdriver'], 'hlib': request.session['hlib'], 'hetc': request.session['hetc'], 'user': request.user.username, }) return HttpResponse(t.render(c))
def directory_index(path, fullpath, hinst, hexe, hlib, hetc, hdriver, ip, sorter, user): """ This function is the veritable life-blood of the project. Silently, it sneaks through the directory structure, furtively collecting names of files (as well as their most personal information, like size and last date accessed). If the files are among the chosen with special marks upon their paths (.gif, .doc, .xls, and the like), it thrusts them upon various converting modules to change their very being and display the copies alongside the originals. Can you tell the difference? And it doesn't stop there. If asked, this function can reorder its finds based on various qualifications, or even hide them from the user by relegating them to a separate list from the regular files. """ try: t = loader.get_template( '/u/savagev/valtest/realtest/templates/data/static/directory_index.html' ) except TemplateDoesNotExist: t = loader.get_template( '/u/kamwoods/Sudoc/svp/realtest/templates/data/static/directory_index.html' ) files = [] mfiles = [] x = [] convs = [] vidconvs = [] # Fix this - it only needs to happen once for clist in Conversion.objects.filter(): lst = clist.fromlist.split(', ') lst.append(clist.toextension) lst.append(clist. async) if clist.toextension == 'flv': vidconvs.append(lst) convs.append(lst) # Set up the stuff for runconvert curdir = fullpath.split("/")[-1] brcd = (fullpath.split("/")[3]).split("_")[1] # only call this function if the user is browsing the top-level directory if len(curdir) > 6 and brcd == curdir[6:]: # Will fork off and do appropriate work runconvert.runconv(fullpath, convs) flvconvert.runconv(fullpath, vidconvs) #this needs to think about FLVRUNCONVERT somehow #pass # Test to see if output directory exists. Only create if it does not # (i.e. on first pass into the directory) if os.path.isdir("/tmp/migrated/" + brcd): pass elif os.path.isfile("/tmp/migrated/" + brcd): raise OSError("a file with that name already exists") else: os.mkdir("/tmp/migrated/" + brcd) for f in os.listdir(fullpath): if not f.startswith('.'): filefp = os.path.join(fullpath, f) mimetype = mimefinder.find_type(filefp) migrated = hasher.md5sum(filefp, convs) #this is for the actual conversions that happen at the bottom mig = migrated migratedext = "" if not migrated == '-': try: migratedext = "/data/static/" + migrated.split( '.')[-1] + ".gif" except: migratedext = "/data/static/unknown.gif" fmodtime = conv_date( time.asctime(time.localtime(os.stat(filefp).st_mtime))) if not os.path.isdir(filefp): fgsize = greek(os.stat(filefp).st_size) else: fgsize = "-" ''' The logic of this loop needs to be fixed. It is intended to determine whether there are masked files in the directory and accordingly put them into the mfiles (masked files) list if necessary. However, Kam wanted to get rid of the extraneous appendings to the two file lists. Right now... not so much. ''' try: i = MaskFile.objects.filter(full_path__exact=filefp)[0] if (hinst and i.mask_flag) and not i.restrict_flag: if os.path.isdir(filefp): mfiles.append([filefp, fmodtime, fgsize]) else: mfiles.append([i.file_name, fmodtime, fgsize]) else: if len(migrated) > 2: migrated = "/tmp/migrated/" + brcd + "/" + migrated else: migrated = "-" files.append([ migrated, migratedext, mimetype, f, fmodtime, fgsize, ]) except: #this should be fixed sometime (-1 test, hahah) if len(migrated) > 2: migrated = "/tmp/migrated/" + brcd + "/" + migrated else: migrated = '-' if (hexe and mimetype == "binary.gif") or (hlib and mimetype == "binary.gif"): mfiles.append([f, fmodtime, fgsize]) else: files.append([ migrated, migratedext, mimetype, f, fmodtime, fgsize, ]) # Don't forget to check for whether we SHOULD be migrating these files # on the fly for l in convs: if filefp[-3:].lower() in l: if l[-1] == False: if not os.path.isfile(migrated): if not migrated == '-': # First test for dbf files if filefp[-3:].lower() == "dbf": out_fp = open( '/tmp/migrated/' + brcd + '/' + mig, 'w') dbfmod.make_list_lim( dbfmod.DBFFile(filefp), out=out_fp) out_fp.close() elif filefp[-3:].lower() == "cdf": #uses skeletontable utility. Kam says it's a hack #os.popen2('/u/savagev/cdf31-dist/bin/skeletontable -skeleton /tmp/migrated/' + brcd + '/' + mig[:-4] + ' ' + filefp) os.popen2( '/u/kamwoods/cdf31-dist/bin/skeletontable -skeleton /tmp/migrated/' + brcd + '/' + mig[:-4] + ' ' + filefp) elif filefp[-3:].lower() == "xls": #uses xlrd to convert it xlsmod.make_html(filefp, migrated) elif filefp[-3:].lower() == "mdb": mdbmod.make_html(filefp, migrated) elif filefp[-3:-1].lower( ) == 'wk' or filefp[-3:] == '123': lotusmod.make_html(filefp, migrated) else: # Otherwise this is an image conversion imgmod.make_jpg( filefp, '/tmp/migrated/' + brcd + '/' + mig) #os.popen2('convert ' + filefp + ' /tmp/migrated/'+brcd+'/'+mig) # Add remaining conversions later # Don't forget excel (python xlrd - google it) - copy into p2.4 site-packages name = mod = size = 'a' # Ignore if no default sort has been passed in - THIS IS CRUFTY if not sorter == '': #parse out the bit of the query string that details what the objects are to be sorted by pieces = sorter.split(";") sorter = pieces[0].split("=")[-1] order = pieces[1].split("=")[-1] # sort by size if sorter == 's': sortind = 5 if order == 'a': size = 'd' # sort by mod-time elif sorter == 'm': sortind = 4 if order == 'a': mod = 'd' # sort by name else: sortind = 3 if order == 'a': name = 'd' sys.stderr.write(str(files[1][4])) files = sort_by_index(files, sortind) if order == 'd': files.reverse() c = Context({ 'brcd': brcd, 'directory': path + '/', 'file_list': files, 'hinst': hinst, 'hexe': hexe, 'hdriver': hdriver, 'hlib': hlib, 'hetc': hetc, 'ip': ip, 'mask_file_list': mfiles, 'mod': mod, 'name': name, 'size': size, 'user': user.username, }) return HttpResponse(t.render(c))
#this needs to think about FLVRUNCONVERT somehow #pass # Test to see if output directory exists. Only create if it does not # (i.e. on first pass into the directory) if os.path.isdir("/tmp/migrated/" + brcd): pass elif os.path.isfile("/tmp/migrated/" + brcd): raise OSError("a file with that name already exists") else: os.mkdir("/tmp/migrated/" + brcd) for f in os.listdir(fullpath): if not f.startswith('.'): filefp = os.path.join(fullpath, f) mimetype = mimefinder.find_type(filefp) migrated = hasher.md5sum(filefp, convs) #this is for the actual conversions that happen at the bottom mig = migrated migratedext = "" if not migrated == '-': try: migratedext = "/data/static/" + migrated.split('.')[-1] + ".gif" except: migratedext = "/data/static/unknown.gif" fmodtime = conv_date(time.asctime(time.localtime(os.stat(filefp).st_mtime))) if not os.path.isdir(filefp): fgsize = greek(os.stat(filefp).st_size) else: fgsize = "-"
def examinetar(request, tarpath, fpath=None, document_root=None, show_indexes=False): if fpath: # Got a valid fpath (inside the tarfile) tarpath = document_root + tarpath + "/" + fpath else: # No valid fpath (tarfile itself) tarpath = document_root + tarpath if fpath == None: try: t = loader.get_template('/u/savagev/valtest/realtest/templates/data/static/directory_index.html') except TemplateDoesNotExist: t = loader.get_template('/u/kamwoods/Sudoc/svp/realtest/templates/data/static/directory_index.html') tar = tarfile.open(tarpath, 'r') files = [] for tarinfo in tar: f = tarpath.split('/')[-1] +'/'+ tarinfo.name filefp = os.path.join(tarpath, f) mimetype = mimefinder.find_type(filefp) fmodtime = tarinfo.mtime if not tarinfo.isdir(): fgsize = static.greek(tarinfo.size) else: fgsize = "-" files.append(["-", "", mimetype, f, fmodtime, fgsize,]) tar.close() c = Context({ 'directory' : tarpath+'/', 'file_list' : files, 'hinst': request.session['hinst'], 'hexe': request.session['hexe'], 'hdriver': request.session['hdriver'], 'hlib': request.session['hlib'], 'hetc': request.session['hetc'], 'user': request.user.username, }) return HttpResponse(t.render(c)) else: # Inside the tarfile (set up the appropriate path) tarmainpath = "/".join([x for x in tarpath.split("/")[0:-1]]) tar = tarfile.open(tarmainpath, 'r') tarredfile = tarpath.split('/')[-1] extractpath = '/tmp/' + tarredfile thefile = tar.extract(tarredfile, "/tmp") content = open(extractpath, 'rb').read() mimetype = mimetypes.guess_type(extractpath)[0] return HttpResponse(content, mimetype=mimetype)