def list_directory(request,share,subdir=None): if not share.check_path(subdir=subdir): return render(request,'error.html', {"message": "Unable to locate the files. It is possible that the directory has been moved, renamed, or deleted.","share":share,"subdir":subdir}) files,directories = list_share_dir(share,subdir=subdir,ajax=request.is_ajax()) print files if request.is_ajax(): return json_response({'files':files,'directories':directories.values()}) #Find any shares that point at this directory for s in Share.user_queryset(request.user).filter(real_path__in=directories.keys()).exclude(id=share.id): directories[s.real_path]['share']=s share_perms = share.get_user_permissions(request.user) PATH = share.get_path() subshare = None if subdir is not None: PATH = os.path.join(PATH,subdir) subshare = Share.objects.filter(parent=share,sub_directory=subdir).first() owner = request.user == share.owner all_perms = share.get_permissions(user_specific=True) shared_users = all_perms['user_perms'].keys() shared_groups = [g['group']['name'] for g in all_perms['group_perms']] emails = sorted([u.email for u in share.get_users_with_permissions()]) readme = None #The following block is for markdown rendering if os.path.isfile(os.path.join(PATH,'README.md')): import markdown input_file = codecs.open(os.path.join(PATH,'README.md'), mode="r", encoding="utf-8") text = input_file.read() readme = markdown.markdown(text,extensions=['fenced_code','tables','nl2br']) download_base = reverse('download_file',kwargs={'share':share.id,'subpath':subdir if subdir else ''}) readme = re.sub(r'src="(?!http)',r'src="{0}'.format(download_base),readme) return render(request,'list.html', {"session_cookie":request.COOKIES.get('sessionid'),"files":files,"directories":directories.values(),"path":PATH,"share":share,"subshare":subshare,"subdir": subdir,'rsync_url':get_setting('RSYNC_URL',None),'HOST':get_setting('HOST',None),'SFTP_PORT':get_setting('SFTP_PORT',None),"folder_form":FolderForm(),"metadata_form":MetaDataForm(), "rename_form":RenameForm(),"request":request,"owner":owner,"share_perms":share_perms,"all_perms":all_perms,"share_perms_json":json.dumps(share_perms),"shared_users":shared_users,"shared_groups":shared_groups,"emails":emails, "readme":readme})
def search_files(request): from utils import find query = request.GET.get('query',None) results=[] if query: shares = Share.user_queryset(request.user) for s in shares: r=find(s,query,prepend_share_id=False) results.append({'share':s,'results':r}) return render(request, 'search/search_files.html', {'query':query,'results':results})
def list_directory(request,share,subdir=None): if not share.check_path(): return render(request,'index.html', {"message": "Unable to locate the files for this share. Please contact the site administrator."}) from os import listdir, stat from os.path import isfile, join, getsize, normpath import time, datetime PATH = share.get_path() subshare = None if subdir is not None: PATH = join(PATH,subdir) subshare = Share.objects.filter(parent=share,sub_directory=subdir).first() share_perms = share.get_user_permissions(request.user) if not share.secure: share_perms = list(set(share_perms+['view_share_files','download_share_files'])) file_list=[] directories={} regex = r'^%s[^/]+/?' % '' if subdir is None else normpath(subdir)+'/' metadatas = {} for md in MetaData.objects.filter(share=share,subpath__regex=regex): metadatas[md.subpath]= md if not request.is_ajax() else md.json() for name in listdir(PATH): path = join(PATH,name) subpath= name if subdir is None else join(subdir,name) # metadata = MetaData.get_or_none(share=share,subpath=subpath) metadata = metadatas[subpath] if metadatas.has_key(subpath) else {} if isfile(path): (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = stat(path) file={'name':name,'extension':name.split('.').pop() if '.' in name else None,'size':sizeof_fmt(size),'bytes':size,'modified':datetime.datetime.fromtimestamp(mtime).strftime("%m/%d/%Y %I:%M %p"),'metadata':metadata,'isText':istext(path)} file_list.append(file) else: (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = stat(path) dir={'name':name,'size':getsize(path),'metadata':metadata,'modified':datetime.datetime.fromtimestamp(mtime).strftime("%m/%d/%Y %I:%M %p")} directories[os.path.realpath(path)]=dir if request.is_ajax(): return json_response({'files':file_list,'directories':directories.values()}) #Find any shares that point at this directory print directories.keys() for s in Share.user_queryset(request.user).filter(real_path__in=directories.keys()).exclude(id=share.id): directories[s.real_path]['share']=s owner = request.user == share.owner all_perms = share.get_permissions(user_specific=True) shared_users = all_perms['user_perms'].keys() shared_groups = [g['group']['name'] for g in all_perms['group_perms']] ftp_user = ShareFTPUser.objects.filter(share=share,user__isnull=True).first() or ShareFTPUser.objects.filter(share=share,user=request.user).first() return render(request,'list.html', {"session_cookie":request.COOKIES.get('sessionid'),"files":file_list,"directories":directories.values(),"path":PATH,"share":share,"subshare":subshare,"subdir": subdir,'rsync_url':get_setting('RSYNC_URL',None),'HOST':get_setting('HOST',None),'SFTP_PORT':get_setting('SFTP_PORT',None),"folder_form":FolderForm(),"metadata_form":MetaDataForm(), "rename_form":RenameForm(),"request":request,"owner":owner,"share_perms":share_perms,"all_perms":all_perms,"share_perms_json":json.dumps(share_perms),"shared_users":shared_users,"shared_groups":shared_groups,'ftp_user':ftp_user})