From 03f86bfee2cb80c86d9245ce1231c430116a98eb Mon Sep 17 00:00:00 2001 From: Patrick McAndrew Date: Thu, 13 Mar 2014 14:48:11 +0000 Subject: [PATCH] api call to get file token details --- seahub/api2/urls.py | 2 ++ seahub/api2/views.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/seahub/api2/urls.py b/seahub/api2/urls.py index df758d9332..43f6f2ac83 100644 --- a/seahub/api2/urls.py +++ b/seahub/api2/urls.py @@ -40,6 +40,8 @@ urlpatterns = patterns('', url(r'^shared-files/$', SharedFilesView.as_view()), url(r'^virtual-repos/$', VirtualRepos.as_view()), + url(r'^f/(?P[a-f0-9]{10})/detail/$', SharedFileDetailView.as_view()), + url(r'^groups/$', Groups.as_view()), url(r'^events/$', EventsView.as_view()), url(r'^unseen_messages/$', UnseenMessagesCountView.as_view()), diff --git a/seahub/api2/views.py b/seahub/api2/views.py index 6527c1f792..acc196896d 100644 --- a/seahub/api2/views.py +++ b/seahub/api2/views.py @@ -1822,6 +1822,58 @@ class VirtualRepos(APIView): return HttpResponse(json.dumps(result), status=500, content_type=content_type) return HttpResponse(json.dumps(result, cls=SearpcObjEncoder), content_type=content_type) + +class SharedFileDetailView(APIView): + authentication_classes = (TokenAuthentication, ) + permission_classes = (IsAuthenticated,) + throttle_classes = (UserRateThrottle, ) + + def get(self, request, token, format=None): + assert token is not None # Checked by URLconf + + try: + fileshare = FileShare.objects.get(token=token) + except FileShare.DoesNotExist: + return api_error(status.HTTP_404_NOT_FOUND, "Token not found") + + shared_by = fileshare.username + repo_id = fileshare.repo_id + repo = get_repo(repo_id) + if not repo: + return api_error(status.HTTP_404_NOT_FOUND, "Repo not found") + + path = fileshare.path.rstrip('/') # Normalize file path + file_name = os.path.basename(path) + + file_id = None + try: + file_id = seafserv_threaded_rpc.get_file_id_by_path(repo_id, + path.encode('utf-8')) + commits = seafserv_threaded_rpc.list_file_revisions(repo_id, path, + -1, -1) + c = commits[0] + except SearpcError, e: + return api_error(HTTP_520_OPERATION_FAILED, + "Failed to get file id by path.") + + if not file_id: + return api_error(status.HTTP_404_NOT_FOUND, "File not found") + + entry = {} + try: + entry["size"] = get_file_size(file_id) + except Exception, e: + entry["size"] = 0 + + entry["type"] = "file" + entry["name"] = file_name + entry["id"] = file_id + entry["mtime"] = c.ctime + entry["repo_id"] = repo_id + entry["path"] = path + + return HttpResponse(json.dumps(entry), status=200, + content_type=json_content_type) class FileShareEncoder(json.JSONEncoder): def default(self, obj):