Selaa lähdekoodia

[api] Add new /rename public api (#3619)

[api] Add new /rename public api
Nidhi Bhat G 1 vuosi sitten
vanhempi
commit
79e427873d

+ 22 - 0
apps/filebrowser/src/filebrowser/api.py

@@ -17,6 +17,7 @@
 
 import logging
 import posixpath
+import os
 
 from django.http import HttpResponse
 from django.utils.translation import gettext as _
@@ -108,3 +109,24 @@ def touch(request):
   
   request.fs.create(request.fs.join(path, name))
   return HttpResponse(status=200)
+
+@error_handler
+def rename(request):
+  src_path = request.POST.get('src_path')
+  dest_path = request.POST.get('dest_path')
+
+  if "#" in dest_path:
+    raise Exception(_(
+      "Error renaming %s to %s. Hashes are not allowed in file or directory names." % (os.path.basename(src_path), dest_path)
+      ))
+
+  # If dest_path doesn't have a directory specified, use same dir.
+  if "/" not in dest_path:
+    src_dir = os.path.dirname(src_path)
+    dest_path = request.fs.join(src_dir, dest_path)
+
+  if request.fs.exists(dest_path):
+    raise Exception(_('The destination path "%s" already exists.') % dest_path)
+
+  request.fs.rename(src_path, dest_path)
+  return HttpResponse(status=200)

+ 4 - 0
desktop/core/src/desktop/api_public.py

@@ -224,6 +224,10 @@ def storage_touch(request):
   django_request = get_django_request(request)
   return filebrowser_api.touch(django_request)
 
+@api_view(["POST"])
+def storage_rename(request):
+  django_request = get_django_request(request)
+  return filebrowser_api.rename(django_request)
 
 # Importer
 

+ 2 - 1
desktop/core/src/desktop/api_public_urls_v1.py

@@ -97,7 +97,8 @@ urlpatterns += [
   re_path(r'^storage/download=(?P<path>.*)$', api_public.storage_download, name='storage_download'),
   re_path(r'^storage/upload/file/?$', api_public.storage_upload_file, name='storage_upload_file'),
   re_path(r'^storage/mkdir$', api_public.storage_mkdir, name='storage_mkdir'),
-  re_path(r'^storage/touch$', api_public.storage_touch, name='storage_touch')
+  re_path(r'^storage/touch$', api_public.storage_touch, name='storage_touch'),
+  re_path(r'^storage/rename$', api_public.storage_rename, name='storage_rename')
 ]
 
 urlpatterns += [