Skip to content

Update internal_api.py #7642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions seahub/api2/endpoints/internal_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2012-2016 Seafile Ltd.
import logging
from django.contrib.sessions.backends.db import SessionStore
import os
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
Expand All @@ -14,8 +14,8 @@
from seahub.share.models import UploadLinkShare, FileShare, check_share_link_access, check_share_link_access_by_scope
from seaserv import seafile_api
from seahub.utils.repo import parse_repo_perm
from seahub.views.file import send_file_access_msg
from seahub.utils import normalize_file_path
from seahub.views.file import send_file_access_msg, FILE_TYPE_FOR_NEW_FILE_LINK
from seahub.utils import normalize_file_path, get_file_type_and_ext
from seahub.views import check_folder_permission

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -159,6 +159,15 @@ def post(self, request, repo_id):
error_msg = 'File not found'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

filename = os.path.basename(file_path)
filetype, ext = get_file_type_and_ext(filename)

# The download permission can be ignored when the permission check
# called from seaf-server for some file types such as video, markdown and pdf
# which is viewed / downloaded directly by requesting seaf-server.

ignore_download_perms = filetype in FILE_TYPE_FOR_NEW_FILE_LINK

token = request.data.get('token') # account token or repo token
ip_addr = request.data.get('ip_addr')
user_agent = request.data.get('user_agent')
Expand All @@ -176,12 +185,14 @@ def post(self, request, repo_id):
op_perms = parse_repo_perm(seafile_api.check_permission_by_path(
repo_id, '/', username))

if op == OP_DOWNLOAD and not op_perms.can_download:
error_msg = 'Permission denied.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
if op == OP_DOWNLOAD:
if not (ignore_download_perms or op_perms.can_download):
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

if op == OP_UPLOAD and not op_perms.can_upload:
error_msg = 'Permission denied.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

send_file_access_msg(request, repo, file_path, 'web', custom_ip=ip_addr, custom_agent=user_agent)
return Response({'user': username})
Expand Down