mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-05 17:02:47 +00:00
69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
# Copyright (c) 2012-2016 Seafile Ltd.
|
|
import logging
|
|
import posixpath
|
|
|
|
from rest_framework.authentication import SessionAuthentication
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
from rest_framework import status
|
|
|
|
from seaserv import seafile_api
|
|
|
|
from seahub.api2.authentication import TokenAuthentication
|
|
from seahub.api2.throttling import UserRateThrottle
|
|
from seahub.api2.utils import api_error
|
|
from seahub.views import check_folder_permission
|
|
|
|
logger = logging.getLogger(__name__)
|
|
json_content_type = 'application/json; charset=utf-8'
|
|
|
|
class RepoFileUploadedBytesView(APIView):
|
|
|
|
authentication_classes = (TokenAuthentication, SessionAuthentication)
|
|
permission_classes = (IsAuthenticated,)
|
|
throttle_classes = (UserRateThrottle,)
|
|
|
|
def get(self, request, repo_id):
|
|
""" For resumable fileupload
|
|
|
|
Permission checking:
|
|
1. login user.
|
|
"""
|
|
|
|
# argument check
|
|
parent_dir = request.GET.get('parent_dir', None)
|
|
if not parent_dir:
|
|
error_msg = 'parent_dir invalid.'
|
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
|
|
|
file_name = request.GET.get('file_name')
|
|
if not file_name:
|
|
error_msg = 'file_name 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)
|
|
|
|
if not seafile_api.get_dir_id_by_path(repo_id, parent_dir):
|
|
error_msg = 'Folder %s not found.' % parent_dir
|
|
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
|
|
|
|
# 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)
|
|
|
|
file_path = posixpath.join(parent_dir, file_name)
|
|
try:
|
|
uploadedBytes = seafile_api.get_upload_tmp_file_offset(repo_id, file_path)
|
|
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({"uploadedBytes": uploadedBytes})
|