Skip to content

Commit

Permalink
Added endpoints for create and delete of folder and relocating the file
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Zoubek authored and ondratu committed Mar 21, 2022
1 parent a126dc5 commit 6d05e9c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* Use "Sync|->:" and "Sync->|:" to signify which way is the current transfer going
* Add DNS service discovery compatible with prusa-slicer
* Support file upload cancels from prusa-slicer (once PoorWSGI updates)
* Added endpoints for create and delete of folder and relocating the file

0.6.0 (2021-12-17)
* Added endpoint for control of extruder
Expand Down
7 changes: 7 additions & 0 deletions prusa/link/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ class BadRequestError(LinkError):
status_code = state.HTTP_BAD_REQUEST


class DestinationSameAsSource(BadRequestError):
"""400 Destination is same as source"""
title = "Destination same as source"
text = "Destination to move file is same as the source of the file"
id = "destination-same-as-source"


class NoFileInRequest(BadRequestError):
"""400 File not found in request payload."""
title = "Missing file in payload."
Expand Down
71 changes: 71 additions & 0 deletions prusa/link/web/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from time import sleep, time
from io import FileIO
from functools import wraps
from shutil import move, rmtree

import logging

Expand Down Expand Up @@ -478,6 +479,76 @@ def api_download(req, target):
return Response(status_code=state.HTTP_CREATED)


@app.route('/api/folder/<target>/<path:re:.+>', method=state.METHOD_POST)
@check_api_digest
@check_target
def api_create_folder(req, target, path):
"""Create a folder in a path"""
# pylint: disable=unused-argument
os_path = get_os_path('/Prusa Link gcodes')
path = join(os_path, path)

if not exists(path):
makedirs(path)
else:
return Response(status_code=state.HTTP_CONFLICT)

return Response(status_code=state.HTTP_CREATED)


@app.route('/api/folder/<target>/<path:re:.+>', method=state.METHOD_DELETE)
@check_api_digest
@check_target
def api_delete_folder(req, target, path):
"""Delete a folder in a path"""
# pylint: disable=unused-argument
os_path = get_os_path('/Prusa Link gcodes')
path = join(os_path, path)

if exists(path):
rmtree(path)
return Response(status_code=state.HTTP_OK)

return Response(status_code=state.HTTP_CONFLICT)


@app.route('/api/modify/<target>', method=state.METHOD_POST)
@check_api_digest
@check_target
def api_modify(req, target):
"""Move file to another directory or/and change its name"""
# pylint: disable=unused-argument
os_path = get_os_path('/Prusa Link gcodes')

source = join(os_path, req.json.get('source'))
destination = join(os_path, req.json.get('destination'))

path = dirname(destination)

job = Job.get_instance()



if job.data.job_state == JobState.IN_PROGRESS and \
source == get_os_path(job.data.selected_file_path):
raise errors.FileCurrentlyPrinted()

if source == destination:
raise errors.DestinationSameAsSource

if not exists(source):
raise errors.FileNotFound

if not exists(path):
try:
makedirs(path)
move(source, destination)
except PermissionError as error:
raise error

return Response(status_code=state.HTTP_CREATED)


@app.route('/api/download', method=state.METHOD_DELETE)
@check_api_digest
def api_download_abort(req):
Expand Down

0 comments on commit 6d05e9c

Please sign in to comment.