Skip to content

Commit

Permalink
Fix for #138
Browse files Browse the repository at this point in the history
  • Loading branch information
FormerLurker committed Feb 6, 2021
1 parent 41e7170 commit b6a2f27
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
44 changes: 35 additions & 9 deletions octoprint_arc_welder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,17 @@ def process_request(self):
return jsonify({"success": False, "message": "Arc Welder is Disabled."})
try:
request_values = request.get_json()
path = request_values["path"]
path = urllibparse.unquote(request_values["path"])
origin = request_values["origin"]
# I think this is the easiest way to get the name.
path_part, name = self._file_manager.split_path(FileDestinations.LOCAL, path)

metadata = self._file_manager.get_metadata(FileDestinations.LOCAL, path)
if metadata is not None and "display" in metadata:
name = metadata["display"]
else:
# probably don't need this, but better safe than sorry
path_part, name = self._file_manager.split_path(FileDestinations.LOCAL, path)

# add the file and metadata to the processor queue
success = self.add_file_to_preprocessor_queue(name, path, origin, True)
except Exception as e:
Expand Down Expand Up @@ -482,6 +489,8 @@ def get_assets(self):
return dict(
js=[
"js/showdown.min.js",

#"js/showdown.js",
"js/pnotify_extensions.js",
"js/markdown_help.js",
"js/arc_welder.js",
Expand Down Expand Up @@ -726,8 +735,10 @@ def _get_print_after_processing(self, is_manual):
# return True
return False

def get_output_file_name_and_path(self, storage_path, gcode_comment_settings):
def get_output_file_name_and_path(self, display_name, storage_path, gcode_comment_settings):

path, name = self._file_manager.split_path(FileDestinations.LOCAL, storage_path)

# see what the plugin settings say about overwriting the target
overwrite_source_file = self._overwrite_source_file
if not overwrite_source_file:
Expand All @@ -741,13 +752,18 @@ def get_output_file_name_and_path(self, storage_path, gcode_comment_settings):
else:
# generate a new file name
file_name = utilities.remove_extension_from_filename(name)
new_display_name = utilities.remove_extension_from_filename(display_name)
file_extension = utilities.get_extension_from_filename(name)
display_extension = utilities.get_extension_from_filename(display_name)
new_name = "{0}{1}{2}.{3}".format(target_prefix, file_name, target_postfix, file_extension)
new_display_name = "{0}{1}{2}.{3}".format(target_prefix, new_display_name, target_postfix, display_extension)
if overwrite_source_file:
new_name = name
new_display_name = display_name

new_path = self._file_manager.join_path(FileDestinations.LOCAL, path, new_name)
return new_name, new_path

return new_name, new_path, new_display_name

def save_preprocessed_file(self, task, results, additional_metadata):
# get the file name and path
Expand All @@ -757,6 +773,7 @@ def save_preprocessed_file(self, task, results, additional_metadata):
source_path = octoprint_args["source_path"]
target_path = octoprint_args["target_path"]
target_name = octoprint_args["target_name"]
target_display_name = octoprint_args["target_display_name"]

if self._get_is_printing(target_path):
raise TargetFileSaveError("The source file will be overwritten, but it is currently printing, cannot overwrite.")
Expand All @@ -773,12 +790,13 @@ def save_preprocessed_file(self, task, results, additional_metadata):
"Unable to overwrite the target file, it is currently in use. Writing to new file."
)
# get a collision free filename and save it like that
target_directory, target_name = self._get_collision_free_filepath(target_path)
target_directory, target_name, target_display_name = self._get_collision_free_filepath(target_path, target_display_name)
target_path = self._file_manager.join_path(FileDestinations.LOCAL, target_directory, target_name)
logger.info("Saving target to new collision free path at %s", target_name)
task["octoprint_args"]["cant_overwrite"] = True
task["octoprint_args"]["target_path"] = target_path
task["octoprint_args"]["target_name"] = target_name
task["octoprint_args"]["target_display_name"] = target_display_name
else:
logger.info("Arc compression complete, creating a new gcode file: %s", target_name)

Expand All @@ -791,7 +809,7 @@ def save_preprocessed_file(self, task, results, additional_metadata):
target_path,
new_file_object,
allow_overwrite=True,
display=target_name,
display=target_display_name,
)
self._file_manager.set_additional_metadata(
FileDestinations.LOCAL, target_path, "arc_welder", True, overwrite=True, merge=False
Expand All @@ -802,6 +820,8 @@ def save_preprocessed_file(self, task, results, additional_metadata):
# add source and target name
metadata["source_name"] = source_name
metadata["target_name"] = target_name
metadata["target_display_name"] = target_display_name

self._file_manager.set_additional_metadata(
FileDestinations.LOCAL,
target_path,
Expand Down Expand Up @@ -885,12 +905,16 @@ def _remove_file_from_filemanager(self, path):
)
raise e

def _get_collision_free_filepath(self, path):
def _get_collision_free_filepath(self, path, display_name):
directory, filename = self._file_manager.split_path(FileDestinations.LOCAL, path)
extension = utilities.get_extension_from_filename(filename)
filename_no_extension = utilities.remove_extension_from_filename(filename)

display_extension = utilities.get_extension_from_filename(display_name)
display_name_no_extension = utilities.remove_extension_from_filename(display_name)

original_filename = filename_no_extension
original_display_name = display_name_no_extension
file_number = 0
# Check to see if the file exists, if it does add a number to the end and continue
while self._file_manager.file_exists(
Expand All @@ -903,8 +927,9 @@ def _get_collision_free_filepath(self, path):
):
file_number += 1
filename_no_extension = "{0}_{1}".format(original_filename, file_number)
display_name_no_extension = "{0}_{1}".format(original_display_name, file_number)

return directory, "{0}.{1}".format(filename_no_extension, extension)
return directory, "{0}.{1}".format(filename_no_extension, extension), "{0}.{1}".format(display_name_no_extension, extension)

def copy_thumbnail(self, thumbnail_src, thumbnail_path, gcode_filename):
# get the plugin implementation
Expand Down Expand Up @@ -1248,13 +1273,14 @@ def get_preprocessor_task(self, source_name, source_path, gcode_comment_settings
default_e_precision = 6

# determine the target file name and path
target_name, target_path = self.get_output_file_name_and_path(source_path, gcode_comment_settings)
target_name, target_path, target_display_name = self.get_output_file_name_and_path(source_name, source_path, gcode_comment_settings)
return {
"octoprint_args": {
"source_name": source_name,
"source_path": source_path,
"target_name": target_name,
"target_path": target_path,
"target_display_name": target_display_name,
"additional_metadata": additional_metadata,
"is_manual_request": is_manual_request,
"print_after_processing": print_after_processing,
Expand Down
4 changes: 3 additions & 1 deletion octoprint_arc_welder/static/js/arc_welder.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ $(function () {
self.statistics_compression_percent = ko.observable().extend({arc_welder_numeric: 1});
self.statistics_source_name = ko.observable();
self.statistics_target_name = ko.observable();
self.statistics_target_display_name = ko.observable();
self.statistics_guid = ko.observable();

/* Progress Observables */
Expand Down Expand Up @@ -1191,6 +1192,7 @@ $(function () {
self.statistics_compression_percent(statistics.compression_percent);
self.statistics_source_name(statistics.source_name || statistics.source_filename);
self.statistics_target_name(statistics.target_name || statistics.target_filename);
self.statistics_target_display_name(statistics.target_display_name || self.statistics_target_name());
self.statistics_guid(statistics.guid);
}
if (is_welded)
Expand Down Expand Up @@ -1667,7 +1669,7 @@ $(function () {
// disable the element
$(event.target).addClass("disabled");
// Request that the file be processed
var data = { "path": encodeURI(file_data.path), "origin": file_data.origin};
var data = { "path": encodeURI(file_data.path), "origin": file_data.origin, "name": encodeURI(file_data.name)};
$.ajax({
url: ArcWelder.APIURL("process"),
type: "POST",
Expand Down
6 changes: 5 additions & 1 deletion octoprint_arc_welder/static/js/pnotify_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ $(function () {
self.subtitle = subtitle;
self.close = function () {
if (self.loader != null)
{
self.loader.remove();
};
};

self.update = function (percent_complete, progress_text) {
self.notice.find(".remove_button").remove();

if (self.$progress == null)
{
return null;
};
if (percent_complete < 0)
percent_complete = 0;
if (percent_complete > 100)
Expand Down Expand Up @@ -86,7 +90,7 @@ $(function () {
self.loader = new PNotify({
title: title,
/*text: '<div class="progress-sub-title"></div><div class="progress progress-striped active" style="margin:0"><div class="arc-welder progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0"></div></div><div class="progress-text" style="width:100%;"></div></div>',*/
text: '<div class="progress-bar-container" style="margin:0"><div class="progress-bar-info text-center"><span class="progress-bar-text"></span></div><div class="progress-bar" style="margin:0" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0"></div></div><div class="progress-text" style="width:100%;"></div></div>',
text: '<div class="progress-bar-container" style="margin:0"><div class="progress-bar-info text-center"><span class="progress-bar-text"></span></div><div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0"></div></div><div class="progress-text" style="width:100%;"></div></div>',
addclass: "arc-welder",
icon: 'fa fa-cog fa-spin',
width: self.popup_width.toString() + "px",
Expand Down
2 changes: 1 addition & 1 deletion octoprint_arc_welder/templates/arc_welder_tab.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
</div>
<div class="row-fluid">
<div class="span4 text-right"><strong>Target File:</strong></div>
<div class="span8"><sapn data-bind="text:statistics_target_name, attr:{title:statistics_target_name}"></sapn></div>
<div class="span8"><sapn data-bind="text:statistics_target_display_name, attr:{title:statistics_target_display_name}"></sapn></div>
</div>
<div class="row-fluid">
<div class="span4 text-right"><strong>Arcs:</strong></div>
Expand Down

0 comments on commit b6a2f27

Please sign in to comment.