def get_dir_share_link(fileshare): # dir shared link if fileshare: dir_shared_link = gen_dir_share_link(fileshare.token) else: dir_shared_link = '' return dir_shared_link
def list_shared_links(request): """List share links, and remove invalid links(file/dir is deleted or moved). """ username = request.user.username fileshares = FileShare.objects.filter(username=username) p_fileshares = [] # personal file share for fs in fileshares: if is_personal_repo(fs.repo_id): # only list files in personal repos r = seafile_api.get_repo(fs.repo_id) if not r: fs.delete() continue if fs.s_type == 'f': if seafile_api.get_file_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path) fs.shared_link = gen_file_share_link(fs.token) else: if seafile_api.get_dir_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path.rstrip('/')) fs.shared_link = gen_dir_share_link(fs.token) fs.repo = r p_fileshares.append(fs) return render_to_response('repo/shared_links.html', { "fileshares": p_fileshares, }, context_instance=RequestContext(request))
def list_shared_links(request): """List shared links, and remove invalid links(file/dir is deleted or moved). """ username = request.user.username # download links fileshares = FileShare.objects.filter(username=username) p_fileshares = [] # personal file share for fs in fileshares: if is_personal_repo(fs.repo_id): # only list files in personal repos r = seafile_api.get_repo(fs.repo_id) if not r: fs.delete() continue if fs.s_type == 'f': if seafile_api.get_file_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path) fs.shared_link = gen_file_share_link(fs.token) else: if seafile_api.get_dir_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path.rstrip('/')) fs.shared_link = gen_dir_share_link(fs.token) fs.repo = r p_fileshares.append(fs) # upload links uploadlinks = UploadLinkShare.objects.filter(username=username) p_uploadlinks = [] for link in uploadlinks: if is_personal_repo(link.repo_id): r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue link.dir_name = os.path.basename(link.path.rstrip('/')) link.shared_link = gen_shared_upload_link(link.token) link.repo = r p_uploadlinks.append(link) return render_to_response('share/links.html', { "fileshares": p_fileshares, "uploadlinks": p_uploadlinks, }, context_instance=RequestContext(request))
def list_shared_links(request): """List shared links, and remove invalid links(file/dir is deleted or moved). """ username = request.user.username # download links fileshares = FileShare.objects.filter(username=username) p_fileshares = [] # personal file share for fs in fileshares: r = seafile_api.get_repo(fs.repo_id) if not r: fs.delete() continue if fs.s_type == 'f': if seafile_api.get_file_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path) fs.shared_link = gen_file_share_link(fs.token) else: if seafile_api.get_dir_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path.rstrip('/')) fs.shared_link = gen_dir_share_link(fs.token) fs.repo = r p_fileshares.append(fs) # upload links uploadlinks = UploadLinkShare.objects.filter(username=username) p_uploadlinks = [] for link in uploadlinks: r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue link.dir_name = os.path.basename(link.path.rstrip('/')) link.shared_link = gen_shared_upload_link(link.token) link.repo = r p_uploadlinks.append(link) return render_to_response('share/links.html', { "fileshares": p_fileshares, "uploadlinks": p_uploadlinks, }, context_instance=RequestContext(request))
def get_repo_dirents(request, repo, commit, path, offset=-1, limit=-1): """List repo dirents based on commit id and path. Use ``offset`` and ``limit`` to do paginating. Returns: A tupple of (file_list, dir_list, dirent_more) TODO: Some unrelated parts(file sharing, stars, modified info, etc) need to be pulled out to multiple functions. """ dir_list = [] file_list = [] dirent_more = False if commit.root_id == EMPTY_SHA1: return ([], [], False) if limit == -1 else ([], [], False) else: try: dirs = seafile_api.list_dir_by_commit_and_path( commit.repo_id, commit.id, path, offset, limit) if not dirs: return ([], [], False) except SearpcError as e: logger.error(e) return ([], [], False) if limit != -1 and limit == len(dirs): dirent_more = True username = request.user.username starred_files = get_dir_starred_files(username, repo.id, path) fileshares = FileShare.objects.filter(repo_id=repo.id).filter( username=username) uploadlinks = UploadLinkShare.objects.filter(repo_id=repo.id).filter( username=username) view_dir_base = reverse("view_common_lib_dir", args=[repo.id, '']) dl_dir_base = reverse('repo_download_dir', args=[repo.id]) file_history_base = reverse('file_revisions', args=[repo.id]) for dirent in dirs: dirent.last_modified = dirent.mtime dirent.sharelink = '' dirent.uploadlink = '' if stat.S_ISDIR(dirent.props.mode): dpath = posixpath.join(path, dirent.obj_name) if dpath[-1] != '/': dpath += '/' for share in fileshares: if dpath == share.path: dirent.sharelink = gen_dir_share_link(share.token) dirent.sharetoken = share.token break for link in uploadlinks: if dpath == link.path: dirent.uploadlink = gen_shared_upload_link(link.token) dirent.uploadtoken = link.token break p_dpath = posixpath.join(path, dirent.obj_name) dirent.view_link = view_dir_base + '?p=' + urlquote(p_dpath) dirent.dl_link = dl_dir_base + '?p=' + urlquote(p_dpath) dir_list.append(dirent) else: file_list.append(dirent) if repo.version == 0: dirent.file_size = get_file_size(repo.store_id, repo.version, dirent.obj_id) else: dirent.file_size = dirent.size dirent.starred = False fpath = posixpath.join(path, dirent.obj_name) p_fpath = posixpath.join(path, dirent.obj_name) dirent.view_link = reverse('view_lib_file', args=[repo.id, p_fpath]) dirent.dl_link = get_file_download_link( repo.id, dirent.obj_id, p_fpath) dirent.history_link = file_history_base + '?p=' + urlquote( p_fpath) if fpath in starred_files: dirent.starred = True for share in fileshares: if fpath == share.path: dirent.sharelink = gen_file_share_link(share.token) dirent.sharetoken = share.token break return (file_list, dir_list, dirent_more)
def get_repo_dirents(request, repo, commit, path, offset=-1, limit=-1): """List repo dirents based on commit id and path. Use ``offset`` and ``limit`` to do paginating. Returns: A tupple of (file_list, dir_list, dirent_more) TODO: Some unrelated parts(file sharing, stars, modified info, etc) need to be pulled out to multiple functions. """ dir_list = [] file_list = [] dirent_more = False if commit.root_id == EMPTY_SHA1: return ([], [], False) if limit == -1 else ([], [], False) else: try: dirs = seafile_api.list_dir_by_commit_and_path(commit.repo_id, commit.id, path, offset, limit) if not dirs: return ([], [], False) except SearpcError as e: logger.error(e) return ([], [], False) if limit != -1 and limit == len(dirs): dirent_more = True username = request.user.username starred_files = get_dir_starred_files(username, repo.id, path) fileshares = FileShare.objects.filter(repo_id=repo.id).filter(username=username) uploadlinks = UploadLinkShare.objects.filter(repo_id=repo.id).filter(username=username) view_dir_base = reverse("view_common_lib_dir", args=[repo.id, '']) dl_dir_base = reverse('repo_download_dir', args=[repo.id]) file_history_base = reverse('file_revisions', args=[repo.id]) for dirent in dirs: dirent.last_modified = dirent.mtime dirent.sharelink = '' dirent.uploadlink = '' if stat.S_ISDIR(dirent.props.mode): dpath = posixpath.join(path, dirent.obj_name) if dpath[-1] != '/': dpath += '/' for share in fileshares: if dpath == share.path: dirent.sharelink = gen_dir_share_link(share.token) dirent.sharetoken = share.token break for link in uploadlinks: if dpath == link.path: dirent.uploadlink = gen_shared_upload_link(link.token) dirent.uploadtoken = link.token break p_dpath = posixpath.join(path, dirent.obj_name) dirent.view_link = view_dir_base + '?p=' + urlquote(p_dpath) dirent.dl_link = dl_dir_base + '?p=' + urlquote(p_dpath) dir_list.append(dirent) else: file_list.append(dirent) if repo.version == 0: dirent.file_size = get_file_size(repo.store_id, repo.version, dirent.obj_id) else: dirent.file_size = dirent.size dirent.starred = False fpath = posixpath.join(path, dirent.obj_name) p_fpath = posixpath.join(path, dirent.obj_name) dirent.view_link = reverse('view_lib_file', args=[repo.id, p_fpath]) dirent.dl_link = get_file_download_link(repo.id, dirent.obj_id, p_fpath) dirent.history_link = file_history_base + '?p=' + urlquote(p_fpath) if fpath in starred_files: dirent.starred = True for share in fileshares: if fpath == share.path: dirent.sharelink = gen_file_share_link(share.token) dirent.sharetoken = share.token break return (file_list, dir_list, dirent_more)
def list_shared_links(request): """List shared links, and remove invalid links(file/dir is deleted or moved). """ username = request.user.username # download links fileshares = FileShare.objects.filter(username=username) fs_files, fs_dirs = [], [] for fs in fileshares: r = seafile_api.get_repo(fs.repo_id) if not r: fs.delete() continue if fs.is_file_share_link(): if seafile_api.get_file_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path) fs.shared_link = gen_file_share_link(fs.token) else: if seafile_api.get_dir_id_by_path(r.id, fs.path) is None: fs.delete() continue if fs.path != '/': fs.filename = os.path.basename(fs.path.rstrip('/')) else: fs.filename = fs.path fs.shared_link = gen_dir_share_link(fs.token) fs.repo = r if fs.expire_date is not None and timezone.now() > fs.expire_date: fs.is_expired = True fs_files.append(fs) if fs.is_file_share_link() else fs_dirs.append(fs) fs_files.sort(lambda x, y: cmp(x.filename, y.filename)) fs_dirs.sort(lambda x, y: cmp(x.filename, y.filename)) # upload links uploadlinks = UploadLinkShare.objects.filter(username=username) p_uploadlinks = [] for link in uploadlinks: r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue if link.path != '/': link.dir_name = os.path.basename(link.path.rstrip('/')) else: link.dir_name = link.path link.shared_link = gen_shared_upload_link(link.token) link.repo = r p_uploadlinks.append(link) p_uploadlinks.sort(lambda x, y: cmp(x.dir_name, y.dir_name)) return render_to_response('share/links.html', { "fileshares": fs_dirs + fs_files, "uploadlinks": p_uploadlinks, }, context_instance=RequestContext(request))