def create_exist_file_draft(self, repo, username, file_uuid, file_path): # create drafts dir if any draft_dir_id = seafile_api.get_dir_id_by_path(repo.id, '/Drafts') if draft_dir_id is None: seafile_api.post_dir(repo.id, '/', 'Drafts', username) # check draft file does not exists and copy origin file content to # draft file draft_file_name = get_draft_file_name(repo.id, file_path) draft_file_path = '/Drafts/' + draft_file_name if seafile_api.get_file_id_by_path(repo.id, draft_file_path): raise DraftFileExist # copy file to draft dir seafile_api.copy_file(repo.id, file_uuid.parent_path, file_uuid.filename, repo.id, '/Drafts', draft_file_name, username=username, need_progress=0, synchronous=1) return draft_file_path
def test_subdir_permission_in_virtual_repo(repo, group, permission): api.post_dir(repo.id, '/dir1', 'subdir1', USER) api.post_dir(repo.id, '/dir2', 'subdir2', USER) v_repo_id_1 = api.share_subdir_to_user(repo.id, '/dir1', USER, USER2, permission) v_subdir_repo_id_1 = api.create_virtual_repo(v_repo_id_1, '/subdir1', 'subdir1', 'test_desc', USER, passwd='') assert api.check_permission(v_subdir_repo_id_1, USER2) == permission assert ccnet_api.group_add_member(group.id, USER, USER2) == 0 v_repo_id_2 = api.share_subdir_to_group(repo.id, '/dir2', USER, group.id, permission) v_subdir_repo_id_2 = api.create_virtual_repo(v_repo_id_2, '/subdir2', 'subdir2', 'test_desc', USER, passwd='') assert api.check_permission(v_subdir_repo_id_2, USER2) == permission assert api.unshare_subdir_for_user(repo.id, '/dir1', USER, USER2) == 0 assert api.unshare_subdir_for_group(repo.id, '/dir2', USER, group.id) == 0
def test_create_in_encrypted_lib_with_invalid_password(self): password = randstring(8) encrypted_repo_id = seafile_api.create_repo( 'encrypted_repo_name', '', self.user_name, password) dirname = randstring(8) seafile_api.post_dir(repo_id=encrypted_repo_id, parent_dir='/', dirname=dirname, username=self.user_name) self.login_as(self.user) url = reverse("api2-dir-sub-repo", args=[encrypted_repo_id]) # test invalid password argument args = "?p=/%s&name=%s&invalid_password=%s" % (dirname, dirname, password) resp = self.client.get(url + args) self.assertEqual(400, resp.status_code) # test wrong password args = "?p=/%s&name=%s&password=%s" % (dirname, dirname, 'invalid_password') resp = self.client.get(url + args) self.assertEqual(400, resp.status_code) json_resp = json.loads(resp.content) assert json_resp['error_msg'] == 'Wrong password' self.remove_repo(encrypted_repo_id)
def test_create_in_encrypted_lib_with_invalid_password(self): password = randstring(8) encrypted_repo_id = seafile_api.create_repo('encrypted_repo_name', '', self.user_name, password) dirname = randstring(8) seafile_api.post_dir(repo_id=encrypted_repo_id, parent_dir='/', dirname=dirname, username=self.user_name) self.login_as(self.user) url = reverse("api2-dir-sub-repo", args=[encrypted_repo_id]) # test invalid password argument args = "?p=/%s&name=%s&invalid_password=%s" % (dirname, dirname, password) resp = self.client.get(url + args) self.assertEqual(400, resp.status_code) # test wrong password args = "?p=/%s&name=%s&password=%s" % (dirname, dirname, 'invalid_password') resp = self.client.get(url + args) self.assertEqual(400, resp.status_code) json_resp = json.loads(resp.content) assert json_resp['error_msg'] == 'Wrong password' self.remove_repo(encrypted_repo_id)
def test_get_dirents(self): username = self.user.username dir_name = randstring(6) file_name = randstring(6) seafile_api.post_dir(self.repo_id, self.folder_path, dir_name, username) seafile_api.post_empty_file(self.repo_id, self.folder_path, file_name, username) self.login_as(self.admin) token = self._add_dir_share_link() url = reverse('api-v2.1-admin-share-link-dirents', args=[token]) resp = self.client.get(url) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert json_resp[0]['is_dir'] == True assert dir_name in json_resp[0]['obj_name'] assert json_resp[1]['is_dir'] == False assert file_name in json_resp[1]['obj_name'] self._remove_share_link(token)
def repo(): repo = create_and_get_repo( 'test_repo_{}'.format(randstring(10)), '', USER, passwd=None ) try: seafile_api.post_dir(repo.id, '/', 'dir1', USER) seafile_api.post_dir(repo.id, '/', 'dir2', USER) yield repo finally: if seafile_api.get_repo(repo.id): # The repo may be deleted in the test case seafile_api.remove_repo(repo.id)
def test_can_get_dir_detail(self): seafile_api.post_dir(self.repo_id, self.folder_path, randstring(3), self.user_name) seafile_api.post_empty_file(self.repo_id, self.folder_path, randstring(3), self.user_name) self.login_as(self.user) resp = self.client.get(self.url + '?path=%s' % self.folder_path) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert json_resp['name'] == self.folder_name
def new_dir(repo_id, parent_dir, dirent_name, username): """ Create a new dir with ajax. """ result = {} content_type = 'application/json; charset=utf-8' # create new dirent try: seafile_api.post_dir(repo_id, parent_dir, dirent_name, username) except SearpcError, e: result['error'] = str(e) return HttpResponse(json.dumps(result), status=500, content_type=content_type)
def post(self, request, org_id, format=None): """Create a file draft if the user has read-write permission to the origin file """ # argument check repo_id = request.data.get('repo_id', '') if not repo_id: error_msg = 'repo_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_path = request.data.get('file_path', '') if not file_path: error_msg = 'file_path invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # 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, file_path) if not file_id: error_msg = 'File %s not found.' % file_path return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check perm = check_folder_permission(request, repo_id, file_path) if perm != PERMISSION_READ_WRITE: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) username = request.user.username # create drafts dir if does not exist draft_dir_id = seafile_api.get_dir_id_by_path(repo_id, '/Drafts') if draft_dir_id is None: seafile_api.post_dir(repo_id, '/', 'Drafts', username) # create draft try: d = Draft.objects.add(username, repo, file_path, file_id=file_id) return Response(d.to_dict()) except DraftFileExist: return api_error(status.HTTP_409_CONFLICT, 'Draft already exists.') except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
def test_can_get_with_recursive_and_file_type_parameter(self): # create a sub folder new_dir_name = randstring(6) seafile_api.post_dir(self.repo_id, self.folder_path, new_dir_name, self.user_name) self.login_as(self.user) resp = self.client.get(self.url + '?recursive=1&t=f') self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert len(json_resp['dirent_list']) == 1 assert json_resp['dirent_list'][0]['type'] == 'file' assert json_resp['dirent_list'][0]['name'] == self.file_name
def test_file_operation(): t_repo_version = 1 t_repo_id1 = api.create_repo('test_file_operation1', '', USER, passwd=None) create_the_file() assert api.post_file(t_repo_id1, file_path, '/', file_name, USER) == 0 assert api.post_dir(t_repo_id1, '/', dir_name, USER) == 0 #test search files file_list = api.search_files(t_repo_id1, "test") assert len(file_list) == 2 assert file_list[0].path == "/test.txt" assert file_list[0].is_dir == False assert file_list[1].path == "/test_dir" assert file_list[1].is_dir == True file_list = api.search_files(t_repo_id1, "dir") assert len(file_list) == 1 assert file_list[0].path == "/test_dir" assert file_list[0].is_dir == True file_list = api.search_files(t_repo_id1, "DiR") assert len(file_list) == 1 assert file_list[0].path == "/test_dir" assert file_list[0].is_dir == True api.remove_repo(t_repo_id1)
def create_collection(self, name): """Create a new collection as member of self. See DAVResource.createCollection() """ assert not "/" in name 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) if not seafile_api.is_valid_filename(self.repo.id, name): raise DAVError(HTTP_BAD_REQUEST) seafile_api.post_dir(self.repo.id, self.rel_path, name, self.username)
def createCollection(self, name): """Create a new collection as member of self. See DAVResource.createCollection() """ assert not "/" in name 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) if not seafile_api.is_valid_filename(self.repo.id, name): raise DAVError(HTTP_BAD_REQUEST) seafile_api.post_dir(self.repo.id, self.rel_path, name, self.username)
def post(self, request, repo_id, format=None): """ create 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) parent_dir = request.GET.get('parent_dir', '/') if not parent_dir: error_msg = 'parent_dir invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if parent_dir[-1] != '/': parent_dir = parent_dir + '/' dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir) if not dir_id: error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) obj_name = request.data.get('obj_name', None) if not obj_name or not seafile_api.is_valid_filename( repo_id, obj_name): error_msg = 'obj_name invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) obj_name = check_filename_with_rename(repo_id, parent_dir, obj_name) username = request.user.username try: seafile_api.post_dir(repo_id, parent_dir, obj_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) dirent_path = posixpath.join(parent_dir, obj_name) dirent = seafile_api.get_dirent_by_path(repo_id, dirent_path) dirent_info = get_dirent_info(dirent) return Response(dirent_info)
def create_exist_file_draft(self, repo, username, file_uuid, file_path): # create drafts dir if does not exist draft_dir_id = seafile_api.get_dir_id_by_path(repo.id, '/Drafts') if draft_dir_id is None: seafile_api.post_dir(repo.id, '/', 'Drafts', username) # check draft file does not exists and copy origin file content to # draft file draft_file_name = get_draft_file_name(repo.id, file_path) draft_file_path = '/Drafts/' + draft_file_name try: # Determine if there is a draft of the file d = self.get(origin_file_uuid=file_uuid.uuid) except Draft.DoesNotExist: try: # Determine if there is a draft with the same name as # the generated draft file path d_2 = self.get(origin_repo_id=repo.id, draft_file_path=draft_file_path) d_2.delete(operator=username) except Draft.DoesNotExist: pass # copy file to draft dir seafile_api.copy_file(repo.id, file_uuid.parent_path, file_uuid.filename, repo.id, '/Drafts', draft_file_name, username=username, need_progress=0, synchronous=1) return draft_file_path if d: file_id = seafile_api.get_file_id_by_path(repo.id, d.draft_file_path) # If the database entry exists and the draft file exists, # then raise DraftFileExist if file_id: raise DraftFileExist # If the database entry exists and the draft file does not exist, # delete the database entry else: d.delete(operator=username) # copy file to draft dir seafile_api.copy_file(repo.id, file_uuid.parent_path, file_uuid.filename, repo.id, '/Drafts', draft_file_name, username=username, need_progress=0, synchronous=1) return draft_file_path
def post(self, request, repo_id, format=None): """ create 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) parent_dir = request.GET.get('parent_dir', '/') if not parent_dir: error_msg = 'parent_dir invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if parent_dir[-1] != '/': parent_dir = parent_dir + '/' dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir) if not dir_id: error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) obj_name = request.data.get('obj_name', None) if not obj_name or not seafile_api.is_valid_filename(repo_id, obj_name): error_msg = 'obj_name invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) obj_name = check_filename_with_rename(repo_id, parent_dir, obj_name) username = request.user.username try: seafile_api.post_dir(repo_id, parent_dir, obj_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) dirent_path = posixpath.join(parent_dir, obj_name) dirent = seafile_api.get_dirent_by_path(repo_id, dirent_path) dirent_info = get_dirent_info(dirent) return Response(dirent_info)
def test_can_copy_file_folder_with_dst(self, mock_can_view_sys_admin_repo): mock_can_view_sys_admin_repo.return_value = True dst_repo_id = seafile_api.create_repo(name='test-repo', desc='', username=self.user_name, passwd=None) dst_dir_name = randstring(6) dst_dir = '/' + dst_dir_name seafile_api.post_dir(dst_repo_id, '/', dst_dir_name, self.user_name) dst_init_num = self.get_file_folder_num(self.user_name, dst_repo_id, dst_dir) self.login_as(self.admin) dst_url = reverse('api-v2.1-admin-library-dirents', args=[dst_repo_id]) + '?parent_dir=%s' % dst_dir ## copy file data = {'dst_repo_id': dst_repo_id, 'dst_dir': dst_dir} resp = self.client.put(self.url + '?path=%s' % (self.file_path), json.dumps(data), 'application/json') self.assertEqual(200, resp.status_code) # length of dst library's dirent list will be dst_init_num + 1 resp = self.client.get(dst_url) json_resp = json.loads(resp.content) assert len(json_resp['dirent_list']) == dst_init_num + 1 ## copy folder resp = self.client.put(self.url + '?path=%s' % (self.folder_path), json.dumps(data), 'application/json') self.assertEqual(200, resp.status_code) # length of dst library's dirent list will be dst_init_num + 2 resp = self.client.get(dst_url) json_resp = json.loads(resp.content) assert len(json_resp['dirent_list']) == dst_init_num + 2 self.remove_repo(dst_repo_id)
def test_can_create_in_encrypted_lib(self): password = randstring(8) encrypted_repo_id = seafile_api.create_repo( 'encrypted_repo_name', '', self.user_name, password) dirname = randstring(8) seafile_api.post_dir(repo_id=encrypted_repo_id, parent_dir='/', dirname=dirname, username=self.user_name) self.login_as(self.user) url = reverse("api2-dir-sub-repo", args=[encrypted_repo_id]) args = "?p=/%s&name=%s&password=%s" % (dirname, dirname, password) resp = self.client.get(url + args) json_resp = json.loads(resp.content) assert len(json_resp['sub_repo_id']) == 36 self.remove_repo(encrypted_repo_id)
def post(self, request, repo_id, format=None): """ create file/folder in a library """ if not request.user.admin_permissions.can_manage_library(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') parent_dir = request.GET.get('parent_dir', '/') parent_dir = normalize_dir_path(parent_dir) dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir) if not dir_id: error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) obj_name = request.data.get('obj_name', None) if not obj_name or not is_valid_dirent_name(obj_name): error_msg = 'obj_name invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) is_file = request.data.get('is_file', 'false') is_file = is_file.lower() if is_file not in ('true', 'false'): error_msg = 'is_file invalid.' username = request.user.username obj_name = check_filename_with_rename(repo_id, parent_dir, obj_name) try: if is_file == 'true': seafile_api.post_empty_file(repo_id, parent_dir, obj_name, username) else: seafile_api.post_dir(repo_id, parent_dir, obj_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) dirent_path = posixpath.join(parent_dir, obj_name) dirent = seafile_api.get_dirent_by_path(repo_id, dirent_path) dirent_info = get_dirent_info(dirent) return Response(dirent_info)
def test_download_sub_dir_in_shared_dir(self): username = self.user.username dir_name = randstring(6) seafile_api.post_dir(self.repo_id, self.folder_path, dir_name, username) self.login_as(self.admin) token = self._add_dir_share_link() url = reverse('api-v2.1-admin-share-link-download', args=[token]) resp = self.client.get(url + '?path=/%s&type=folder' % dir_name) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert '8082' in json_resp['download_link'] assert 'zip' in json_resp['download_link'] self._remove_share_link(token)
def test_can_get_download_multi_zip_token(self): # create another folder for download multi another_folder_name = 'another_folder_name' seafile_api.post_dir(repo_id=self.repo.id, parent_dir='/', dirname=another_folder_name, username=self.user.username) self.login_as(self.user) parent_dir = '/' folder_name = self.folder_name url = self.url + '?parent_dir=%s&dirents=%s&dirents=%s' % (parent_dir, folder_name, another_folder_name) resp = self.client.get(url) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert len(json_resp['zip_token']) == 36
def test_can_create_in_encrypted_lib(self): password = randstring(8) encrypted_repo_id = seafile_api.create_repo('encrypted_repo_name', '', self.user_name, password) dirname = randstring(8) seafile_api.post_dir(repo_id=encrypted_repo_id, parent_dir='/', dirname=dirname, username=self.user_name) self.login_as(self.user) url = reverse("api2-dir-sub-repo", args=[encrypted_repo_id]) args = "?p=/%s&name=%s&password=%s" % (dirname, dirname, password) resp = self.client.get(url + args) json_resp = json.loads(resp.content) assert len(json_resp['sub_repo_id']) == 36 self.remove_repo(encrypted_repo_id)
def test_can_get_download_multi_zip_token(self): # create another folder for download multi another_folder_name = 'another_folder_name' seafile_api.post_dir(repo_id=self.repo.id, parent_dir='/', dirname=another_folder_name, username=self.user.username) self.login_as(self.user) parent_dir = '/' folder_name = self.folder_name url = self.url + '?parent_dir=%s&dirents=%s&dirents=%s' % ( parent_dir, folder_name, another_folder_name) resp = self.client.get(url) self.assertEqual(200, resp.status_code) json_resp = json.loads(resp.content) assert len(json_resp['zip_token']) == 36
def post(self, request, repo_id, format=None): """ create file/folder in a library """ parent_dir = request.GET.get('parent_dir', '/') parent_dir = normalize_dir_path(parent_dir) dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir) if not dir_id: error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) obj_name = request.data.get('obj_name', None) if not obj_name or not is_valid_dirent_name(obj_name): error_msg = 'obj_name invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) is_file = request.data.get('is_file', 'false') is_file = is_file.lower() if is_file not in ('true', 'false'): error_msg = 'is_file invalid.' username = request.user.username obj_name = check_filename_with_rename(repo_id, parent_dir, obj_name) try: if is_file == 'true': seafile_api.post_empty_file(repo_id, parent_dir, obj_name, username) else: seafile_api.post_dir(repo_id, parent_dir, obj_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) dirent_path = posixpath.join(parent_dir, obj_name) dirent = seafile_api.get_dirent_by_path(repo_id, dirent_path) dirent_info = get_dirent_info(dirent) return Response(dirent_info)
def post(self, request, repo_id, format=None): """ Create, rename, revert dir. Permission checking: 1. create: user with 'rw' permission for current dir's parent dir; 2. rename: user with 'rw' permission for current dir; 3. revert: user with 'rw' permission for current dir's parent dir; """ # argument check path = request.GET.get('p', None) if not path or path[0] != '/': error_msg = 'p invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if path == '/': error_msg = 'Can not operate root dir.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) operation = request.data.get('operation', None) if not operation: error_msg = 'operation invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) operation = operation.lower() if operation not in ('mkdir', 'rename', 'revert'): error_msg = "operation can only be 'mkdir', 'rename' or 'revert'." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # 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) path = path.rstrip('/') username = request.user.username parent_dir = os.path.dirname(path) if operation == 'mkdir': # permission check if check_folder_permission(request, repo_id, parent_dir) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # resource check parent_dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir) if not parent_dir_id: error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) new_dir_name = os.path.basename(path) if not is_valid_dirent_name(new_dir_name): return api_error(status.HTTP_400_BAD_REQUEST, 'name invalid.') new_dir_name = check_filename_with_rename(repo_id, parent_dir, new_dir_name) try: seafile_api.post_dir(repo_id, parent_dir, new_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) new_dir_path = posixpath.join(parent_dir, new_dir_name) dir_info = self.get_dir_info(repo_id, new_dir_path) resp = Response(dir_info) return resp if operation == 'rename': # 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) # permission check if check_folder_permission(request, repo_id, path) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) old_dir_name = os.path.basename(path) new_dir_name = request.data.get('newname', None) if not new_dir_name: error_msg = 'newname invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not is_valid_dirent_name(new_dir_name): return api_error(status.HTTP_400_BAD_REQUEST, 'name invalid.') if new_dir_name == old_dir_name: dir_info = self.get_dir_info(repo_id, path) resp = Response(dir_info) return resp try: # rename duplicate name new_dir_name = check_filename_with_rename( repo_id, parent_dir, new_dir_name) # rename dir seafile_api.rename_file(repo_id, parent_dir, old_dir_name, new_dir_name, username) new_dir_path = posixpath.join(parent_dir, new_dir_name) dir_info = self.get_dir_info(repo_id, new_dir_path) resp = Response(dir_info) return resp except SearpcError, e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
def test_file_operation(): t_repo_version = 1 t_repo_id1 = api.create_repo('test_file_operation1', '', USER, passwd=None) create_the_file() # test post_file assert api.post_file(t_repo_id1, file_path, '/', file_name, USER) == 0 t_file_id = api.get_file_id_by_path(t_repo_id1, '/' + file_name) t_file_size = len(file_content) assert t_file_size == api.get_file_size(t_repo_id1, t_repo_version, t_file_id) # test post_dir assert api.post_dir(t_repo_id1, '/', dir_name, USER) == 0 # test copy_file (synchronize) t_copy_file_result1 = api.copy_file(t_repo_id1, '/', file_name, t_repo_id1, '/', new_file_name, USER, 0, 1) assert t_copy_file_result1 assert t_copy_file_result1.task_id is None assert not t_copy_file_result1.background t_file_id = api.get_file_id_by_path(t_repo_id1, '/' + new_file_name) assert t_file_size == api.get_file_size(t_repo_id1, t_repo_version, t_file_id) # test copy_file (asynchronous) t_repo_id2 = api.create_repo('test_file_operation2', '', USER, passwd=None) usage = api.get_user_self_usage(USER) api.set_user_quota(USER, usage + 1) t_copy_file_result2 = api.copy_file(t_repo_id1, '/', file_name, t_repo_id2, '/', file_name, USER, 1, 0) assert t_copy_file_result2 assert t_copy_file_result2.background while True: time.sleep(0.1) t_copy_task = api.get_copy_task(t_copy_file_result2.task_id) assert t_copy_task.failed assert t_copy_task.failed_reason == 'Quota is full' if t_copy_task.failed: break api.set_user_quota(USER, -1) t_copy_file_result2 = api.copy_file(t_repo_id1, '/', file_name, t_repo_id2, '/', file_name, USER, 1, 0) assert t_copy_file_result2 assert t_copy_file_result2.task_id assert t_copy_file_result2.background while True: time.sleep(0.1) t_copy_task = api.get_copy_task(t_copy_file_result2.task_id) if t_copy_task.successful: break t_file_id = api.get_file_id_by_path(t_repo_id2, '/' + file_name) assert t_file_size == api.get_file_size(t_repo_id2, t_repo_version, t_file_id) # test move_file (synchronize) t_move_file_info1 = api.get_dirent_by_path(t_repo_id1, '/' + new_file_name) t_move_file_result1 = api.move_file(t_repo_id1, '/', new_file_name, t_repo_id1, '/' + dir_name, new_file_name, 1, USER, 0, 1) assert t_move_file_result1 t_move_file_info2 = api.get_dirent_by_path( t_repo_id1, '/' + dir_name + '/' + new_file_name) assert t_move_file_info1.mtime == t_move_file_info2.mtime t_file_id = api.get_file_id_by_path(t_repo_id1, '/' + new_file_name) assert t_file_id is None # test move_file (synchronize) t_move_file_result1 = api.move_file(t_repo_id1, '/' + dir_name, new_file_name, t_repo_id1, '/', new_file_name_2, 1, USER, 0, 1) assert t_move_file_result1 t_file_id = api.get_file_id_by_path(t_repo_id1, '/' + dir_name + '/' + new_file_name) assert t_file_id is None # test move_file (asynchronous) usage = api.get_user_self_usage(USER) api.set_user_quota(USER, usage + 1) t_move_file_result2 = api.move_file(t_repo_id1, '/', file_name, t_repo_id2, '/', new_file_name, 1, USER, 1, 0) assert t_move_file_result2 assert t_move_file_result2.task_id assert t_move_file_result2.background while True: time.sleep(0.1) t_move_task = api.get_copy_task(t_move_file_result2.task_id) assert t_move_task.failed assert t_move_task.failed_reason == 'Quota is full' if t_move_task.failed: break api.set_user_quota(USER, -1) t_move_file_result2 = api.move_file(t_repo_id1, '/', file_name, t_repo_id2, '/', new_file_name, 1, USER, 1, 0) assert t_move_file_result2 assert t_move_file_result2.task_id assert t_move_file_result2.background while True: time.sleep(0.1) t_move_task = api.get_copy_task(t_move_file_result2.task_id) if t_move_task.successful: break t_file_id = api.get_file_id_by_path(t_repo_id2, '/' + new_file_name) assert t_file_size == api.get_file_size(t_repo_id2, t_repo_version, t_file_id) # test post_empty_file assert api.post_empty_file(t_repo_id1, '/' + dir_name, empty_file_name, USER) == 0 t_file_id = api.get_file_id_by_path(t_repo_id1, '/' + dir_name + '/' + empty_file_name) assert api.get_file_size(t_repo_id1, t_repo_version, t_file_id) == 0 # test rename_file assert api.rename_file(t_repo_id1, '/' + dir_name, empty_file_name, new_empty_file_name, USER) == 0 #test put_file t_new_file_id = api.put_file(t_repo_id1, file_path, '/' + dir_name, new_empty_file_name, USER, None) assert t_new_file_id # test get_file_revisions t_commit_list = api.get_file_revisions(t_repo_id2, None, '/' + file_name, 2) assert t_commit_list assert len(t_commit_list) == 2 assert t_commit_list[0].creator_name == USER # test del_file assert api.del_file(t_repo_id2, '/', file_name, USER) == 0 # test get_deleted t_deleted_file_list = api.get_deleted(t_repo_id2, 1) assert t_deleted_file_list assert len(t_deleted_file_list) == 2 assert t_deleted_file_list[0].obj_name == file_name assert t_deleted_file_list[0].basedir == '/' # test del a non-exist file. should return 0. assert api.del_file(t_repo_id2, '/', file_name, USER) == 0 assert api.del_file(t_repo_id1, '/' + dir_name, new_empty_file_name, USER) == 0 assert api.del_file(t_repo_id1, '/' + dir_name, new_file_name, USER) == 0 assert api.del_file(t_repo_id2, '/', new_file_name, USER) == 0 assert api.del_file(t_repo_id1, '/', new_file_name_2, USER) == 0 time.sleep(1) api.remove_repo(t_repo_id1)
def create_test_dir(repo, dir_name): parent_dir = '/' api.post_dir(repo.id,parent_dir,dir_name,USER)
def create_folder(self, **kwargs): seafile_api.post_dir(**kwargs) return kwargs['parent_dir'] + kwargs['dirname']
def _test_under_path(path): repo = TEST_REPO path = path.rstrip('/') #sdir = repo.get_dir('/') parent_dir = '/' if path: dirs = [p for p in path.split('/') if p] for d in dirs: api.post_dir(repo.get('id'), parent_dir, d, USER) parent_dir = parent_dir + d + '/' entries = davclient.repo_listdir(repo, path) self.assertEmpty(entries) # create a folder from webapi and list it in webdav dirname = 'folder-%s' % randstring() api.post_dir(repo.get('id'), parent_dir, dirname, USER) entries = davclient.repo_listdir(repo, parent_dir) self.assertHasLen(entries, 1) sfolder = entries[0] self.assertEqual(dav_basename(sfolder), dirname) # create a file from webapi and list it in webdav testfpath = os.path.join(os.path.dirname(__file__), 'data', 'test.txt') with open(testfpath, 'rb') as fp: testfcontent = fp.read() fname = 'uploaded-file-%s.txt' % randstring() api.post_file(repo.get('id'), testfpath, parent_dir, fname, USER) entries = davclient.repo_listdir(repo, parent_dir) self.assertHasLen(entries, 2) downloaded_file = davclient.repo_getfile( repo, posixpath.join(parent_dir, fname)) assert downloaded_file == testfcontent # create a folder through webdav, and check it in webapi dirname = 'another-level1-folder-%s' % randstring(10) davclient.repo_mkdir(repo, parent_dir, dirname) entries = api.list_dir_by_path(repo.get('id'), parent_dir) self.assertHasLen(entries, 3) davdir = [e for e in entries if e.obj_name == dirname][0] self.assertEqual(davdir.obj_name, dirname) # upload a file through webdav, and check it in webapi fname = 'uploaded-file-%s' % randstring() repo_fpath = posixpath.join(parent_dir, fname) davclient.repo_uploadfile(repo, testfpath, repo_fpath) entries = api.list_dir_by_path(repo.get('id'), parent_dir) self.assertHasLen(entries, 4) # remove a dir through webdav self.assertIn(dirname, [dirent.obj_name for dirent in \ api.list_dir_by_path(repo.get('id'), parent_dir)]) davclient.repo_removedir(repo, os.path.join(parent_dir, dirname)) entries = api.list_dir_by_path(repo.get('id'), parent_dir) self.assertHasLen(entries, 3) self.assertNotIn(dirname, [dirent.obj_name for dirent in entries]) # remove a file through webdav self.assertIn(fname, [dirent.obj_name for dirent in \ api.list_dir_by_path(repo.get('id'), parent_dir)]) davclient.repo_removefile(repo, os.path.join(parent_dir, fname)) entries = api.list_dir_by_path(repo.get('id'), parent_dir) self.assertHasLen(entries, 2) self.assertNotIn(fname, [dirent.obj_name for dirent in entries])
def create_folder(self, **kwargs): seafile_api.post_dir(**kwargs) return kwargs["parent_dir"] + kwargs["dirname"]
def post(self, request, format=None): repo_id = request.repo_api_token_obj.repo_id # argument check path = request.GET.get('path', None) if not path or path[0] != '/': error_msg = 'path invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if path == '/': error_msg = 'Can not operate root dir.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) operation = request.data.get('operation', None) if not operation: error_msg = 'operation invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) operation = operation.lower() if operation not in ('mkdir', 'rename', 'revert'): error_msg = "operation can only be 'mkdir', 'rename' or 'revert'." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # 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) path = path.rstrip('/') username = request.user.username parent_dir = os.path.dirname(path) if operation == 'mkdir': # resource check parent_dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir) if not parent_dir_id: error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check if check_folder_permission_by_repo_api(request, repo_id, parent_dir) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) new_dir_name = os.path.basename(path) if not is_valid_dirent_name(new_dir_name): return api_error(status.HTTP_400_BAD_REQUEST, 'name invalid.') retry_count = 0 while retry_count < 10: new_dir_name = check_filename_with_rename( repo_id, parent_dir, new_dir_name) try: seafile_api.post_dir(repo_id, parent_dir, new_dir_name, username) break except SearpcError as e: if str(e) == 'file already exists': retry_count += 1 else: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) new_dir_path = posixpath.join(parent_dir, new_dir_name) dir_info = self.get_dir_info(repo_id, new_dir_path) resp = Response(dir_info) return resp if operation == 'rename': # 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) # permission check if check_folder_permission_by_repo_api(request, repo_id, path) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) old_dir_name = os.path.basename(path) new_dir_name = request.data.get('newname', None) if not new_dir_name: error_msg = 'newname invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not is_valid_dirent_name(new_dir_name): return api_error(status.HTTP_400_BAD_REQUEST, 'name invalid.') if new_dir_name == old_dir_name: dir_info = self.get_dir_info(repo_id, path) resp = Response(dir_info) return resp try: # rename duplicate name new_dir_name = check_filename_with_rename( repo_id, parent_dir, new_dir_name) # rename dir seafile_api.rename_file(repo_id, parent_dir, old_dir_name, new_dir_name, username) new_dir_path = posixpath.join(parent_dir, new_dir_name) dir_info = self.get_dir_info(repo_id, new_dir_path) resp = Response(dir_info) return resp except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if operation == 'revert': commit_id = request.data.get('commit_id', None) if not commit_id: error_msg = 'commit_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if seafile_api.get_dir_id_by_path(repo_id, path): # dir exists in repo if check_folder_permission_by_repo_api(request, repo_id, path) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) else: # dir NOT exists in repo if check_folder_permission_by_repo_api(request, repo_id, '/') != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) try: seafile_api.revert_dir(repo_id, commit_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) return Response({'success': True})
def test_merge_virtual_repo(repo): api.post_dir(repo.id, '/dir1', 'subdir1', USER) api.post_dir(repo.id, '/dir2', 'subdir2', USER) v_repo_id = api.share_subdir_to_user(repo.id, '/dir1', USER, USER2, 'rw') create_test_file() params = {'ret-json': '1'} obj_id = '{"parent_dir":"/"}' create_test_dir(repo, 'test') #test upload file to vritual repo root dir. token = api.get_fileserver_access_token(v_repo_id, obj_id, 'upload', USER2, False) upload_url_base = 'http://127.0.0.1:8082/upload-api/' + token m = MultipartEncoder( fields={ 'parent_dir': '/', 'file': (file_name, open(file_path, 'rb'), 'application/octet-stream') }) response = requests.post(upload_url_base, params=params, data=m, headers={'Content-Type': m.content_type}) assert_upload_response(response, False, False) time.sleep(1.5) repo_size = api.get_repo_size(v_repo_id) assert repo_size == 0 time.sleep(1.5) repo_size = api.get_repo_size(repo.id) assert repo_size == 0 #test resumable upload file to virtual repo root dir parent_dir = '/' headers = { 'Content-Range': 'bytes 0-{}/{}'.format(str(len(chunked_part1_content) - 1), str(total_size)), 'Content-Disposition': 'attachment; filename=\"{}\"'.format(resumable_file_name) } response = request_resumable_upload(chunked_part1_path, headers, upload_url_base, parent_dir, False) assert_resumable_upload_response(response, v_repo_id, resumable_file_name, False) time.sleep(1.5) v_repo_size = api.get_repo_size(v_repo_id) assert v_repo_size == 0 time.sleep(1.5) repo_size = api.get_repo_size(repo.id) assert repo_size == 0 headers = { 'Content-Range': 'bytes {}-{}/{}'.format(str(len(chunked_part1_content)), str(total_size - 1), str(total_size)), 'Content-Disposition': 'attachment; filename=\"{}\"'.format(resumable_file_name) } response = request_resumable_upload(chunked_part2_path, headers, upload_url_base, parent_dir, False) assert_resumable_upload_response(response, v_repo_id, resumable_file_name, True) time.sleep(2.5) v_repo_size = api.get_repo_size(v_repo_id) assert v_repo_size == total_size time.sleep(1.5) repo_size = api.get_repo_size(repo.id) assert repo_size == total_size #test update file to virtual repo. write_file(file_path, file_content) token = api.get_fileserver_access_token(v_repo_id, obj_id, 'update', USER2, False) update_url_base = 'http://127.0.0.1:8082/update-api/' + token m = MultipartEncoder( fields={ 'target_file': '/' + file_name, 'file': (file_name, open(file_path, 'rb'), 'application/octet-stream') }) response = requests.post(update_url_base, data=m, headers={'Content-Type': m.content_type}) assert_update_response(response, False) time.sleep(1.5) v_repo_size = api.get_repo_size(v_repo_id) assert v_repo_size == total_size + file_size time.sleep(1.5) repo_size = api.get_repo_size(repo.id) assert repo_size == total_size + file_size api.del_file(v_repo_id, '/', file_name, USER2) time.sleep(1.5) v_repo_size = api.get_repo_size(v_repo_id) assert v_repo_size == total_size time.sleep(1.5) repo_size = api.get_repo_size(repo.id) assert repo_size == total_size api.del_file(v_repo_id, '/', resumable_file_name, USER2) time.sleep(1.5) v_repo_size = api.get_repo_size(v_repo_id) assert v_repo_size == 0 time.sleep(1.5) repo_size = api.get_repo_size(repo.id) assert repo_size == 0 api.del_file(repo.id, '/dir1', 'subdir1', USER) api.del_file(repo.id, '/dir2', 'subdir1', USER) assert api.unshare_subdir_for_user(repo.id, '/dir1', USER, USER2) == 0 del_local_files()
def test_repo_manipulation(): #test get_system_default_repo_id t_default_repo_id = api.get_system_default_repo_id() assert t_default_repo_id #test create_repo t_repo_id = api.create_repo('test_repo_manipulation', '', USER, passwd=None) assert t_repo_id #test counts_repo t_repo_count = 0 t_repo_count = api.count_repos() assert t_repo_count != 0 #test get_repo ,edit_repo t_new_name = 'n_name' t_new_desc = 'n_desc' t_repo_version = 1 t_repo = api.get_repo(t_repo_id) assert t_repo api.edit_repo(t_repo_id, t_new_name, t_new_desc, USER) t_repo = api.get_repo(t_repo_id) assert t_repo.name == t_new_name and t_repo.desc == t_new_desc #test revert_repo and get_commit t_commit_id_before_changing = t_repo.head_cmmt_id api.post_dir(t_repo_id, '/', 'dir1', USER) t_repo = api.get_repo(t_repo_id) api.revert_repo(t_repo_id, t_commit_id_before_changing, USER) t_repo = api.get_repo(t_repo_id) t_commit_id_after_revert = t_repo.head_cmmt_id t_commit_before_changing = api.get_commit(t_repo_id, t_repo_version, t_commit_id_before_changing) t_commit_after_revert = api.get_commit(t_repo_id, t_repo_version, t_commit_id_after_revert) assert t_commit_before_changing.root_id == t_commit_after_revert.root_id #test is_repo_owner assert api.is_repo_owner(USER, t_repo_id) assert api.is_repo_owner(USER2, t_repo_id) == 0 #test get_repo_owner owner_get = api.get_repo_owner(t_repo_id) assert owner_get == USER #test set_repo_owner api.set_repo_owner(t_repo_id, USER2) assert api.is_repo_owner(USER2, t_repo_id) #test create_enc_repo t_enc_repo_id = '826d1b7b-f110-46f2-8d5e-7b5ac3e11f4d' t_enc_version = 2 t_passwd = '123' magic_and_random_key = api.generate_magic_and_random_key( t_enc_version, t_enc_repo_id, t_passwd) t_magic = magic_and_random_key.magic t_random_key = magic_and_random_key.random_key t_enc_repo_id = api.create_enc_repo(t_enc_repo_id, 'test_encrypted_repo', '', USER, t_magic, t_random_key, t_enc_version) assert t_enc_repo_id == '826d1b7b-f110-46f2-8d5e-7b5ac3e11f4d' #test get_repo_list t_start = -1 t_limit = -1 t_repo_list = api.get_repo_list(t_start, t_limit) assert t_repo_list and len(t_repo_list) t_start = 1 t_limit = 1 t_repo_list = api.get_repo_list(t_start, t_limit) assert t_repo_list and len(t_repo_list) == 1 #test get_owned_repo_list t_repo_list = api.get_owned_repo_list(USER2) assert t_repo_list and len(t_repo_list) #test get_commit_list t_offset = 0 t_limit = 0 t_commit_list = api.get_commit_list(t_repo_id, t_offset, t_limit) assert t_commit_list and len(t_commit_list) == 4 t_offset = 1 t_limit = 1 t_commit_list = api.get_commit_list(t_repo_id, t_offset, t_limit) assert t_commit_list and len(t_commit_list) == 1 #test remove_repo api.remove_repo(t_repo_id) t_repo = api.get_repo(t_repo_id) assert t_repo == None
def post(self, request, repo_id, format=None): repo = seafile_api.get_repo(repo_id) if not repo: return api_error(status.HTTP_404_NOT_FOUND, 'Library not found.') path = request.GET.get('p', '') if not path or path[0] != '/': error_msg = 'p invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if path == '/': error_msg = 'Can not make or rename root dir.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if path[-1] == '/': path = path[:-1] username = request.user.username parent_dir = os.path.dirname(path) operation = request.POST.get('operation', '') if operation.lower() == 'mkdir': 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) create_parents = request.POST.get('create_parents', '').lower() in ('true', '1') if not create_parents: parent_dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir) if not parent_dir_id: error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) new_dir_name = os.path.basename(path) new_dir_name = check_filename_with_rename(repo_id, parent_dir, new_dir_name) try: seafile_api.post_dir(repo_id, parent_dir, new_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) else: if not is_pro_version(): error_msg = 'Feature not supported.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) try: seafile_api.mkdir_with_parents(repo_id, '/', path[1:], 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': resp = reloaddir(request, repo, parent_dir) else: resp = Response({'success': True}) return resp elif operation.lower() == 'rename': 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) if check_folder_permission(request, repo.id, path) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) parent_dir = os.path.dirname(path) old_dir_name = os.path.basename(path) newname = request.POST.get('newname', '') if not newname: error_msg = 'newname invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if newname == old_dir_name: return Response({'success': True}) try: # rename duplicate name checked_newname = check_filename_with_rename(repo_id, parent_dir, newname) # rename dir seafile_api.rename_file(repo_id, parent_dir, old_dir_name, checked_newname, username) return Response({'success': True}) except SearpcError, e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
def post(self, request, repo_id, format=None): """ Create, rename, revert dir. Permission checking: 1. create: user with 'rw' permission for current dir's parent dir; 2. rename: user with 'rw' permission for current dir; 3. revert: user with 'rw' permission for current dir's parent dir; """ # argument check path = request.GET.get('p', None) if not path or path[0] != '/': error_msg = 'p invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if path == '/': error_msg = 'Can not operate root dir.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) operation = request.data.get('operation', None) if not operation: error_msg = 'operation invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) operation = operation.lower() if operation not in ('mkdir', 'rename', 'revert'): error_msg = "operation can only be 'mkdir', 'rename' or 'revert'." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # 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) path = path.rstrip('/') username = request.user.username parent_dir = os.path.dirname(path) if operation == 'mkdir': # permission check if check_folder_permission(request, repo_id, parent_dir) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # resource check parent_dir_id = seafile_api.get_dir_id_by_path(repo_id, parent_dir) if not parent_dir_id: error_msg = 'Folder %s not found.' % parent_dir return api_error(status.HTTP_404_NOT_FOUND, error_msg) new_dir_name = os.path.basename(path) new_dir_name = check_filename_with_rename(repo_id, parent_dir, new_dir_name) try: seafile_api.post_dir(repo_id, parent_dir, new_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) new_dir_path = posixpath.join(parent_dir, new_dir_name) dir_info = self.get_dir_info(repo_id, new_dir_path) resp = Response(dir_info) return resp if operation == 'rename': # 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) # permission check if check_folder_permission(request, repo_id, path) != 'rw': error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) old_dir_name = os.path.basename(path) new_dir_name = request.data.get('newname', None) if not new_dir_name: error_msg = 'newname invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if new_dir_name == old_dir_name: dir_info = self.get_dir_info(repo_id, path) resp = Response(dir_info) return resp try: # rename duplicate name new_dir_name = check_filename_with_rename(repo_id, parent_dir, new_dir_name) # rename dir seafile_api.rename_file(repo_id, parent_dir, old_dir_name, new_dir_name, username) new_dir_path = posixpath.join(parent_dir, new_dir_name) dir_info = self.get_dir_info(repo_id, new_dir_path) resp = Response(dir_info) return resp except SearpcError, e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
def setUp(self): seafile_api.post_dir(self.repo.id, '/', 'Drafts', self.user.username)
def test_file_property_and_dir_listing(): t_repo_version = 1 t_repo_id = api.create_repo('test_file_property_and_dir_listing', '', USER, passwd=None) create_the_file() api.post_file(t_repo_id, file_path, '/', file_name, USER) api.post_dir(t_repo_id, '/', dir_name, USER) api.post_file(t_repo_id, file_path, '/' + dir_name, file_name, USER) #test is_valid_filename t_valid_file_name = 'valid_filename' t_invalid_file_name = '/invalid_filename' assert api.is_valid_filename(t_repo_id, t_valid_file_name) assert api.is_valid_filename(t_repo_id, t_invalid_file_name) == 0 #test get_file_id_by_path t_file_id = api.get_file_id_by_path(t_repo_id, '/test.txt') assert t_file_id #test get_dir_id_by_path t_dir_id = api.get_dir_id_by_path(t_repo_id, '/test_dir') assert t_dir_id #test get_file_size t_file_size = len(file_content) assert t_file_size == api.get_file_size(t_repo_id, t_repo_version, t_file_id) #test get_dir_size t_dir_size = len(file_content) assert t_dir_size == api.get_dir_size(t_repo_id, t_repo_version, t_dir_id) #test get_file_count_info_by_path t_file_count_info = api.get_file_count_info_by_path(t_repo_id, '/') assert t_file_count_info.file_count == 2 assert t_file_count_info.dir_count == 1 assert t_file_count_info.size == t_file_size + t_dir_size #test get_file_id_by_commit_and_path t_file_id_tmp = t_file_id t_repo = api.get_repo(t_repo_id) assert t_repo t_commit_id = t_repo.head_cmmt_id t_file_id = api.get_file_id_by_commit_and_path(t_repo_id, t_commit_id, '/test.txt') assert t_file_id == t_file_id_tmp #test get_dirent_by_path std_file_mode = 0100000 | 0644 t_dirent_obj = api.get_dirent_by_path(t_repo_id, '/test.txt') assert t_dirent_obj assert t_dirent_obj.obj_id == t_file_id assert t_dirent_obj.obj_name == 'test.txt' assert t_dirent_obj.mode == std_file_mode assert t_dirent_obj.version == t_repo_version assert t_dirent_obj.size == t_file_size assert t_dirent_obj.modifier == USER #test list_file_by_file_id t_block_list = api.list_file_by_file_id(t_repo_id, t_file_id) assert t_block_list #test list_blocks_by_file_id t_block_list = api.list_blocks_by_file_id(t_repo_id, t_file_id) assert t_block_list #test list_dir_by_dir_id t_dir_list = api.list_dir_by_dir_id(t_repo_id, t_dir_id) assert len(t_dir_list) == 1 #test list_dir_by_path t_dir_list = api.list_dir_by_path(t_repo_id, '/test_dir') assert len(t_dir_list) == 1 #test get_dir_id_by_commit_and_path t_dir_id = api.get_dir_id_by_commit_and_path(t_repo_id, t_commit_id, '/test_dir') assert t_dir_id #test list_dir_by_commit_and_path t_dir_list = api.list_dir_by_commit_and_path(t_repo_id, t_commit_id, '/test_dir') assert len(t_dir_list) == 1 #test list_dir_with_perm t_dir_list = api.list_dir_with_perm(t_repo_id, '/test_dir', t_dir_id, USER) assert len(t_dir_list) == 1 #test mkdir_with_parent api.mkdir_with_parents(t_repo_id, '/test_dir', 'test_subdir', USER) t_dir_id = api.get_dir_id_by_path(t_repo_id, '/test_dir/test_subdir') assert t_dir_id #test get_total_storage t_total_size = api.get_total_storage() t_repo_size = api.get_repo_size(t_repo_id) assert t_total_size == t_repo_size #get_total_file_number time.sleep(1) assert api.get_total_file_number() == 2 api.remove_repo(t_repo_id)
def test_zip_download(): create_test_files() t_repo_id = api.create_repo('test_zip_download', '', USER) base_url = 'http://127.0.0.1:8082/' #test zip download dir dir_name = 'dir' api.post_dir(t_repo_id, '/', dir_name, USER) api.post_file(t_repo_id, file1_path, '/dir', file1_name, USER) api.post_file(t_repo_id, file2_path, '/dir', file2_name, USER) dir_id = api.get_dir_id_by_path(t_repo_id, '/dir') obj_id = {'obj_id': dir_id, 'dir_name': dir_name, 'is_windows': 0} obj_id_json_str = json.dumps(obj_id) token = api.get_fileserver_access_token(t_repo_id, obj_id_json_str, 'download-dir', USER) time.sleep(1) download_url = base_url + 'zip/' + token response = requests.get(download_url) assert response.status_code == 200 download_zipfile_path = download_dir_path + '/dir.zip' with open(download_zipfile_path, 'wb') as fp: fp.write(response.content) zipFile = zipfile.ZipFile(download_zipfile_path) for name in zipFile.namelist(): zipFile.extract(name, download_dir_path) zipFile.close() assert os.path.exists(download_dir_path + '/dir.zip') assert os.path.exists(download_dir_path + '/dir') assert os.path.exists(download_dir_path + '/dir' + '/file1.txt') assert os.path.exists(download_dir_path + '/dir' + '/file2.txt') with open(download_dir_path + '/dir' + '/file1.txt', 'r') as fp1: line = fp1.read() assert line == file1_content with open(download_dir_path + '/dir' + '/file2.txt', 'r') as fp2: line = fp2.read() assert line == file2_content os.remove(download_dir_path + '/dir' + '/file1.txt') os.remove(download_dir_path + '/dir' + '/file2.txt') os.rmdir(download_dir_path + '/dir') os.remove(download_dir_path + '/dir.zip') #test zip download empty dir empty_dir_name = 'empty_dir' api.post_dir(t_repo_id, '/', empty_dir_name, USER) dir_id = api.get_dir_id_by_path(t_repo_id, '/empty_dir') obj_id = {'obj_id': dir_id, 'dir_name': empty_dir_name, 'is_windows': 0} obj_id_json_str = json.dumps(obj_id) token = api.get_fileserver_access_token(t_repo_id, obj_id_json_str, 'download-dir', USER) time.sleep(1) download_url = base_url + 'zip/' + token response = requests.get(download_url) assert response.status_code == 200 download_zipfile_path = download_dir_path + '/empty_dir.zip' with open(download_zipfile_path, 'wb') as fp: fp.write(response.content) zipFile = zipfile.ZipFile(download_zipfile_path) for name in zipFile.namelist(): zipFile.extract(name, download_dir_path) zipFile.close() assert os.path.exists(download_dir_path + '/empty_dir') assert not os.listdir(download_dir_path + '/empty_dir') os.rmdir(download_dir_path + '/empty_dir') os.remove(download_dir_path + '/empty_dir.zip') #test zip download mutliple files api.post_file(t_repo_id, file1_path, '/', file1_name, USER) api.post_file(t_repo_id, file2_path, '/', file2_name, USER) obj_id = { 'parent_dir': '/', 'file_list': [file1_name, file2_name], 'is_windows': 0 } obj_id_json_str = json.dumps(obj_id) token = api.get_fileserver_access_token(t_repo_id, obj_id_json_str, 'download-multi', USER) time.sleep(1) download_url = base_url + 'zip/' + token response = requests.get(download_url) assert response.status_code == 200 download_zipfile_path = download_dir_path + '/multi_files.zip' with open(download_zipfile_path, 'wb') as fp: fp.write(response.content) zipFile = zipfile.ZipFile(download_zipfile_path) for name in zipFile.namelist(): zipFile.extract(name, download_dir_path) zipFile.close() assert os.path.exists(download_dir_path + '/file1.txt') assert os.path.exists(download_dir_path + '/file2.txt') with open(download_dir_path + '/file1.txt', 'r') as fp1: line = fp1.read() assert line == file1_content with open(download_dir_path + '/file2.txt', 'r') as fp2: line = fp2.read() assert line == file2_content os.remove(download_dir_path + '/file1.txt') os.remove(download_dir_path + '/file2.txt') os.remove(download_dir_path + '/multi_files.zip') remove_test_files() api.remove_repo(t_repo_id)