Skip to content

Commit

Permalink
copy_sdoc_images_to_different_repo (#5668)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkywalkerSpace authored Oct 16, 2023
1 parent 7631117 commit dca836a
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
73 changes: 72 additions & 1 deletion seahub/api2/endpoints/repos_batch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2012-2016 Seafile Ltd.
import os
import posixpath
import json
import logging

Expand Down Expand Up @@ -29,12 +30,14 @@
share_repo_to_group_successful
from seahub.utils import is_org_context, send_perm_audit_msg, \
normalize_dir_path, get_folder_permission_recursively, \
normalize_file_path, check_filename_with_rename
normalize_file_path, check_filename_with_rename, get_file_type_and_ext
from seahub.utils.repo import get_repo_owner, get_available_repo_perms, \
parse_repo_perm, get_locked_files_by_dir, get_sub_folder_permission_by_dir

from seahub.views import check_folder_permission
from seahub.settings import MAX_PATH
from seahub.utils.file_types import SEADOC
from seahub.seadoc.utils import copy_sdoc_images_to_different_repo, move_sdoc_images_to_different_repo

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1168,6 +1171,23 @@ def post(self, request):

result = {}
result['task_id'] = res.task_id if res.background else ''

# sdoc image
try:
sdoc_dirents = []
if src_repo_id != dst_repo_id:
for dirent in src_dirents:
filetype, fileext = get_file_type_and_ext(dirent)
if filetype == SEADOC:
sdoc_dirents.append(dirent)
for sdoc_dirent in sdoc_dirents:
src_path = posixpath.join(src_parent_dir, sdoc_dirent)
dst_path = posixpath.join(dst_parent_dir, sdoc_dirent)
copy_sdoc_images_to_different_repo(
src_repo_id, src_path, dst_repo_id, dst_path, username, is_async=True)
except Exception as e:
logger.error(e)

return Response(result)


Expand Down Expand Up @@ -1284,6 +1304,23 @@ def post(self, request):

result = {}
result['task_id'] = res.task_id if res.background else ''

# sdoc image
try:
sdoc_dirents = []
if src_repo_id != dst_repo_id:
for dirent in src_dirents:
filetype, fileext = get_file_type_and_ext(dirent)
if filetype == SEADOC:
sdoc_dirents.append(dirent)
for sdoc_dirent in sdoc_dirents:
src_path = posixpath.join(src_parent_dir, sdoc_dirent)
dst_path = posixpath.join(dst_parent_dir, sdoc_dirent)
move_sdoc_images_to_different_repo(
src_repo_id, src_path, dst_repo_id, dst_path, username, is_async=True)
except Exception as e:
logger.error(e)

return Response(result)


Expand Down Expand Up @@ -1381,6 +1418,23 @@ def post(self, request):

result = {}
result['success'] = True

# sdoc image
try:
sdoc_dirents = []
if src_repo_id != dst_repo_id:
for dirent in src_dirents:
filetype, fileext = get_file_type_and_ext(dirent)
if filetype == SEADOC:
sdoc_dirents.append(dirent)
for sdoc_dirent in sdoc_dirents:
src_path = posixpath.join(src_parent_dir, sdoc_dirent)
dst_path = posixpath.join(dst_parent_dir, sdoc_dirent)
copy_sdoc_images_to_different_repo(
src_repo_id, src_path, dst_repo_id, dst_path, username, is_async=False)
except Exception as e:
logger.error(e)

return Response(result)


Expand Down Expand Up @@ -1497,6 +1551,23 @@ def post(self, request):

result = {}
result['success'] = True

# sdoc image
try:
sdoc_dirents = []
if src_repo_id != dst_repo_id:
for dirent in src_dirents:
filetype, fileext = get_file_type_and_ext(dirent)
if filetype == SEADOC:
sdoc_dirents.append(dirent)
for sdoc_dirent in sdoc_dirents:
src_path = posixpath.join(src_parent_dir, sdoc_dirent)
dst_path = posixpath.join(dst_parent_dir, sdoc_dirent)
move_sdoc_images_to_different_repo(
src_repo_id, src_path, dst_repo_id, dst_path, username, is_async=False)
except Exception as e:
logger.error(e)

return Response(result)


Expand Down
61 changes: 61 additions & 0 deletions seahub/seadoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,64 @@ def gen_path_link(path, repo_name):

return zipped


def copy_sdoc_images_to_different_repo(src_repo_id, src_path, dst_repo_id, dst_path, username, is_async=True):
src_repo = seafile_api.get_repo(src_repo_id)
src_file_uuid = get_seadoc_file_uuid(src_repo, src_path)
src_image_parent_path = '/images/sdoc/' + src_file_uuid + '/'
src_dir_id = seafile_api.get_dir_id_by_path(src_repo_id, src_image_parent_path)
if not src_dir_id:
return
dst_repo = seafile_api.get_repo(dst_repo_id)
dst_file_uuid = get_seadoc_file_uuid(dst_repo, dst_path)
dst_image_parent_path = gen_seadoc_image_parent_path(
dst_file_uuid, dst_repo_id, username=username)
image_dirents = seafile_api.list_dir_by_path(src_repo_id, src_image_parent_path)
image_names = [item.obj_name for item in image_dirents]

if is_async:
need_progress=1
synchronous=0
else:
need_progress=0
synchronous=1
seafile_api.copy_file(
src_repo_id, src_image_parent_path,
json.dumps(image_names),
dst_repo_id, dst_image_parent_path,
json.dumps(image_names),
username=username,
need_progress=need_progress, synchronous=synchronous,
)
return


def move_sdoc_images_to_different_repo(src_repo_id, src_path, dst_repo_id, dst_path, username, is_async=True):
src_repo = seafile_api.get_repo(src_repo_id)
src_file_uuid = get_seadoc_file_uuid(src_repo, src_path)
src_image_parent_path = '/images/sdoc/' + src_file_uuid + '/'
src_dir_id = seafile_api.get_dir_id_by_path(src_repo_id, src_image_parent_path)
if not src_dir_id:
return
dst_repo = seafile_api.get_repo(dst_repo_id)
dst_file_uuid = get_seadoc_file_uuid(dst_repo, dst_path)
dst_image_parent_path = gen_seadoc_image_parent_path(
dst_file_uuid, dst_repo_id, username=username)
image_dirents = seafile_api.list_dir_by_path(src_repo_id, src_image_parent_path)
image_names = [item.obj_name for item in image_dirents]

if is_async:
need_progress=1
synchronous=0
else:
need_progress=0
synchronous=1
seafile_api.move_file(
src_repo_id, src_image_parent_path,
json.dumps(image_names),
dst_repo_id, dst_image_parent_path,
json.dumps(image_names),
replace=False, username=username,
need_progress=need_progress, synchronous=synchronous,
)
return

0 comments on commit dca836a

Please sign in to comment.