Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor downloading feature and support makerworld.com and thingerverse.com model opening #5486

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/libslic3r/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <functional>
#include <type_traits>
#include <system_error>
#include <regex>

#include <boost/system/error_code.hpp>
#include <boost/algorithm/string.hpp>
Expand Down Expand Up @@ -220,6 +221,30 @@ extern bool is_shapes_dir(const std::string& dir);
//BBS: add json support
extern bool is_json_file(const std::string& path);

// Orca: custom protocal support utils
inline bool is_orca_open(const std::string& url) { return boost::starts_with(url, "orcaslicer://open"); }
inline bool is_prusaslicer_open(const std::string& url) { return boost::starts_with(url, "prusaslicer://open"); }
inline bool is_bambustudio_open(const std::string& url) { return boost::starts_with(url, "bambustudio://open"); }
inline bool is_cura_open(const std::string& url) { return boost::starts_with(url, "cura://open"); }
inline bool is_supported_open_protocol(const std::string& url) { return is_orca_open(url) || is_prusaslicer_open(url) || is_bambustudio_open(url) || is_cura_open(url); }
inline bool is_printables_link(const std::string& url) {
const std::regex url_regex("(http|https)://printables.com", std::regex_constants::icase);
return std::regex_match(url, url_regex);
}
inline bool is_makerworld_link(const std::string& url) {
const std::regex url_regex("(http|https)://makerworld.com", std::regex_constants::icase);
return std::regex_match(url, url_regex);
}
inline bool is_thingiverse_link(const std::string& url) {
const std::regex url_regex("(http|https)://www.thingiverse.com", std::regex_constants::icase);
return std::regex_match(url, url_regex);
}

// sanitize a string to be used as a filename
inline std::string sanitize_filename(const std::string &filename){
const std::regex special_chars("[/\\\\:*?\"<>|]");
return std::regex_replace(filename, special_chars, "_");
}
// File path / name / extension splitting utilities, working with UTF-8,
// to be published to Perl.
namespace PerlUtils {
Expand Down
31 changes: 13 additions & 18 deletions src/slic3r/GUI/Downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void Downloader::start_download(const std::string& full_url)

// Orca: Replace PS workaround for "mysterious slash" with a more dynamic approach
// Windows seems to have fixed the issue and this provides backwards compatability for those it still affects
boost::regex re(R"(^(orcaslicer|prusaslicer):\/\/open[\/]?\?file=)", boost::regbase::icase);
boost::regex re(R"(^(orcaslicer|prusaslicer|bambustudio|cura):\/\/open[\/]?\?file=)", boost::regbase::icase);
boost::smatch results;

if (!boost::regex_search(full_url, results, re)) {
Expand All @@ -161,23 +161,18 @@ void Downloader::start_download(const std::string& full_url)
}
size_t id = get_next_id();
std::string escaped_url = FileGet::escape_url(full_url.substr(results.length()));
// Orca:: any website that supports orcaslicer://open/?file= can be downloaded

// if (!boost::starts_with(escaped_url, "https://") || !FileGet::is_subdomain(escaped_url, "printables.com")) {
// std::string msg = format(_L("Download won't start. Download URL doesn't point to https://printables.com : %1%"), escaped_url);
// BOOST_LOG_TRIVIAL(error) << msg;
// NotificationManager* ntf_mngr = wxGetApp().notification_manager();
// ntf_mngr->push_notification(NotificationType::CustomNotification, NotificationManager::NotificationLevel::ErrorNotificationLevel,
// "Download failed. Download URL doesn't point to https://printables.com.");
// return;
// }

std::string text(escaped_url);
m_downloads.emplace_back(std::make_unique<Download>(id, std::move(escaped_url), this, m_dest_folder));
NotificationManager* ntf_mngr = wxGetApp().notification_manager();
ntf_mngr->push_download_URL_progress_notification(id, m_downloads.back()->get_filename(), std::bind(&Downloader::user_action_callback, this, std::placeholders::_1, std::placeholders::_2));
m_downloads.back()->start();
BOOST_LOG_TRIVIAL(debug) << "started download";
if (is_bambustudio_open(full_url) || (is_orca_open(full_url) && is_makerworld_link(full_url)))
plater->request_model_download(escaped_url);
else {
std::string text(escaped_url);
m_downloads.emplace_back(std::make_unique<Download>(id, std::move(escaped_url), this, m_dest_folder));
NotificationManager* ntf_mngr = wxGetApp().notification_manager();
ntf_mngr->push_download_URL_progress_notification(id, m_downloads.back()->get_filename(),
std::bind(&Downloader::user_action_callback, this, std::placeholders::_1,
std::placeholders::_2));
m_downloads.back()->start();
}
BOOST_LOG_TRIVIAL(debug) << "started download";
}

void Downloader::on_progress(wxCommandEvent& event)
Expand Down
47 changes: 38 additions & 9 deletions src/slic3r/GUI/DownloaderFileGet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "format.hpp"
#include "GUI.hpp"
#include "I18N.hpp"
#include "libslic3r/Utils.hpp"

namespace Slic3r {
namespace GUI {
Expand Down Expand Up @@ -124,7 +125,15 @@ FileGet::priv::priv(int ID, std::string&& url, const std::string& filename, wxEv
, m_dest_folder(dest_folder)
{
}

std::string extract_remote_filename(const std::string& str) {
std::regex r("filename=\"([^\"]*)\"");
std::smatch match;
if (std::regex_search(str.begin(), str.end(), match, r)) {
return match.str(1);
} else {
return "";
}
}
void FileGet::priv::get_perform()
{
assert(m_evt_handler);
Expand All @@ -135,10 +144,11 @@ void FileGet::priv::get_perform()
m_stopped = false;

// open dest file
std::string extension;
if (m_written == 0)
{
boost::filesystem::path dest_path = m_dest_folder / m_filename;
std::string extension = boost::filesystem::extension(dest_path);
extension = dest_path.extension().string();
std::string just_filename = m_filename.substr(0, m_filename.size() - extension.size());
std::string final_filename = just_filename;
// Find unsed filename
Expand All @@ -164,18 +174,20 @@ void FileGet::priv::get_perform()
m_evt_handler->QueueEvent(evt);
return;
}

m_filename = final_filename + extension;

m_tmp_path = m_dest_folder / (m_filename + "." + std::to_string(get_current_pid()) + ".download");
m_filename = sanitize_filename(final_filename + extension);

m_tmp_path = m_dest_folder / (m_filename + "." + std::to_string(get_current_pid()) + ".download");

wxCommandEvent* evt = new wxCommandEvent(EVT_DWNLDR_FILE_NAME_CHANGE);
evt->SetString(boost::nowide::widen(m_filename));
evt->SetInt(m_id);
m_evt_handler->QueueEvent(evt);
}

boost::filesystem::path dest_path = m_dest_folder / m_filename;
boost::filesystem::path dest_path;
if(!extension.empty())
dest_path = m_dest_folder / m_filename;

wxString temp_path_wstring(m_tmp_path.wstring());

Expand Down Expand Up @@ -208,6 +220,19 @@ void FileGet::priv::get_perform()
Http::get(m_url)
.size_limit(DOWNLOAD_SIZE_LIMIT) //more?
.set_range(range_string)
.on_header_callback([&](std::string header) {
if(dest_path.empty()) {
std::string filename = extract_remote_filename(header);
if (!filename.empty()) {
m_filename = filename;
dest_path = m_dest_folder / m_filename;
wxCommandEvent* evt = new wxCommandEvent(EVT_DWNLDR_FILE_NAME_CHANGE);
evt->SetString(boost::nowide::widen(m_filename));
evt->SetInt(m_id);
m_evt_handler->QueueEvent(evt);
}
}
})
.on_progress([&](Http::Progress progress, bool& cancel) {
// to prevent multiple calls into following ifs (m_cancel / m_pause)
if (m_stopped){
Expand Down Expand Up @@ -292,14 +317,18 @@ void FileGet::priv::get_perform()
//}
try
{
/*
// Orca: thingiverse need this
if (m_written < body.size())
{
// this code should never be entered. As there should be on_progress call after last bit downloaded.
std::string part_for_write = body.substr(m_written);
fwrite(part_for_write.c_str(), 1, part_for_write.size(), file);
}
*/

wxCommandEvent* evt = new wxCommandEvent(EVT_DWNLDR_FILE_PROGRESS);
evt->SetString(std::to_string(100));
evt->SetInt(m_id);
m_evt_handler->QueueEvent(evt);
}
fclose(file);
boost::filesystem::rename(m_tmp_path, dest_path);
}
Expand Down
100 changes: 16 additions & 84 deletions src/slic3r/GUI/GUI_App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,63 +804,39 @@ void GUI_App::post_init()

m_open_method = "double_click";
bool switch_to_3d = false;
if (!this->init_params->input_files.empty()) {

if (!this->init_params->input_files.empty()) {

BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", init with input files, size %1%, input_gcode %2%")
%this->init_params->input_files.size() %this->init_params->input_gcode;



if (this->init_params->input_files.size() == 1 &&
(boost::starts_with(this->init_params->input_files.front(), "orcaslicer://open") ||
boost::starts_with(this->init_params->input_files.front(), "prusaslicer://open"))) {

if (boost::starts_with(this->init_params->input_files.front(), "orcaslicer://open")||
boost::starts_with(this->init_params->input_files.front(), "prusaslicer://open")) {
switch_to_3d = true;
start_download(this->init_params->input_files.front());
} else if (vector<string> input_str_arr = split_str(this->init_params->input_files.front(), "orcaslicer://open/?file="); input_str_arr.size() > 1) {
std::string download_origin_url;
for (auto input_str : input_str_arr) {
if (!input_str.empty())
download_origin_url = input_str;
}

std::string download_file_url = url_decode(download_origin_url);
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << download_file_url;
if (!download_file_url.empty() &&
(boost::starts_with(download_file_url, "http://") || boost::starts_with(download_file_url, "https://"))) {
request_model_download(download_file_url);
}
}
m_open_method = "makerworld";
}
else {
const auto first_url = this->init_params->input_files.front();
if (this->init_params->input_files.size() == 1 && is_supported_open_protocol(first_url)) {
switch_to_3d = true;
start_download(first_url);
m_open_method = "url";
} else {
switch_to_3d = true;
if (this->init_params->input_gcode) {
mainframe->select_tab(size_t(MainFrame::tp3DEditor));
plater_->select_view_3D("3D");
this->plater()->load_gcode(from_u8(this->init_params->input_files.front()));
m_open_method = "gcode";
}
else {
} else {
mainframe->select_tab(size_t(MainFrame::tp3DEditor));
plater_->select_view_3D("3D");
wxArrayString input_files;
for (auto & file : this->init_params->input_files) {
for (auto& file : this->init_params->input_files) {
input_files.push_back(wxString::FromUTF8(file));
}
this->plater()->set_project_filename(_L("Untitled"));
this->plater()->load_files(input_files);
try {
if (!input_files.empty()) {
std::string file_path = input_files.front().ToStdString();
std::string file_path = input_files.front().ToStdString();
std::filesystem::path path(file_path);
m_open_method = "file_" + path.extension().string();
}
}
catch (...) {
} catch (...) {
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ", file path exception!";
m_open_method = "file";
}
Expand Down Expand Up @@ -2630,10 +2606,6 @@ bool GUI_App::on_init_inner()
#endif
this->post_init();

if (!m_download_file_url.empty()) {
request_model_download(m_download_file_url);
m_download_file_url = "";
}
update_publish_status();
}

Expand Down Expand Up @@ -4099,27 +4071,9 @@ void GUI_App::on_user_login_handle(wxCommandEvent &evt)
void GUI_App::check_track_enable()
{
// Orca: alaways disable track event
return;
if (app_config && app_config->get("firstguide", "privacyuse") == "true") {
//enable track event
json header_json;
header_json["ver"] = SLIC3R_VERSION;
wxString os_desc = wxGetOsDescription();
int major = 0, minor = 0, micro = 0;
header_json["os"] = std::string(os_desc.ToUTF8());
header_json["name"] = std::string(SLIC3R_APP_NAME);
header_json["uuid"] = app_config->get("slicer_uuid");
if (m_agent) {
m_agent->track_enable(true);
m_agent->track_header(header_json.dump());
}
/* record studio start event */
json j;
j["user_mode"] = this->get_mode_str();
j["open_method"] = m_open_method;
if (m_agent) {
m_agent->track_event("studio_launch", j.dump());
}
if (m_agent) {
m_agent->track_enable(false);
m_agent->track_remove_files();
}
}

Expand Down Expand Up @@ -5879,32 +5833,9 @@ void GUI_App::OSXStoreOpenFiles(const wxArrayString &fileNames)

void GUI_App::MacOpenURL(const wxString& url)
{
BOOST_LOG_TRIVIAL(trace) << __FUNCTION__ << "get mac url " << url;

if (url.empty())
return;

if (boost::starts_with(url, "orcasliceropen://")) {
auto input_str_arr = split_str(url.ToStdString(), "orcasliceropen://");

std::string download_origin_url;
for (auto input_str : input_str_arr) {
if (!input_str.empty()) download_origin_url = input_str;
}

std::string download_file_url = url_decode(download_origin_url);
BOOST_LOG_TRIVIAL(trace) << __FUNCTION__ << download_file_url;
if (!download_file_url.empty() && (boost::starts_with(download_file_url, "http://") || boost::starts_with(download_file_url, "https://"))) {

if (m_post_initialized) {
request_model_download(download_file_url);
}
else {
m_download_file_url = download_file_url;
}
}
} else if (boost::starts_with(url, "orcaslicer://"))
start_download(boost::nowide::narrow(url));
start_download(boost::nowide::narrow(url));
}

// wxWidgets override to get an event on open files.
Expand Down Expand Up @@ -6703,6 +6634,7 @@ void GUI_App::start_download(std::string url)
}
m_downloader->init(dest_folder);
m_downloader->start_download(url);

}

bool is_support_filament(int extruder_id)
Expand Down
2 changes: 0 additions & 2 deletions src/slic3r/GUI/GUI_App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ class GUI_App : public wxApp
bool m_opengl_initialized{ false };
#endif

//import model from mall
wxString m_download_file_url;

//#ifdef _WIN32
wxColour m_color_label_modified;
Expand Down
2 changes: 1 addition & 1 deletion src/slic3r/GUI/InstanceCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ void OtherInstanceMessageHandler::handle_message(const std::string& message)

std::vector<boost::filesystem::path> paths;
std::vector<std::string> downloads;
boost::regex re(R"(^(orcaslicer|prusaslicer):\/\/open[\/]?\?file=)", boost::regbase::icase);
boost::regex re(R"(^(orcaslicer|prusaslicer|cura|bambustudio):\/\/open[\/]?\?file=)", boost::regbase::icase);
boost::smatch results;

// Skip the first argument, it is the path to the slicer executable.
Expand Down
Loading
Loading