Ver código fonte

[api] Add new /mkdir public API (#3597)

- Implement a dedicated /mkdir API method tailored for new storage browser.
- This separates the old method for existing filebrowser to not have regressions + much cleaner new method.
Harsh Gupta 1 ano atrás
pai
commit
ac97eabe42

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

@@ -16,6 +16,10 @@
 # limitations under the License.
 
 import logging
+import posixpath
+
+from django.http import HttpResponse
+from django.utils.translation import gettext as _
 
 from desktop.lib.django_util import JsonResponse
 from desktop.lib import fsmanager
@@ -80,3 +84,16 @@ def get_filesystems_with_home_dirs(request): # Using as a public API only for no
     })
 
   return JsonResponse(filesystems, safe=False)
+
+
+@error_handler
+def mkdir(request):
+  path = request.POST.get('path')
+  name = request.POST.get('name')
+
+  if name and (posixpath.sep in name or "#" in name):
+    raise Exception(_("Error creating %s directory. Slashes or hashes are not allowed in directory name." % name))
+
+  request.fs.mkdir(request.fs.join(path, name))
+  return HttpResponse(status=200)
+

+ 1 - 1
desktop/core/src/desktop/api_public.py

@@ -217,7 +217,7 @@ def storage_upload_file(request):
 @api_view(["POST"])
 def storage_mkdir(request):
   django_request = get_django_request(request)
-  return filebrowser_views.mkdir(django_request)
+  return filebrowser_api.mkdir(django_request)
 
 
 # Importer