Created
November 28, 2010 14:23
-
-
Save wenbert/718969 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@login_required | |
def download(request,groupname,filename): | |
""" | |
Download a file | |
""" | |
custgroup = Group.objects.get(name=groupname) | |
if custgroup not in request.user.groups.all(): | |
return render_to_response('404.html') | |
else: | |
pass | |
path = settings.APPLICATION_STORAGE | |
filename = os.path.join(groupname,filename) | |
filepath = os.path.join(path,filename) | |
filename = filepath | |
wrapper = FileWrapper(file(filename)) | |
response = HttpResponse(wrapper, mimetype='application/force-download') | |
response['Content-Length'] = os.path.getsize(filename) | |
response['Content-Disposition'] = 'attachment; filename=%s' \ | |
% smart_str(filename) | |
return response | |
@login_required | |
def download_dir_as_zip(request,groupname): | |
""" | |
Download a directory or a file as zip. | |
""" | |
path = settings.APPLICATION_STORAGE | |
url_dir = request.GET.get('dir') | |
#check if group of request.user | |
custgroup = Group.objects.get(name=groupname) | |
if custgroup not in request.user.groups.all(): | |
return render_to_response('404.html') | |
else: | |
pass | |
#complete path of the directory with the groupname | |
grouppath = os.path.join(path, str(groupname)) | |
#if dir in the URL is set, append the dir from the GET to the grouppath | |
if url_dir: | |
grouppath = os.path.join(grouppath,str(url_dir)) | |
#some security checks so that users will not be able | |
#to "navigate" out of their folders | |
if settings.APPLICATION_STORAGE not in grouppath: | |
#make sure that grouppath is inside the APPLICATION_STORAGE | |
return render_to_response('404.html') | |
elif "../" in grouppath: | |
#make sure not to allow relative paths, etc. | |
return render_to_response('404.html') | |
temp = tempfile.TemporaryFile() | |
archivename = os.path.basename(grouppath) + ".zip" # archive in the curdir | |
#zipdir(grouppath, archivename) | |
assert os.path.isdir(grouppath) | |
with closing(ZipFile(temp, "w", ZIP_DEFLATED)) as z: | |
for root, dirs, files in os.walk(grouppath): | |
#NOTE: ignore empty directories | |
for fn in files: | |
absfn = os.path.join(root, fn) | |
zfn = absfn[len(grouppath)+len(os.sep):] #XXX: relative path | |
z.write(absfn, zfn) | |
wrapper = FileWrapper(temp) | |
response = HttpResponse(wrapper, content_type='application/zip') | |
response['Content-Disposition'] = 'attachment; filename=' + archivename | |
response['Content-Length'] = temp.tell() | |
temp.seek(0) | |
return response | |
@login_required | |
def download_file_as_zip(request,groupname,filename): | |
""" | |
Download a file as zipfile. | |
""" | |
custgroup = Group.objects.get(name=groupname) | |
if custgroup not in request.user.groups.all(): | |
return render_to_response('404.html') | |
else: | |
pass | |
path = settings.APPLICATION_STORAGE | |
filename = os.path.join(groupname,filename) | |
filepath = os.path.join(path,filename) | |
temp = tempfile.TemporaryFile() | |
archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED) | |
archive.write(filepath, os.path.basename(filename)) | |
archive.close() | |
wrapper = FileWrapper(temp) | |
response = HttpResponse(wrapper, content_type='application/zip') | |
response['Content-Disposition'] = 'attachment; filename=%s.zip'\ | |
%(os.path.basename(filename)) | |
response['Content-Length'] = temp.tell() | |
temp.seek(0) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment