|
9 | 9 | import os
|
10 | 10 | import datetime
|
11 | 11 |
|
12 |
| -class ThumbnailProcessor(octoprint.filemanager.util.LineProcessorStream): |
13 |
| - |
14 |
| - def __init__(self, fileBufferedReader, path, logger): |
15 |
| - super(ThumbnailProcessor, self).__init__(fileBufferedReader) |
16 |
| - self._thumbnail_data = "" |
17 |
| - self._collecting_data = False |
18 |
| - self._logger = logger |
19 |
| - self._path = path |
20 |
| - self._folder_path = os.path.dirname(path) |
21 |
| - |
22 |
| - def process_line(self, origLine): |
23 |
| - if not len(origLine): |
24 |
| - return None |
25 |
| - |
26 |
| - isBytesLineForPy3 = type(origLine) is bytes and not (type(origLine) is str) |
27 |
| - line = octoprint.util.to_unicode(origLine, errors="replace") |
28 |
| - line = line.lstrip() |
29 |
| - |
30 |
| - if (len(line) != 0 and line.startswith("; thumbnail end")): |
31 |
| - self._collecting_data = False |
32 |
| - if len(self._thumbnail_data) > 0: |
33 |
| - if not os.path.exists(self._folder_path): |
34 |
| - os.makedirs(self._folder_path) |
35 |
| - if os.path.exists(self._path): |
36 |
| - os.remove(self._path) |
37 |
| - import base64 |
38 |
| - with open(self._path, "wb") as fh: |
39 |
| - fh.write(base64.b64decode(self._thumbnail_data)) |
40 |
| - self._thumbnail_data = "" |
41 |
| - |
42 |
| - if (len(line) != 0 and self._collecting_data == True): |
43 |
| - self._thumbnail_data += line.replace("; ","") |
44 |
| - |
45 |
| - if (len(line) != 0 and line.startswith("; thumbnail begin")): |
46 |
| - self._collecting_data = True |
47 |
| - |
48 |
| - line = origLine |
49 |
| - |
50 |
| - if (isBytesLineForPy3 and type(line) is str): |
51 |
| - # line = line.encode('utf8') |
52 |
| - # line = line.encode('ISO-8859-1') |
53 |
| - line = octoprint.util.to_bytes(line, errors="replace") |
54 |
| - else: |
55 |
| - if (isBytesLineForPy3 == False): |
56 |
| - # do nothing, because we don't modify the line |
57 |
| - if (type(line) is unicode): |
58 |
| - line = octoprint.util.to_native_str(line) |
59 |
| - return line |
60 |
| - |
61 | 12 | class PrusaslicerthumbnailsPlugin(octoprint.plugin.SettingsPlugin,
|
62 | 13 | octoprint.plugin.AssetPlugin,
|
63 | 14 | octoprint.plugin.TemplatePlugin,
|
@@ -97,34 +48,36 @@ def get_template_configs(self):
|
97 | 48 | dict(type="settings", custom_bindings=False, template="prusaslicerthumbnails_settings.jinja2"),
|
98 | 49 | ]
|
99 | 50 |
|
| 51 | + def _extract_thumbnail(self, gcode_filename, thumbnail_filename): |
| 52 | + import re |
| 53 | + import base64 |
| 54 | + regex = r"(?:^; thumbnail begin \d+x\d+ \d+)(?:\n|\r\n?)((?:.+(?:\n|\r\n?))+)(?:^; thumbnail end)" |
| 55 | + with open(gcode_filename,"rb") as gcode_file: |
| 56 | + test_str = gcode_file.read().decode('utf-8') |
| 57 | + matches = re.findall(regex, test_str, re.MULTILINE) |
| 58 | + if len(matches) > 0: |
| 59 | + path = os.path.dirname(thumbnail_filename) |
| 60 | + if not os.path.exists(path): |
| 61 | + os.makedirs(path) |
| 62 | + with open(thumbnail_filename,"wb") as png_file: |
| 63 | + png_file.write(base64.b64decode(matches[-1:][0].replace("; ", "").encode())) |
| 64 | + |
100 | 65 | ##~~ EventHandlerPlugin mixin
|
101 | 66 |
|
102 | 67 | def on_event(self, event, payload):
|
103 |
| - if event == "FileAdded" and "gcode" in payload["type"]: |
104 |
| - self._logger.info("File Added: %s" % payload["path"]) |
105 |
| - if event == "FileRemoved" and "gcode" in payload["type"]: |
| 68 | + if event == "FolderRemoved" and payload["storage"] == "local": |
| 69 | + import shutil |
| 70 | + shutil.rmtree(self.get_plugin_data_folder() + "/" + payload["path"], ignore_errors=True) |
| 71 | + if event in ["FileAdded","FileRemoved"] and payload["storage"] == "local" and "gcode" in payload["type"]: |
106 | 72 | thumbnail_filename = self.get_plugin_data_folder() + "/" + payload["path"].replace(".gcode",".png")
|
107 | 73 | if os.path.exists(thumbnail_filename):
|
108 | 74 | os.remove(thumbnail_filename)
|
109 |
| - if event == "MetadataAnalysisStarted" and ".gcode" in payload["path"]: |
110 |
| - self._analysis_active = True |
111 |
| - if event == "MetadataAnalysisFinished" and ".gcode" in payload["path"]: |
112 |
| - thumbnail_filename = self.get_plugin_data_folder() + "/" + payload["path"].replace(".gcode",".png") |
113 |
| - if os.path.exists(thumbnail_filename): |
114 |
| - thumbnail_url = "/plugin/prusaslicerthumbnails/thumbnail/" + payload["path"].replace(".gcode", ".png") + "?" + "{:%Y%m%d%H%M%S}".format(datetime.datetime.now()) |
115 |
| - self._storage_interface = self._file_manager._storage(payload.get("origin", "local")) |
116 |
| - self._storage_interface.set_additional_metadata(payload.get("path"), "thumbnail", thumbnail_url, overwrite=True) |
117 |
| - self._analysis_active = False |
118 |
| - |
119 |
| - ##~~ preprocessor hook |
120 |
| - |
121 |
| - def thumbnail_extractor(self, path, file_object, links=None, printer_profile=None, allow_overwrite=True, *args, **kwargs): |
122 |
| - if not octoprint.filemanager.valid_file_type(path, type="gcode"): |
123 |
| - return file_object |
124 |
| - |
125 |
| - thumbnail_filename = self.get_plugin_data_folder() + "/" + path.replace(".gcode",".png") |
126 |
| - return octoprint.filemanager.util.StreamWrapper(file_object.filename, ThumbnailProcessor(file_object.stream(), thumbnail_filename, self._logger)) |
127 |
| - # return file_object |
| 75 | + if event == "FileAdded": |
| 76 | + gcode_filename = self._file_manager.path_on_disk("local", payload["path"]) |
| 77 | + self._extract_thumbnail(gcode_filename, thumbnail_filename) |
| 78 | + if os.path.exists(thumbnail_filename): |
| 79 | + thumbnail_url = "plugin/prusaslicerthumbnails/thumbnail/" + payload["path"].replace(".gcode", ".png") + "?" + "{:%Y%m%d%H%M%S}".format(datetime.datetime.now()) |
| 80 | + self._file_manager.set_additional_metadata("local", payload["path"], "thumbnail", thumbnail_url, overwrite=True) |
128 | 81 |
|
129 | 82 | ##~~ Routes hook
|
130 | 83 | def route_hook(self, server_routes, *args, **kwargs):
|
@@ -165,7 +118,6 @@ def __plugin_load__():
|
165 | 118 | global __plugin_hooks__
|
166 | 119 | __plugin_hooks__ = {
|
167 | 120 | "octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information,
|
168 |
| - "octoprint.filemanager.preprocessor": __plugin_implementation__.thumbnail_extractor, |
169 | 121 | "octoprint.server.http.routes": __plugin_implementation__.route_hook
|
170 | 122 | }
|
171 | 123 |
|
0 commit comments