def download_file(request): email = request.user.email file_name = request.GET['file_name'] current_dir = request.GET['current_dir'] current_dir=setClear(sanitize(current_dir)) file_path = os.path.join(settings.LOCAL_BUFFER_PATH,email,current_dir,file_name) file_path = file_path.encode('utf-8') if not os.path.exists(file_path): if settings.USE_SWIFT: swift = Swift() swift.connect() swift.get_object_to_file(email,current_dir,file_name) type,encoding = mimetypes.guess_type(file_path) if type is None: content_type = magic.from_file(file_path,mime=True) if content_type == 'application/msword': content_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" else: content_type = type f = open(file_path) response = HttpResponse(FileWrapper(f),content_type=content_type) response['Content-encoding'] = encoding response['Content-Length'] = os.path.getsize(file_path) if u'WebKit' in request.META['HTTP_USER_AGENT']: filename_header = 'filename="%s"' % file_name.encode('utf-8') elif u'MSIE' in request.META['HTTP_USER_AGENT']: filename_header = '' filename_header = 'filename*=UTF-8\'\'%s' % urllib.quote(file_name.encode('utf-8')) else: filename_header = 'filename*=UTF-8\'\'%s' % urllib.quote(file_name.encode('utf-8')) response['Content-Disposition'] = 'attachement; '+ filename_header return response
def files(request): user = request.user file_list = [] WEB_RSYNC = settings.WEB_RSYNC if request.GET.has_key('current_dir'): current_dir= request.GET['current_dir'] else: current_dir= '' current_dir=setClear(sanitize(current_dir)) if request.GET.has_key('order_by'): order_by = request.GET['order_by'] if request.GET.has_key('sort_method'): sort_method = request.GET['sort_method'] filelevel_list = current_dir.split('/') if '' in filelevel_list: filelevel_list.remove('') final_filelevel_list = [] levelname = {} if filelevel_list.__len__() > 0: current_level = '' for level in filelevel_list: current_level += level + '/' final_filelevel_list.append({'href':current_level,'name':level}) lc=locals() return render(request,'home/files.html', lc)
def batch_download(request): files = request.GET['files'] current_dir = request.GET['current_dir'] current_dir=setClear(sanitize(current_dir)) email = request.user.email #file_name = request.GET['file_name'] file_list = files.split('#') file_list.remove('') zipfilename = 'batch_'+get_timestamp()+'.zip'; zipfilepath = os.path.join(settings.LOCAL_BUFFER_PATH,'batched',zipfilename) zfile = zipfile.ZipFile(zipfilepath, 'w', compression=zipfile.ZIP_DEFLATED) for file in file_list: file_path = os.path.join(settings.LOCAL_BUFFER_PATH,email,current_dir,file) file_path = file_path.encode('utf-8') zfile.write(file_path,arcname=file) prefix_name = os.path.join(settings.LOCAL_BUFFER_PATH,email,current_dir) for temp_root, temp_dirs, temp_files in os.walk(file_path): for temp_file in temp_files: temp_full_file_name = os.path.join(temp_root, temp_file) temp_archname = temp_full_file_name.replace(prefix_name,'') zfile.write(os.path.join(temp_root, temp_file),arcname=temp_archname) zfile.close() return HttpResponse(zipfilename)
def uploadhandler(request): if request.POST.has_key('current_dir'): current_dir= request.POST['current_dir'] else: current_dir= '' current_dir=setClear(sanitize(current_dir)) file = request.FILES['file'] handle_uploaded_file(request.user.email,current_dir,file) return HttpResponseRedirect('/home/files?current_dir='+current_dir)
def uploadfile(request): data = ["\u521b\u8d5b\u4ee3\u7801.zip"] if request.GET.has_key('current_dir'): current_dir= request.GET['current_dir'] else: current_dir= '' current_dir=setClear(sanitize(current_dir)) file = request.FILES['myfile'] return_data = [file.__str__()] handle_uploaded_file(request.user.email,current_dir,file) return HttpResponse(json.dumps(return_data))
def openShare(request): filename = request.GET['filename'] current_dir = request.GET['current_dir'] current_dir=setClear(sanitize(current_dir)) filepath = os.path.join(settings.LOCAL_BUFFER_PATH,request.user.email,current_dir,filename) filepath = filepath.encode('utf-8') object_name = current_dir+filename random_code= uuid.uuid1().hex[0:20] secret = uuid.uuid4().hex[0:4] o = openSharedObject(ownerEmail=request.user.email,objectName = object_name,randomCode=random_code,secret=secret) o.save() print 'result', random_code,secret return HttpResponse(random_code)
def new_folder(request): new_folder = request.GET['new_folder'] current_dir = request.GET['current_dir'] current_dir=setClear(sanitize(current_dir)) folder_path = os.path.join(settings.LOCAL_BUFFER_PATH,request.user.email,current_dir,new_folder) folder_path = folder_path.encode('utf-8') try: os.mkdir(folder_path) if settings.USE_SWIFT: swift = Swift() swift.connect() prefix = current_dir swift.put_object_of_foler(container = request.user.email,prefix = prefix,folder=new_folder) except: print 'fail to create '+new_folder return HttpResponseRedirect('/home/files?current_dir='+current_dir)
def rename_file(request): old_name = request.GET['old_name'] new_name = request.GET['new_name'] if request.GET.has_key('current_dir'): current_dir= request.GET['current_dir'] else: current_dir= '' current_dir=setClear(sanitize(current_dir)) #todo swfit rename try: buffer_path = os.path.join(settings.LOCAL_BUFFER_PATH,request.user.email,current_dir,old_name) new_path = os.path.join(settings.LOCAL_BUFFER_PATH,request.user.email,current_dir,new_name) os.renames(buffer_path,new_path) except: print 'fail to rename'+old_name return HttpResponseRedirect('/home/files?current_dir='+current_dir)
def delete_file(request): file_name = request.POST['file_name'] current_dir = request.POST['current_dir'] current_dir=setClear(sanitize(current_dir)) if settings.USE_SWIFT: swift = Swift() swift.connect() prefix = current_dir if file_name[-1] == '/': swift.delete_folder(request.user.email,prefix+file_name) else: swift.delete_object(request.user.email,prefix,file_name) try: buffer_path = os.path.join(settings.LOCAL_BUFFER_PATH,request.user.email,current_dir,file_name) buffer_path = buffer_path.encode('utf-8') if os.path.isdir(buffer_path): shutil.rmtree(buffer_path) else: os.remove(buffer_path) except: print 'locally fail to delete '+file_name return HttpResponseRedirect('/home/files?current_dir='+current_dir)
def filelist(request): RenameAllowed = False user = request.user file_list = [] WEB_RSYNC = settings.WEB_RSYNC if request.GET.has_key('current_dir'): current_dir= request.GET['current_dir'] else: current_dir= '' current_dir=setClear(sanitize(current_dir)) if settings.USE_SWIFT: swift = Swift() swift.connect() prefix = current_dir if prefix == '': prefix = None else: prefix = prefix tuple = swift.list_container(user.email,prefix=prefix,delimiter='/'); if tuple == None: swift.put_container(user.email) tuple = swift.list_container(user.email,prefix=prefix,delimiter='/') for f in tuple[1]: if f.has_key('bytes'): fname = f['name'].split('/')[-1] fname = fname.replace('/','') if fname == '': continue this_dir = './' fileType = 'file' filesize = int(f['bytes']) if(filesize < 1000): filesize = filesize.__str__() +' B' elif(filesize < 1000*1000): filesize = (int(float(filesize)/1000)).__str__() +' KB' elif(filesize < 1000*1000*1000): filesize = (round(float(filesize)/1000/1000,1)).__str__() +' MB' last_modified = f['last_modified'] pattern = re.compile(r'\.\d{6}',re.S) last_modified = pattern.sub('',last_modified) last_modified = last_modified.replace('T',' ') icon = 'text-icon icon' else: icon = 'folder-icon icon' this_dir = f['subdir'] fname = f['subdir'] fname = fname.split('/') fname = fname[-2] last_modified = '' filesize = 0 fileType = 'dir' file_list.append({'icon':icon,'this_dir':this_dir,'filetype':fileType,'name':fname,'last_modified':last_modified,'bytes':filesize}) else: user_path = os.path.join(settings.LOCAL_BUFFER_PATH,request.user.email,current_dir) file_list=[] files = os.listdir(user_path) for f in files: #print datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(user_path,f))) abs_path = os.path.join(user_path,f) abs_path = abs_path.encode('utf-8') type,encoding = mimetypes.guess_type(abs_path) if type == None: icon='glyphicon glyphicon-file icon' elif type.split('/')[0] == 'image': icon = 'image-icon icon' elif type == 'application/zip': icon = 'zip-icon icon' elif type == 'application/pdf': icon= 'pdf-icon icon' elif type.split('/')[0] == 'video': icon = 'icon video-icon' elif type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' or type=='application/msword': icon = 'word-icon icon' elif type=='application/vnd.ms-xpsdocument': icon = 'xps-icon icon' elif type.split('/')[0] == 'text': icon = 'text-icon icon' elif type == 'application/vnd.ms-excel' or type =='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': icon = 'excel-icon icon' elif type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation' or type == 'application/mspowerpoint': icon = 'ppt-icon icon' else: icon='glyphicon glyphicon-file icon' this_dir = './' fileType = 'file' if os.path.isdir(abs_path): icon = 'folder-icon icon' fileType = 'dir' this_dir = os.path.join(current_dir,f) this_dir = this_dir.encode('utf-8') rawsize = os.path.getsize(abs_path) filesize = rawsize if(filesize < 1000): filesize = filesize.__str__() +' B' elif(filesize < 1000*1000): filesize = (int(float(filesize)/1000)).__str__() +' KB' elif(filesize < 1000*1000*1000): filesize = (round(float(filesize)/1000/1000,1)).__str__() +' MB' last_modified = datetime.datetime.fromtimestamp(os.path.getmtime(abs_path)); last_modified_format = datetime.datetime(last_modified.year, last_modified.month, last_modified.day, last_modified.hour, last_modified.minute, last_modified.second) last_modified_str = last_modified_format.__str__() file_list.append({'this_dir':this_dir,'filetype':fileType,'icon':icon,'name':f,'rawsize':rawsize,'bytes':filesize,'last_modified':last_modified_str}) sort_method = 'asc' if not request.GET.has_key('order_by'): file_list = sorted(file_list,key = lambda k:k['last_modified'],reverse=True) else: order_by = request.GET['order_by'] if order_by == 'filename': file_list = sorted(file_list,key = lambda k:k['name']) elif order_by == 'size': file_list = sorted(file_list,key = lambda k:k['rawsize']) else: file_list = sorted(file_list,key = lambda k:k['last_modified'],reverse=True) if request.GET.has_key('sort_method'): sort_method = request.GET['sort_method'] if sort_method == 'desc': file_list = reversed(file_list) sort = 'desc' lc=locals() return render(request,'home/fileList.html', lc)