def delete_table(self, dtable): repo_id = dtable.workspace.repo_id asset_dir_path = '/asset/' + str(dtable.uuid) asset_dir_id = seafile_api.get_dir_id_by_path(repo_id, asset_dir_path) _, table_file_name, _ = restore_trash_dtable_names(dtable) if asset_dir_id: parent_dir = os.path.dirname(asset_dir_path) file_name = os.path.basename(asset_dir_path) try: seafile_api.del_file(repo_id, parent_dir, file_name, '') except SearpcError as e: logger.error('delete dtable: %s assets error: %s', dtable.id, e) self.stderr.write('[%s] delete file: %s error: %s' % (datetime.now(), dtable.name, e)) # delete table try: seafile_api.del_file(repo_id, '/', table_file_name, '') except SearpcError as e: logger.error('delete dtable: %s file error: %s', table_file_name, e) try: DTables.objects.delete_dtable(dtable.workspace, dtable.name) delete_dtable.send( sender=None, dtable_uuid=dtable.uuid.hex, ) except Exception as e: logger.error('delete table: %s in db error: %s', dtable.id, e)
def handleMove(self, destPath): if self.provider.readonly: raise DAVError(HTTP_FORBIDDEN) parts = destPath.strip("/").split("/", 1) if len(parts) <= 1: raise DAVError(HTTP_BAD_REQUEST) repo_name = parts[0] rel_path = parts[1] dest_dir, dest_file = os.path.split(rel_path) dest_repo = getRepoByName(repo_name, self.username, self.org_id, self.is_guest) if seafile_api.check_permission_by_path(dest_repo.id, self.rel_path, self.username) != "rw": raise DAVError(HTTP_FORBIDDEN) src_dir, src_file = os.path.split(self.rel_path) if not src_file: raise DAVError(HTTP_BAD_REQUEST) if not seafile_api.is_valid_filename(dest_repo.id, dest_file): raise DAVError(HTTP_BAD_REQUEST) # some clients such as GoodReader requires "overwrite" semantics file_id_dest = seafile_api.get_file_id_by_path(dest_repo.id, rel_path) if file_id_dest != None: seafile_api.del_file(dest_repo.id, dest_dir, dest_file, self.username) seafile_api.move_file(self.repo.id, src_dir, src_file, dest_repo.id, dest_dir, dest_file, self.username, NEED_PROGRESS, SYNCHRONOUS) return True
def delete(self, operator): draft_file_name = os.path.basename(self.draft_file_path) draft_file_path = os.path.dirname(self.draft_file_path) seafile_api.del_file(self.origin_repo_id, draft_file_path, draft_file_name, operator) super(Draft, self).delete()
def handle_move(self, dest_path): if self.provider.readonly: raise DAVError(HTTP_FORBIDDEN) parts = dest_path.strip("/").split("/", 1) if len(parts) <= 1: raise DAVError(HTTP_BAD_REQUEST) repo_name = parts[0] rel_path = parts[1] dest_dir, dest_file = os.path.split(rel_path) dest_repo = getRepoByName(repo_name, self.username, self.org_id, self.is_guest) if seafile_api.check_permission_by_path(dest_repo.id, self.rel_path, self.username) != "rw": raise DAVError(HTTP_FORBIDDEN) src_dir, src_file = os.path.split(self.rel_path) if not src_file: raise DAVError(HTTP_BAD_REQUEST) if not seafile_api.is_valid_filename(dest_repo.id, dest_file): raise DAVError(HTTP_BAD_REQUEST) # some clients such as GoodReader requires "overwrite" semantics file_id_dest = seafile_api.get_file_id_by_path(dest_repo.id, rel_path) if file_id_dest != None: seafile_api.del_file(dest_repo.id, dest_dir, dest_file, self.username) seafile_api.move_file(self.repo.id, src_dir, src_file, dest_repo.id, dest_dir, dest_file, 1, self.username, NEED_PROGRESS, SYNCHRONOUS) return True
def delete(self, request, slug, page_name): """Delete a page in a wiki """ try: wiki = Wiki.objects.get(slug=slug) except Wiki.DoesNotExist: error_msg = "Wiki not found." return api_error(status.HTTP_404_NOT_FOUND, error_msg) username = request.user.username if wiki.username != username: error_msg = _('Permission denied.') return api_error(status.HTTP_403_FORBIDDEN, error_msg) try: repo = seafile_api.get_repo(wiki.repo_id) if not repo: error_msg = "Wiki library not found." return api_error(status.HTTP_404_NOT_FOUND, error_msg) except SearpcError: error_msg = _("Internal Server Error") return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) file_name = page_name + ".md" try: seafile_api.del_file(repo.id, '/', file_name, request.user.username) except SearpcError as e: logger.error(e) error_msg = _('Internal Server Error') return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response()
def delete_dirents(request, repo_id): """ Delete multi files/dirs with ajax. """ content_type = 'application/json; charset=utf-8' repo = get_repo(repo_id) if not repo: err_msg = _(u'Library does not exist.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) # argument checking parent_dir = request.GET.get("parent_dir") dirents_names = request.POST.getlist('dirents_names') if not (parent_dir and dirents_names): err_msg = _(u'Argument missing.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) # permission checking username = request.user.username deleted = [] undeleted = [] for dirent_name in dirents_names: full_path = posixpath.join(parent_dir, dirent_name) if check_folder_permission(request, repo.id, full_path) != 'rw': undeleted.append(dirent_name) continue try: seafile_api.del_file(repo_id, parent_dir, dirent_name, username) deleted.append(dirent_name) except SearpcError, e: logger.error(e) undeleted.append(dirent_name)
def delete(self, request, dtable_uuid): # argument check parent_path = request.GET.get('parent_path', '') if not parent_path: error_msg = 'parent_path is invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) parent_path = parent_path.lstrip('/') name = request.GET.get('name', '').lstrip('/') if not name: error_msg = 'name is invalid' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # dtable check dtable = DTables.objects.get_dtable_by_uuid(dtable_uuid) if not dtable: error_msg = 'Table %s not found.' % (dtable_uuid, ) return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check username = request.user.username if check_dtable_permission(username, dtable.workspace) != PERMISSION_READ_WRITE: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # delete resource parent_path = os.path.join('/asset', dtable_uuid, parent_path) try: seafile_api.del_file(dtable.workspace.repo_id, parent_path, name, username) except Exception as e: logger.error('del parent_path: %s, name: %s, error: %s', parent_path, name, e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def delete(self, request, repo_id): """ delete a single file/folder in a library """ path = request.GET.get('path', None) if not path: error_msg = 'path invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) path = normalize_file_path(path) file_id = None dir_id = None try: file_id = seafile_api.get_file_id_by_path(repo_id, path) dir_id = seafile_api.get_dir_id_by_path(repo_id, path) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if not file_id and not dir_id: return Response({'success': True}) parent_dir = os.path.dirname(path) file_name = os.path.basename(path) try: seafile_api.del_file(repo_id, parent_dir, file_name, request.user.username) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def delete(self, request, repo_id, format=None): # delete file repo = seafile_api.get_repo(repo_id) if not repo: error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) path = request.GET.get('p', None) if not path: error_msg = 'p invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_id = seafile_api.get_file_id_by_path(repo_id, path) if not file_id: return Response({'success': True}) parent_dir = os.path.dirname(path) if check_folder_permission(request, repo_id, parent_dir) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) parent_dir = os.path.dirname(path) file_name = os.path.basename(path) try: seafile_api.del_file(repo_id, parent_dir, file_name, request.user.username) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if request.GET.get('reloaddir', '').lower() == 'true': return reloaddir(request, repo, parent_dir) else: return Response({'success': True})
def delete(self, request, group_id, table_id): """ delete a dtable from a group :param request: :param group_id: :param table_id: :return: """ group = seaserv.get_group(group_id) if not group: error_msg = _('Group not found.') return api_error(status.HTTP_404_NOT_FOUND, error_msg) owner = '%s@seafile_group' % (group_id, ) workspace = Workspaces.objects.get_workspace_by_owner(owner) if not workspace: error_msg = _('Workspace not found') return api_error(status.HTTP_404_NOT_FOUND, error_msg) dtable = DTables.objects.filter(workspace=workspace, id=int(table_id)).first() if not dtable: error_msg = _('Table not found') return api_error(status.HTTP_404_NOT_FOUND, error_msg) username = request.user.username # delete asset asset_dir_path = '/asset/' + str(dtable.uuid) asset_dir_id = seafile_api.get_dir_id_by_path(workspace.repo_id, asset_dir_path) if asset_dir_id: parent_dir = os.path.dirname(asset_dir_path) file_name = os.path.basename(asset_dir_path) try: seafile_api.del_file(workspace.repo_id, parent_dir, file_name, username) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) # delete table try: table_file_name = dtable.name + FILE_TYPE seafile_api.del_file(workspace.repo_id, '/', table_file_name, username) except SearpcError as e: logger.error(e) error_msg = _('Internal Server Error.') return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) try: DTables.objects.delete_dtable(workspace, dtable.name) except Exception as e: logger.error(e) error_msg = _('Internal Server Error.') return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True}, status=status.HTTP_200_OK)
def test_can_clean_department_repo_trash(self): if not LOCAL_PRO_DEV_ENV: return # create a department group_id = ccnet_api.create_group('department_test', 'system admin', parent_group_id=-1) seafile_api.set_group_quota(group_id, -2) repo_id = seafile_api.add_group_owned_repo(group_id, 'dep_test', 'rw') repo_owner = seafile_api.get_repo_owner(repo_id) assert '@seafile_group' in repo_owner group_repos = seafile_api.get_repos_by_group(group_id) assert len(group_repos) == 1 group = ccnet_api.get_group(group_id) # department add user ccnet_api.group_add_member(group_id, group.creator_name, self.user_name) ccnet_api.group_add_member(group_id, group.creator_name, self.tmp_user.username) ccnet_api.group_set_admin(group_id, self.user_name) ccnet_api.group_unset_admin(group_id, self.tmp_user.username) assert is_group_admin(group_id, self.user_name) assert not is_group_admin(group_id, self.tmp_user.username) file_name = 'dep_test.txt' self.create_file(repo_id=repo_id, parent_dir='/', filename=file_name, username=self.user_name) # delete a file first seafile_api.del_file(repo_id, '/', file_name, self.user_name) # get trash item count self.login_as(self.user) resp = self.client.get(reverse('api-v2.1-repo-trash', args=[repo_id])) json_resp = json.loads(resp.content) assert len(json_resp['data']) > 0 # department member can not clean trash self.logout() self.login_as(self.tmp_user) resp = self.client.delete(self.url) self.assertEqual(403, resp.status_code) # department admin can clean library trash self.logout() self.login_as(self.user) ccnet_api.group_set_admin(group_id, self.user_name) resp = self.client.delete(self.url) self.assertEqual(200, resp.status_code) # get trash item count again resp = self.client.get(self.url) json_resp = json.loads(resp.content) assert len(json_resp['data']) == 0
def delete(self, request, repo_id, format=None): """ Delete file. Permission checking: 1. user with 'rw' permission. """ # argument check path = request.GET.get('p', None) if not path: error_msg = 'p invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) path = normalize_file_path(path) # resource check repo = seafile_api.get_repo(repo_id) if not repo: error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) file_id = seafile_api.get_file_id_by_path(repo_id, path) if not file_id: return Response({'success': True}) # permission check parent_dir = os.path.dirname(path) username = request.user.username if check_folder_permission(request, repo_id, parent_dir) != PERMISSION_READ_WRITE: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # check file lock try: is_locked, locked_by_me = check_file_lock(repo_id, path, username) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if is_locked and not locked_by_me: error_msg = _("File is locked") return api_error(status.HTTP_403_FORBIDDEN, error_msg) # delete file file_name = os.path.basename(path) try: seafile_api.del_file(repo_id, parent_dir, file_name, request.user.username) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def handle_delete(self): if self.provider.readonly: raise DAVError(HTTP_FORBIDDEN) if seafile_api.check_permission_by_path(self.repo.id, self.rel_path, self.username) != "rw": raise DAVError(HTTP_FORBIDDEN) parent, filename = os.path.split(self.rel_path) seafile_api.del_file(self.repo.id, parent, filename, self.username) return True
def test_can_get(self): # delete a file first file_name = os.path.basename(self.file) seafile_api.del_file(self.repo_id, '/', file_name, self.user_name) self.login_as(self.user) resp = self.client.get(self.url) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert json_resp['data'][0]['obj_name'] == file_name assert json_resp['data'][0]['is_dir'] == False
def handleDelete(self): if self.provider.readonly: raise DAVError(HTTP_FORBIDDEN) if seafile_api.check_permission_by_path(self.repo.id, self.rel_path, self.username) != "rw": raise DAVError(HTTP_FORBIDDEN) parent, filename = os.path.split(self.rel_path) seafile_api.del_file(self.repo.id, parent, filename, self.username) return True
def handleDelete(self): if self.provider.readonly: raise DAVError(HTTP_FORBIDDEN) if seafile_api.check_permission(self.repo.id, self.username) != "rw": raise DAVError(HTTP_FORBIDDEN) parent, filename = os.path.split(self.rel_path) # Can't delete repo root if not filename: raise DAVError(HTTP_BAD_REQUEST) seafile_api.del_file(self.repo.id, parent, filename, self.username) return True
def delete(self, request, repo_id, format=None): """ Delete dir. Permission checking: 1. user with 'rw' permission. """ # argument check path = request.GET.get('p', None) if not path: error_msg = 'p invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if path == '/': error_msg = 'Can not delete root path.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # resource check dir_id = seafile_api.get_dir_id_by_path(repo_id, path) if not dir_id: error_msg = 'Folder %s not found.' % path return api_error(status.HTTP_404_NOT_FOUND, error_msg) repo = seafile_api.get_repo(repo_id) if not repo: error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check if check_folder_permission(request, repo_id, path) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) if path[-1] == '/': path = path[:-1] path = path.rstrip('/') username = request.user.username parent_dir = os.path.dirname(path) dir_name = os.path.basename(path) try: seafile_api.del_file(repo_id, parent_dir, dir_name, username) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def delete_dirent(request, repo_id): """ Delete a file/dir with ajax. """ content_type = 'application/json; charset=utf-8' repo = get_repo(repo_id) if not repo: err_msg = _(u'Library does not exist.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) # argument checking parent_dir = request.GET.get("parent_dir", None) dirent_name = request.GET.get("name", None) if not (parent_dir and dirent_name): err_msg = _(u'Argument missing.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) full_path = posixpath.join(parent_dir, dirent_name) username = request.user.username if seafile_api.get_dir_id_by_path(repo.id, full_path) is not None: # when dirent is a dir, check current dir perm if check_folder_permission(request, repo.id, full_path) != 'rw': err_msg = _('Permission denied') return HttpResponse(json.dumps({'error': err_msg}), status=403, content_type=content_type) if seafile_api.get_file_id_by_path(repo.id, full_path) is not None: # when dirent is a file, check parent dir perm if check_folder_permission(request, repo.id, parent_dir) != 'rw': err_msg = _('Permission denied') return HttpResponse(json.dumps({'error': err_msg}), status=403, content_type=content_type) # delete file/dir try: seafile_api.del_file(repo_id, parent_dir, dirent_name, username) return HttpResponse(json.dumps({'success': True}), content_type=content_type) except SearpcError, e: logger.error(e) err_msg = _(u'Internal error. Failed to delete %s.') % escape(dirent_name) return HttpResponse(json.dumps({'error': err_msg}), status=500, content_type=content_type)
def delete(self, request, repo_id): """ delete a single file/folder in a library """ repo = seafile_api.get_repo(repo_id) if not repo: error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) if not can_view_sys_admin_repo(repo): error_msg = 'Feature disabled.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) path = request.GET.get('path', None) if not path: error_msg = 'path invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if path[0] != '/': path = '/' + path file_id = None dir_id = None try: file_id = seafile_api.get_file_id_by_path(repo_id, path) dir_id = seafile_api.get_dir_id_by_path(repo_id, path) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if not file_id and not dir_id: return Response({'success': True}) parent_dir = os.path.dirname(path) file_name = os.path.basename(path) try: seafile_api.del_file(repo_id, parent_dir, file_name, request.user.username) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def delete(self, request, virus_id): virus_record = get_virus_record_by_id(virus_id) if not virus_record: error_msg = 'Virus file %d not found.' % virus_record.file_path return api_error(status.HTTP_404_NOT_FOUND, error_msg) parent_dir = os.path.dirname(virus_record.file_path) filename = os.path.basename(virus_record.file_path) try: seafile_api.del_file(virus_record.repo_id, parent_dir, filename, request.user.username) handle_virus_record(virus_id) except Exception as e: logger.error(e) error_msg = 'Failed to delete, please try again later.' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({"success": True}, status=status.HTTP_200_OK)
def iterate_and_del_files_recursively(repo_id, path, days): dirents = seafile_api.list_dir_by_path(repo_id, path) for dirent in dirents: if stat.S_ISDIR(dirent.mode): iterate_and_del_files_recursively( repo_id, os.path.join(path, dirent.obj_name), days) continue mtime = dirent.mtime cur_time = int(time.time()) time_delta = days * 24 * 60 * 60 if cur_time - time_delta > mtime: file_full_path = os.path.join(path, dirent.obj_name) seafile_api.del_file(repo_id, path, dirent.obj_name, 'seafevents') logger.info('{} of {} deleted at {}.'.format( file_full_path, repo_id, cur_time))
def delete_dirent(request, repo_id): """ Delete a file/dir with ajax. """ if not request.is_ajax(): raise Http404 content_type = 'application/json; charset=utf-8' repo = get_repo(repo_id) if not repo: err_msg = _(u'Library does not exist.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) # permission checking username = request.user.username if check_repo_access_permission(repo.id, username) != 'rw': err_msg = _(u'Permission denied.') return HttpResponse(json.dumps({'error': err_msg}), status=403, content_type=content_type) # argument checking parent_dir = request.GET.get("parent_dir", None) dirent_name = request.GET.get("name", None) if not (parent_dir and dirent_name): err_msg = _(u'Argument missing.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) # delete file/dir try: seafile_api.del_file(repo_id, parent_dir, dirent_name, username) return HttpResponse(json.dumps({'success': True}), content_type=content_type) except SearpcError, e: logger.error(e) err_msg = _(u'Internal error. Failed to delete %s.') % dirent_name return HttpResponse(json.dumps({'error': err_msg}), status=500, content_type=content_type)
def test_can_clean_library_trash(self): # delete a file first seafile_api.del_file(self.repo_id, '/', self.file_name, self.user_name) self.login_as(self.user) # get trash item count resp = self.client.get(self.url) json_resp = json.loads(resp.content) assert len(json_resp['data']) > 0 # clean library trash resp = self.client.delete(self.url) self.assertEqual(200, resp.status_code) # get trash item count again resp = self.client.get(self.url) json_resp = json.loads(resp.content) assert len(json_resp['data']) == 0
def test_enc_repo_post(self): # delete a file first seafile_api.del_file(self.enc_repo_id, '/', self.file_name, self.user_name) self.login_as(self.user) # get commit id form trash trash_url = reverse('api-v2.1-repo-trash', args=[self.enc_repo_id]) trash_resp = self.client.get(trash_url) self.assertEqual(200, trash_resp.status_code) trash_json_resp = json.loads(trash_resp.content) assert trash_json_resp['data'][0]['obj_name'] == self.file_name assert not trash_json_resp['data'][0]['is_dir'] assert trash_json_resp['data'][0]['commit_id'] commit_id = trash_json_resp['data'][0]['commit_id'] dir_url = reverse('api-v2.1-dir-view', args=[self.enc_repo_id]) url = reverse('api-v2.1-repo-commit-revert', args=[self.enc_repo_id, commit_id]) # test can not post without repo decrypted resp = self.client.post(url) self.assertEqual(403, resp.status_code) resp = self.client.get(dir_url) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert len(json_resp['dirent_list']) == 0 # test can post with repo decrypted decrypted_url = reverse('api-v2.1-repo-set-password', args=[self.enc_repo_id]) resp = self.client.post(decrypted_url, data={'password': '******'}) self.assertEqual(200, resp.status_code) resp = self.client.post(url) self.assertEqual(200, resp.status_code) resp = self.client.get(dir_url) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert len(json_resp['dirent_list']) == 1
def test_post(self): # delete a file first seafile_api.del_file(self.repo_id, '/', self.file_name, self.user_name) self.login_as(self.user) # get commit id form trash trash_url = reverse('api-v2.1-repo-trash', args=[self.repo_id]) trash_resp = self.client.get(trash_url) self.assertEqual(200, trash_resp.status_code) trash_json_resp = json.loads(trash_resp.content) assert trash_json_resp['data'][0]['obj_name'] == self.file_name assert not trash_json_resp['data'][0]['is_dir'] assert trash_json_resp['data'][0]['commit_id'] commit_id = trash_json_resp['data'][0]['commit_id'] dir_url = reverse('api-v2.1-dir-view', args=[self.repo_id]) url = reverse('api-v2.1-repo-commit-revert', args=[self.repo_id, commit_id]) # test can post resp = self.client.get(dir_url) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert len(json_resp['dirent_list']) == 1 resp = self.client.post(url) self.assertEqual(200, resp.status_code) resp = self.client.get(dir_url) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert len(json_resp['dirent_list']) == 2 # test_can_not_post_with_invalid_repo_permission self.logout() self.login_as(self.admin) resp = self.client.post(url) self.assertEqual(403, resp.status_code)
def delete_dirents(request, repo_id): """ Delete multi files/dirs with ajax. """ if not request.is_ajax(): raise Http404 content_type = 'application/json; charset=utf-8' repo = get_repo(repo_id) if not repo: err_msg = _(u'Library does not exist.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) # permission checking username = request.user.username if check_repo_access_permission(repo.id, username) != 'rw': err_msg = _(u'Permission denied.') return HttpResponse(json.dumps({'error': err_msg}), status=403, content_type=content_type) # argument checking parent_dir = request.GET.get("parent_dir") dirents_names = request.POST.getlist('dirents_names') if not (parent_dir and dirents_names): err_msg = _(u'Argument missing.') return HttpResponse(json.dumps({'error': err_msg}), status=400, content_type=content_type) deleted = [] undeleted = [] for dirent_name in dirents_names: try: seafile_api.del_file(repo_id, parent_dir, dirent_name, username) deleted.append(dirent_name) except SearpcError, e: logger.error(e) undeleted.append(dirent_name)
def delete(self, request, virus_id): """delete virus file """ if not request.user.admin_permissions.other_permission(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') virus_file = get_virus_file_by_vid(virus_id) if not virus_file: error_msg = 'Virus file %s not found.' % virus_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) parent_dir = os.path.dirname(virus_file.file_path) filename = os.path.basename(virus_file.file_path) try: seafile_api.del_file(virus_file.repo_id, parent_dir, filename, request.user.username) delete_virus_file(virus_id) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({"success": True}, status=status.HTTP_200_OK)
def test_share_dir_to_group(repo, group, permission): assert ccnet_api.group_add_member(group.id, USER, USER2) == 0 v_repo_id_1 = api.share_subdir_to_group(repo.id, '/dir1', USER, group.id, permission) v_repo_id_2 = api.share_subdir_to_group(repo.id, '/dir2', USER, group.id, permission) assert api.check_permission(v_repo_id_1, USER2) == permission assert api.check_permission(v_repo_id_2, USER2) == permission assert api.del_file(repo.id, '/', 'dir1', USER) == 0 assert api.unshare_subdir_for_group(repo.id, '/dir2', USER, group.id) == 0 assert api.check_permission(v_repo_id_1, USER2) is None assert api.check_permission(v_repo_id_2, USER2) is None
def test_share_dir_to_user(repo, permission): v_repo_id_1 = api.share_subdir_to_user(repo.id, '/dir1', USER, USER2, permission) v_repo_id_2 = api.share_subdir_to_user(repo.id, '/dir2', USER, USER2, permission) assert api.check_permission(v_repo_id_1, USER2) == permission assert api.check_permission(v_repo_id_2, USER2) == permission vir_repo_2 = api.get_shared_repo_by_path(repo.id, '/dir2', USER2) assert vir_repo_2.permission == permission assert api.del_file(repo.id, '/', 'dir1', USER) == 0 assert api.unshare_subdir_for_user(repo.id, '/dir2', USER, USER2) == 0 assert api.get_shared_repo_by_path(repo.id, '/dir1', USER2) is None assert api.get_shared_repo_by_path(repo.id, '/dir2', USER2) is None
def test_share_dir_to_group(repo, group, permission): assert ccnet_api.group_add_member(group.id, USER, USER2) == 0 v_repo_id_1 = api.share_subdir_to_group(repo.id, '/dir1', USER, group.id, permission) v_repo_id_2 = api.share_subdir_to_group(repo.id, '/dir2', USER, group.id, permission) assert api.check_permission(v_repo_id_1, USER2) == permission assert api.check_permission(v_repo_id_2, USER2) == permission repo_get = api.get_group_shared_repo_by_path (repo.id, '/dir1', group.id) assert repo_get and repo_get.repo_id == v_repo_id_1 users = api.get_shared_groups_for_subdir(repo.id, '/dir1', USER) assert len(users) == 1 assert api.del_file(repo.id, '/', 'dir1', USER) == 0 assert api.unshare_subdir_for_group(repo.id, '/dir2', USER, group.id) == 0 assert api.check_permission(v_repo_id_1, USER2) is None assert api.check_permission(v_repo_id_2, USER2) is None
def test_share_dir_to_user(repo, permission): v_repo_id_1 = api.share_subdir_to_user(repo.id, '/dir1', USER, USER2, permission) v_repo_id_2 = api.share_subdir_to_user(repo.id, '/dir2', USER, USER2, permission) assert api.check_permission(v_repo_id_1, USER2) == permission assert api.check_permission(v_repo_id_2, USER2) == permission vir_repo_2 = api.get_shared_repo_by_path(repo.id, '/dir2', USER2) assert vir_repo_2.permission == permission users = api.get_shared_users_for_subdir(repo.id, '/dir1', USER) assert len(users) == 1 and users[0].user == USER2 assert api.del_file(repo.id, '/', 'dir1', USER) == 0 assert api.unshare_subdir_for_user(repo.id, '/dir2', USER, USER2) == 0 time.sleep(1) assert api.get_shared_repo_by_path(repo.id, '/dir1', USER2) is None assert api.get_shared_repo_by_path(repo.id, '/dir2', USER2) is None
def del_repo_files(repo_id): api.del_file(repo_id, '/', file_name, USER) api.del_file(repo_id, '/', file_name_not_replaced, USER) api.del_file(repo_id, '/', 'subdir', USER) api.del_file(repo_id, '/', resumable_file_name, USER)
def delete_file(self): seafile_api.del_file(self.repo_id, self.parent_dir, self.file_name, self.username)
def post(self, request): """ Only support move folder. Permission checking: User with 'rw' permission for src/dst folder. """ src_repo_id = request.data.get('src_repo_id', None) src_parent_dir = request.data.get('src_parent_dir', None) src_folder_name = request.data.get('src_dirent_name', None) dst_repo_id = request.data.get('dst_repo_id', None) dst_parent_dir = request.data.get('dst_parent_dir', None) # argument check if not src_repo_id: error_msg = 'src_repo_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not src_parent_dir: error_msg = 'src_parent_dir invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not src_folder_name: error_msg = 'src_dirent_name invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not dst_repo_id: error_msg = 'dst_repo_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not dst_parent_dir: error_msg = 'dst_parent_dir invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if src_repo_id == dst_repo_id and src_parent_dir == dst_parent_dir: error_msg = _('Invalid destination path') return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if len(dst_parent_dir + src_folder_name) > MAX_PATH: error_msg = _('Destination path is too long.') return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # src resource check src_repo = seafile_api.get_repo(src_repo_id) if not src_repo: error_msg = 'Library %s not found.' % src_repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) src_folder_path = posixpath.join(src_parent_dir, src_folder_name) dir_id = seafile_api.get_dir_id_by_path(src_repo_id, src_folder_path) if not dir_id: error_msg = 'Folder %s not found.' % src_folder_path return api_error(status.HTTP_404_NOT_FOUND, error_msg) # dst resource check dst_repo = seafile_api.get_repo(dst_repo_id) if not dst_repo: error_msg = 'Library %s not found.' % dst_repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) if not seafile_api.get_dir_id_by_path(dst_repo_id, dst_parent_dir): error_msg = 'Folder %s not found.' % dst_parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check for src folder if check_folder_permission(request, src_repo_id, src_folder_path) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # permission check for dst parent dir if check_folder_permission(request, dst_repo_id, dst_parent_dir) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) ## check if above quota for dst repo if get_repo_owner(request, src_repo_id) != get_repo_owner( request, dst_repo_id): current_size = 0 current_size = seafile_api.get_dir_size(src_repo.store_id, src_repo.version, dir_id) if seafile_api.check_quota(dst_repo_id, current_size) < 0: return api_error(HTTP_443_ABOVE_QUOTA, _("Out of quota.")) username = request.user.username move_folder_with_merge(username, src_repo_id, src_parent_dir, src_folder_name, dst_repo_id, dst_parent_dir, src_folder_name) seafile_api.del_file(src_repo_id, src_parent_dir, src_folder_name, username) return Response({'success': True})
def post(self, request): """ Only support move folder. Permission checking: User with 'rw' permission for src/dst folder. """ src_repo_id = request.data.get('src_repo_id', None) src_parent_dir = request.data.get('src_parent_dir', None) src_folder_name = request.data.get('src_dirent_name', None) dst_repo_id = request.data.get('dst_repo_id', None) dst_parent_dir = request.data.get('dst_parent_dir', None) # argument check if not src_repo_id: error_msg = 'src_repo_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not src_parent_dir: error_msg = 'src_parent_dir invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not src_folder_name: error_msg = 'src_dirent_name invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not dst_repo_id: error_msg = 'dst_repo_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not dst_parent_dir: error_msg = 'dst_parent_dir invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if src_repo_id == dst_repo_id and src_parent_dir == dst_parent_dir: error_msg = _('Invalid destination path') return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if len(dst_parent_dir + src_folder_name) > MAX_PATH: error_msg = _('Destination path is too long.') return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # src resource check src_repo = seafile_api.get_repo(src_repo_id) if not src_repo: error_msg = 'Library %s not found.' % src_repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) src_folder_path = posixpath.join(src_parent_dir, src_folder_name) dir_id = seafile_api.get_dir_id_by_path(src_repo_id, src_folder_path) if not dir_id: error_msg = 'Folder %s not found.' % src_folder_path return api_error(status.HTTP_404_NOT_FOUND, error_msg) # dst resource check dst_repo = seafile_api.get_repo(dst_repo_id) if not dst_repo: error_msg = 'Library %s not found.' % dst_repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) if not seafile_api.get_dir_id_by_path(dst_repo_id, dst_parent_dir): error_msg = 'Folder %s not found.' % dst_parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check for src folder if check_folder_permission(request, src_repo_id, src_folder_path) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # permission check for dst parent dir if check_folder_permission(request, dst_repo_id, dst_parent_dir) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) ## check if above quota for dst repo if get_repo_owner(request, src_repo_id) != get_repo_owner(request, dst_repo_id): current_size = 0 current_size = seafile_api.get_dir_size(src_repo.store_id, src_repo.version, dir_id) if seafile_api.check_quota(dst_repo_id, current_size) < 0: return api_error(HTTP_443_ABOVE_QUOTA, _(u"Out of quota.")) username = request.user.username move_folder_with_merge(username, src_repo_id, src_parent_dir, src_folder_name, dst_repo_id, dst_parent_dir, src_folder_name) seafile_api.del_file(src_repo_id, src_parent_dir, src_folder_name, username) return Response({'success': True})
def remove_folder(self): seafile_api.del_file(self.repo.id, os.path.dirname(self.folder), os.path.basename(self.folder), self.user.username)