/
files.py
1400 lines (1199 loc) · 50.1 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__author__ = "Gina Häußge <osd@foosel.net>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
import hashlib
import logging
import os
import threading
from urllib.parse import quote as urlquote
import psutil
from flask import abort, jsonify, make_response, request, url_for
import octoprint.filemanager
import octoprint.filemanager.storage
import octoprint.filemanager.util
import octoprint.slicing
from octoprint.access.permissions import Permissions
from octoprint.events import Events
from octoprint.filemanager.destinations import FileDestinations
from octoprint.filemanager.storage import StorageError
from octoprint.server import (
NO_CONTENT,
current_user,
eventManager,
fileManager,
printer,
slicingManager,
)
from octoprint.server.api import api
from octoprint.server.util.flask import (
get_json_command_from_request,
no_firstrun_access,
with_revalidation_checking,
)
from octoprint.settings import settings, valid_boolean_trues
from octoprint.util import sv, time_this
# ~~ GCODE file handling
_file_cache = {}
_file_cache_mutex = threading.RLock()
_DATA_FORMAT_VERSION = "v2"
def _clear_file_cache():
with _file_cache_mutex:
_file_cache.clear()
def _create_lastmodified(path, recursive):
path = path[len("/api/files") :]
if path.startswith("/"):
path = path[1:]
if path == "":
# all storages involved
lms = [0]
for storage in fileManager.registered_storages:
try:
lms.append(fileManager.last_modified(storage, recursive=recursive))
except Exception:
logging.getLogger(__name__).exception(
"There was an error retrieving the last modified data from storage {}".format(
storage
)
)
lms.append(None)
if any(filter(lambda x: x is None, lms)):
# we return None if ANY of the involved storages returned None
return None
# if we reach this point, we return the maximum of all dates
return max(lms)
else:
if "/" in path:
storage, path_in_storage = path.split("/", 1)
else:
storage = path
path_in_storage = None
try:
return fileManager.last_modified(
storage, path=path_in_storage, recursive=recursive
)
except Exception:
logging.getLogger(__name__).exception(
"There was an error retrieving the last modified data from storage {} and path {}".format(
storage, path_in_storage
)
)
return None
def _create_etag(path, filter, recursive, lm=None):
if lm is None:
lm = _create_lastmodified(path, recursive)
if lm is None:
return None
hash = hashlib.sha1()
def hash_update(value):
value = value.encode("utf-8")
hash.update(value)
hash_update(str(lm))
hash_update(str(filter))
hash_update(str(recursive))
path = path[len("/api/files") :]
if path.startswith("/"):
path = path[1:]
if "/" in path:
storage, _ = path.split("/", 1)
else:
storage = path
if path == "" or storage == FileDestinations.SDCARD:
# include sd data in etag
hash_update(repr(sorted(printer.get_sd_files(), key=lambda x: sv(x["name"]))))
hash_update(_DATA_FORMAT_VERSION) # increment version if we change the API format
return hash.hexdigest()
@api.route("/files", methods=["GET"])
@Permissions.FILES_LIST.require(403)
@with_revalidation_checking(
etag_factory=lambda lm=None: _create_etag(
request.path,
request.values.get("filter", False),
request.values.get("recursive", False),
lm=lm,
),
lastmodified_factory=lambda: _create_lastmodified(
request.path, request.values.get("recursive", False)
),
unless=lambda: request.values.get("force", False)
or request.values.get("_refresh", False),
)
def readGcodeFiles():
filter = request.values.get("filter", False)
recursive = request.values.get("recursive", "false") in valid_boolean_trues
force = request.values.get("force", "false") in valid_boolean_trues
files = _getFileList(
FileDestinations.LOCAL,
filter=filter,
recursive=recursive,
allow_from_cache=not force,
)
files.extend(_getFileList(FileDestinations.SDCARD, allow_from_cache=not force))
usage = psutil.disk_usage(settings().getBaseFolder("uploads", check_writable=False))
return jsonify(files=files, free=usage.free, total=usage.total)
@api.route("/files/test", methods=["POST"])
@Permissions.FILES_LIST.require(403)
def runFilesTest():
valid_commands = {
"sanitize": ["storage", "path", "filename"],
"exists": ["storage", "path", "filename"],
}
command, data, response = get_json_command_from_request(request, valid_commands)
if response is not None:
return response
def sanitize(storage, path, filename):
sanitized_path = fileManager.sanitize_path(storage, path)
sanitized_name = fileManager.sanitize_name(storage, filename)
joined = fileManager.join_path(storage, sanitized_path, sanitized_name)
return sanitized_path, sanitized_name, joined
if command == "sanitize":
_, _, sanitized = sanitize(data["storage"], data["path"], data["filename"])
return jsonify(sanitized=sanitized)
elif command == "exists":
storage = data["storage"]
path = data["path"]
filename = data["filename"]
sanitized_path, _, sanitized = sanitize(storage, path, filename)
exists = _getFileDetails(storage, sanitized)
if exists:
suggestion = filename
name, ext = os.path.splitext(filename)
counter = 0
while fileManager.file_exists(
storage,
fileManager.join_path(
storage,
sanitized_path,
fileManager.sanitize_name(storage, suggestion),
),
):
counter += 1
suggestion = f"{name}_{counter}{ext}"
return jsonify(
exists=True,
suggestion=suggestion,
size=exists.get("size"),
date=exists.get("date"),
)
else:
return jsonify(exists=False)
@api.route("/files/<string:origin>", methods=["GET"])
@Permissions.FILES_LIST.require(403)
@with_revalidation_checking(
etag_factory=lambda lm=None: _create_etag(
request.path,
request.values.get("filter", False),
request.values.get("recursive", False),
lm=lm,
),
lastmodified_factory=lambda: _create_lastmodified(
request.path, request.values.get("recursive", False)
),
unless=lambda: request.values.get("force", False)
or request.values.get("_refresh", False),
)
def readGcodeFilesForOrigin(origin):
if origin not in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
abort(404)
filter = request.values.get("filter", False)
recursive = request.values.get("recursive", "false") in valid_boolean_trues
force = request.values.get("force", "false") in valid_boolean_trues
files = _getFileList(
origin, filter=filter, recursive=recursive, allow_from_cache=not force
)
if origin == FileDestinations.LOCAL:
usage = psutil.disk_usage(
settings().getBaseFolder("uploads", check_writable=False)
)
return jsonify(files=files, free=usage.free, total=usage.total)
else:
return jsonify(files=files)
@api.route("/files/<string:target>/<path:filename>", methods=["GET"])
@Permissions.FILES_LIST.require(403)
@with_revalidation_checking(
etag_factory=lambda lm=None: _create_etag(
request.path,
request.values.get("filter", False),
request.values.get("recursive", False),
lm=lm,
),
lastmodified_factory=lambda: _create_lastmodified(
request.path, request.values.get("recursive", False)
),
unless=lambda: request.values.get("force", False)
or request.values.get("_refresh", False),
)
def readGcodeFile(target, filename):
if target not in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
abort(404)
if not _validate(target, filename):
abort(404)
recursive = False
if "recursive" in request.values:
recursive = request.values["recursive"] in valid_boolean_trues
file = _getFileDetails(target, filename, recursive=recursive)
if not file:
abort(404)
return jsonify(file)
def _getFileDetails(origin, path, recursive=True):
parent, path = os.path.split(path)
files = _getFileList(origin, path=parent, recursive=recursive, level=1)
for f in files:
if f["name"] == path:
return f
else:
return None
@time_this(
logtarget=__name__ + ".timings",
message="{func}({func_args},{func_kwargs}) took {timing:.2f}ms",
incl_func_args=True,
log_enter=True,
message_enter="Entering {func}({func_args},{func_kwargs})...",
)
def _getFileList(
origin, path=None, filter=None, recursive=False, level=0, allow_from_cache=True
):
if origin == FileDestinations.SDCARD:
sdFileList = printer.get_sd_files(refresh=not allow_from_cache)
files = []
if sdFileList is not None:
for f in sdFileList:
type_path = octoprint.filemanager.get_file_type(f["name"])
if not type_path:
# only supported extensions
continue
else:
file_type = type_path[0]
file = {
"type": file_type,
"typePath": type_path,
"name": f["name"],
"display": f["display"] if f["display"] else f["name"],
"path": f["name"],
"origin": FileDestinations.SDCARD,
"refs": {
"resource": url_for(
".readGcodeFile",
target=FileDestinations.SDCARD,
filename=f["name"],
_external=True,
)
},
}
if f["size"] is not None:
file.update({"size": f["size"]})
if f["date"] is not None:
file.update({"date": f["date"]})
files.append(file)
else:
# PERF: Only retrieve the extension tree once
extension_tree = octoprint.filemanager.full_extension_tree()
filter_func = None
if filter:
filter_func = lambda entry, entry_data: octoprint.filemanager.valid_file_type(
entry, type=filter, tree=extension_tree
)
with _file_cache_mutex:
cache_key = f"{origin}:{path}:{recursive}:{filter}"
files, lastmodified = _file_cache.get(cache_key, ([], None))
# recursive needs to be True for lastmodified queries so we get lastmodified of whole subtree - #3422
if (
not allow_from_cache
or lastmodified is None
or lastmodified
< fileManager.last_modified(origin, path=path, recursive=True)
):
files = list(
fileManager.list_files(
origin,
path=path,
filter=filter_func,
recursive=recursive,
level=level,
force_refresh=not allow_from_cache,
)[origin].values()
)
lastmodified = fileManager.last_modified(
origin, path=path, recursive=True
)
_file_cache[cache_key] = (files, lastmodified)
def analyse_recursively(files, path=None):
if path is None:
path = ""
result = []
for file_or_folder in files:
# make a shallow copy in order to not accidentally modify the cached data
file_or_folder = dict(file_or_folder)
file_or_folder["origin"] = FileDestinations.LOCAL
if file_or_folder["type"] == "folder":
if "children" in file_or_folder:
children = analyse_recursively(
file_or_folder["children"].values(),
path + file_or_folder["name"] + "/",
)
latest_print = None
success = 0
failure = 0
for child in children:
if (
"prints" not in child
or "last" not in child["prints"]
or "date" not in child["prints"]["last"]
):
continue
success += child["prints"].get("success", 0)
failure += child["prints"].get("failure", 0)
if (
latest_print is None
or child["prints"]["last"]["date"] > latest_print["date"]
):
latest_print = child["prints"]["last"]
file_or_folder["children"] = children
file_or_folder["prints"] = {
"success": success,
"failure": failure,
}
if latest_print:
file_or_folder["prints"]["last"] = latest_print
file_or_folder["refs"] = {
"resource": url_for(
".readGcodeFile",
target=FileDestinations.LOCAL,
filename=path + file_or_folder["name"],
_external=True,
)
}
else:
if (
"analysis" in file_or_folder
and octoprint.filemanager.valid_file_type(
file_or_folder["name"], type="gcode", tree=extension_tree
)
):
file_or_folder["gcodeAnalysis"] = file_or_folder["analysis"]
del file_or_folder["analysis"]
if (
"history" in file_or_folder
and octoprint.filemanager.valid_file_type(
file_or_folder["name"], type="gcode", tree=extension_tree
)
):
# convert print log
history = file_or_folder["history"]
del file_or_folder["history"]
success = 0
failure = 0
last = None
for entry in history:
success += 1 if "success" in entry and entry["success"] else 0
failure += (
1 if "success" in entry and not entry["success"] else 0
)
if not last or (
"timestamp" in entry
and "timestamp" in last
and entry["timestamp"] > last["timestamp"]
):
last = entry
if last:
prints = {
"success": success,
"failure": failure,
"last": {
"success": last["success"],
"date": last["timestamp"],
},
}
if "printTime" in last:
prints["last"]["printTime"] = last["printTime"]
file_or_folder["prints"] = prints
file_or_folder["refs"] = {
"resource": url_for(
".readGcodeFile",
target=FileDestinations.LOCAL,
filename=file_or_folder["path"],
_external=True,
),
"download": url_for("index", _external=True)
+ "downloads/files/"
+ FileDestinations.LOCAL
+ "/"
+ urlquote(file_or_folder["path"]),
}
result.append(file_or_folder)
return result
files = analyse_recursively(files)
return files
def _verifyFileExists(origin, filename):
if origin == FileDestinations.SDCARD:
return filename in (x["name"] for x in printer.get_sd_files())
else:
return fileManager.file_exists(origin, filename)
def _verifyFolderExists(origin, foldername):
if origin == FileDestinations.SDCARD:
return False
else:
return fileManager.folder_exists(origin, foldername)
def _isBusy(target, path):
currentOrigin, currentPath = _getCurrentFile()
if (
currentPath is not None
and currentOrigin == target
and fileManager.file_in_path(FileDestinations.LOCAL, path, currentPath)
and (printer.is_printing() or printer.is_paused())
):
return True
return any(
target == x[0] and fileManager.file_in_path(FileDestinations.LOCAL, path, x[1])
for x in fileManager.get_busy_files()
)
@api.route("/files/<string:target>", methods=["POST"])
@no_firstrun_access
@Permissions.FILES_UPLOAD.require(403)
def uploadGcodeFile(target):
input_name = "file"
input_upload_name = (
input_name + "." + settings().get(["server", "uploads", "nameSuffix"])
)
input_upload_path = (
input_name + "." + settings().get(["server", "uploads", "pathSuffix"])
)
if input_upload_name in request.values and input_upload_path in request.values:
if target not in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
abort(404)
upload = octoprint.filemanager.util.DiskFileWrapper(
request.values[input_upload_name], request.values[input_upload_path]
)
# Store any additional user data the caller may have passed.
userdata = None
if "userdata" in request.values:
import json
try:
userdata = json.loads(request.values["userdata"])
except Exception:
abort(400, description="userdata contains invalid JSON")
# check preconditions for SD upload
if target == FileDestinations.SDCARD and not settings().getBoolean(
["feature", "sdSupport"]
):
abort(404)
sd = target == FileDestinations.SDCARD
if sd:
# validate that all preconditions for SD upload are met before attempting it
if not (
printer.is_operational()
and not (printer.is_printing() or printer.is_paused())
):
abort(
409,
description="Can not upload to SD card, printer is either not operational or already busy",
)
if not printer.is_sd_ready():
abort(409, description="Can not upload to SD card, not yet initialized")
# evaluate select and print parameter and if set check permissions & preconditions
# and adjust as necessary
#
# we do NOT abort(409) here since this would be a backwards incompatible behaviour change
# on the API, but instead return the actually effective select and print flags in the response
#
# note that this behaviour might change in a future API version
select_request = (
"select" in request.values
and request.values["select"] in valid_boolean_trues
and Permissions.FILES_SELECT.can()
)
print_request = (
"print" in request.values
and request.values["print"] in valid_boolean_trues
and Permissions.PRINT.can()
)
to_select = select_request
to_print = print_request
if (to_select or to_print) and not (
printer.is_operational()
and not (printer.is_printing() or printer.is_paused())
):
# can't select or print files if not operational or ready
to_select = to_print = False
# determine future filename of file to be uploaded, abort if it can't be uploaded
try:
# FileDestinations.LOCAL = should normally be target, but can't because SDCard handling isn't implemented yet
canonPath, canonFilename = fileManager.canonicalize(
FileDestinations.LOCAL, upload.filename
)
if request.values.get("path"):
canonPath = request.values.get("path")
if request.values.get("filename"):
canonFilename = request.values.get("filename")
futurePath = fileManager.sanitize_path(FileDestinations.LOCAL, canonPath)
futureFilename = fileManager.sanitize_name(
FileDestinations.LOCAL, canonFilename
)
except Exception:
canonFilename = None
futurePath = None
futureFilename = None
if futureFilename is None:
abort(400, description="Can not upload file, invalid file name")
# prohibit overwriting currently selected file while it's being printed
futureFullPath = fileManager.join_path(
FileDestinations.LOCAL, futurePath, futureFilename
)
futureFullPathInStorage = fileManager.path_in_storage(
FileDestinations.LOCAL, futureFullPath
)
if not printer.can_modify_file(futureFullPathInStorage, sd):
abort(
409,
description="Trying to overwrite file that is currently being printed",
)
if (
fileManager.file_exists(FileDestinations.LOCAL, futureFullPathInStorage)
and request.values.get("noOverwrite") in valid_boolean_trues
):
abort(409, description="File already exists and noOverwrite was set")
if (
fileManager.file_exists(FileDestinations.LOCAL, futureFullPathInStorage)
and not Permissions.FILES_DELETE.can()
):
abort(
403,
description="File already exists, cannot overwrite due to a lack of permissions",
)
reselect = printer.is_current_file(futureFullPathInStorage, sd)
user = current_user.get_name()
def fileProcessingFinished(filename, absFilename, destination):
"""
Callback for when the file processing (upload, optional slicing, addition to analysis queue) has
finished.
Depending on the file's destination triggers either streaming to SD card or directly calls to_select.
"""
if (
destination == FileDestinations.SDCARD
and octoprint.filemanager.valid_file_type(filename, "machinecode")
):
return filename, printer.add_sd_file(
filename,
absFilename,
on_success=selectAndOrPrint,
tags={"source:api", "api:files.sd"},
)
else:
selectAndOrPrint(filename, absFilename, destination)
return filename
def selectAndOrPrint(filename, absFilename, destination):
"""
Callback for when the file is ready to be selected and optionally printed. For SD file uploads this is only
the case after they have finished streaming to the printer, which is why this callback is also used
for the corresponding call to addSdFile.
Selects the just uploaded file if either to_select or to_print are True, or if the
exact file is already selected, such reloading it.
"""
if octoprint.filemanager.valid_file_type(added_file, "gcode") and (
to_select or to_print or reselect
):
printer.select_file(
absFilename,
destination == FileDestinations.SDCARD,
to_print,
user,
)
try:
added_file = fileManager.add_file(
FileDestinations.LOCAL,
futureFullPathInStorage,
upload,
allow_overwrite=True,
display=canonFilename,
)
except (OSError, StorageError) as e:
_abortWithException(e)
else:
filename = fileProcessingFinished(
added_file,
fileManager.path_on_disk(FileDestinations.LOCAL, added_file),
target,
)
done = not sd
if userdata is not None:
# upload included userdata, add this now to the metadata
fileManager.set_additional_metadata(
FileDestinations.LOCAL, added_file, "userdata", userdata
)
sdFilename = None
if isinstance(filename, tuple):
filename, sdFilename = filename
payload = {
"name": futureFilename,
"path": filename,
"target": target,
"select": select_request,
"print": print_request,
"effective_select": to_select,
"effective_print": to_print,
}
if userdata is not None:
payload["userdata"] = userdata
eventManager.fire(Events.UPLOAD, payload)
files = {}
location = url_for(
".readGcodeFile",
target=FileDestinations.LOCAL,
filename=filename,
_external=True,
)
files.update(
{
FileDestinations.LOCAL: {
"name": futureFilename,
"path": filename,
"origin": FileDestinations.LOCAL,
"refs": {
"resource": location,
"download": url_for("index", _external=True)
+ "downloads/files/"
+ FileDestinations.LOCAL
+ "/"
+ urlquote(filename),
},
}
}
)
if sd and sdFilename:
location = url_for(
".readGcodeFile",
target=FileDestinations.SDCARD,
filename=sdFilename,
_external=True,
)
files.update(
{
FileDestinations.SDCARD: {
"name": sdFilename,
"path": sdFilename,
"origin": FileDestinations.SDCARD,
"refs": {"resource": location},
}
}
)
r = make_response(
jsonify(
files=files,
done=done,
effectiveSelect=to_select,
effectivePrint=to_print,
),
201,
)
r.headers["Location"] = location
return r
elif "foldername" in request.values:
foldername = request.values["foldername"]
if target not in [FileDestinations.LOCAL]:
abort(400, description="target is invalid")
canonPath, canonName = fileManager.canonicalize(target, foldername)
futurePath = fileManager.sanitize_path(target, canonPath)
futureName = fileManager.sanitize_name(target, canonName)
if not futureName or not futurePath:
abort(400, description="folder name is empty")
if "path" in request.values and request.values["path"]:
futurePath = fileManager.sanitize_path(
FileDestinations.LOCAL, request.values["path"]
)
futureFullPath = fileManager.join_path(target, futurePath, futureName)
if octoprint.filemanager.valid_file_type(futureName):
abort(409, description="Can't create folder, please try another name")
try:
added_folder = fileManager.add_folder(
target, futureFullPath, display=canonName
)
except (OSError, StorageError) as e:
_abortWithException(e)
location = url_for(
".readGcodeFile",
target=FileDestinations.LOCAL,
filename=added_folder,
_external=True,
)
folder = {
"name": futureName,
"path": added_folder,
"origin": target,
"refs": {"resource": location},
}
r = make_response(jsonify(folder=folder, done=True), 201)
r.headers["Location"] = location
return r
else:
abort(400, description="No file to upload and no folder to create")
@api.route("/files/<string:target>/<path:filename>", methods=["POST"])
@no_firstrun_access
def gcodeFileCommand(filename, target):
if target not in [FileDestinations.LOCAL, FileDestinations.SDCARD]:
abort(404)
if not _validate(target, filename):
abort(404)
# valid file commands, dict mapping command name to mandatory parameters
valid_commands = {
"select": [],
"unselect": [],
"slice": [],
"analyse": [],
"copy": ["destination"],
"move": ["destination"],
}
command, data, response = get_json_command_from_request(request, valid_commands)
if response is not None:
return response
user = current_user.get_name()
if command == "select":
with Permissions.FILES_SELECT.require(403):
if not _verifyFileExists(target, filename):
abort(404)
# selects/loads a file
if not octoprint.filemanager.valid_file_type(filename, type="machinecode"):
abort(
415,
description="Cannot select file for printing, not a machinecode file",
)
if not printer.is_ready():
abort(
409,
description="Printer is already printing, cannot select a new file",
)
printAfterLoading = False
if "print" in data and data["print"] in valid_boolean_trues:
with Permissions.PRINT.require(403):
if not printer.is_operational():
abort(
409,
description="Printer is not operational, cannot directly start printing",
)
printAfterLoading = True
sd = False
if target == FileDestinations.SDCARD:
filenameToSelect = filename
sd = True
else:
filenameToSelect = fileManager.path_on_disk(target, filename)
printer.select_file(filenameToSelect, sd, printAfterLoading, user)
elif command == "unselect":
with Permissions.FILES_SELECT.require(403):
if not printer.is_ready():
return make_response(
"Printer is already printing, cannot unselect current file", 409
)
_, currentFilename = _getCurrentFile()
if currentFilename is None:
return make_response(
"Cannot unselect current file when there is no file selected", 409
)
if filename != currentFilename and filename != "current":
return make_response(
"Only the currently selected file can be unselected", 400
)
printer.unselect_file()
elif command == "slice":
with Permissions.SLICE.require(403):
if not _verifyFileExists(target, filename):
abort(404)
try:
if "slicer" in data:
slicer = data["slicer"]
del data["slicer"]
slicer_instance = slicingManager.get_slicer(slicer)
elif "cura" in slicingManager.registered_slicers:
slicer = "cura"
slicer_instance = slicingManager.get_slicer("cura")
else:
abort(415, description="Cannot slice file, no slicer available")
except octoprint.slicing.UnknownSlicer:
abort(404)
if not any(
[
octoprint.filemanager.valid_file_type(filename, type=source_file_type)
for source_file_type in slicer_instance.get_slicer_properties().get(
"source_file_types", ["model"]
)
]
):
abort(415, description="Cannot slice file, not a model file")
cores = psutil.cpu_count()
if (
slicer_instance.get_slicer_properties().get("same_device", True)
and (printer.is_printing() or printer.is_paused())
and (cores is None or cores < 2)
):
# slicer runs on same device as OctoPrint, slicing while printing is hence disabled
abort(
409,
description="Cannot slice on this slicer while printing on single core systems or systems of unknown core count due to performance reasons",
)
if "destination" in data and data["destination"]:
destination = data["destination"]
del data["destination"]
elif "gcode" in data and data["gcode"]:
destination = data["gcode"]
del data["gcode"]
else:
import os
name, _ = os.path.splitext(filename)
destination = (
name
+ "."
+ slicer_instance.get_slicer_properties().get(
"destination_extensions", ["gco", "gcode", "g"]
)[0]
)
full_path = destination
if "path" in data and data["path"]:
full_path = fileManager.join_path(target, data["path"], destination)
else:
path, _ = fileManager.split_path(target, filename)
if path:
full_path = fileManager.join_path(target, path, destination)
canon_path, canon_name = fileManager.canonicalize(target, full_path)
sanitized_name = fileManager.sanitize_name(target, canon_name)
if canon_path:
full_path = fileManager.join_path(target, canon_path, sanitized_name)