Various improvements to SimplyPrint integration (#4831)

* Allow using BBL's device tab when 3rd party print host is used

* Add option to open SimplyPrint panel in device tab after uploading

* Fix default print host for prusa connect

* Do not set api key in device view when SimplyPrint is used

* Sending 3mf file to SimplyPrint when using BBL printers

* Fix file extension when uploading 3mf

* Prepare for large file uploading

* Implement chunk upload

* Fix file uploading exceeding content size

* Fix wrong field type

* Add `temp=true` to all chunk upload calls

* Add macro to enable test api

* Merge branch 'main' into dev/simplyprint-improve

* Fix another missing `temp=true`

* Add delete token

* Try fixing build error on *nix systems

* Merge branch 'main' into dev/simplyprint-improve

* Merge branch 'main' into dev/simplyprint-improve

* Merge remote-tracking branch 'remote/main' into dev/simplyprint-improve

# Conflicts:
#	src/slic3r/GUI/BackgroundSlicingProcess.cpp

* Move the `bbl_use_print_host_webui` option to print host dialog. Also make it a derived option of `print_host_webui` instead.

* Merge branch 'main' into dev/simplyprint-improve

# Conflicts:
#	src/slic3r/GUI/MainFrame.cpp
#	src/slic3r/GUI/Plater.cpp

* Merge branch 'main' into dev/simplyprint-improve

# Conflicts:
#	src/slic3r/GUI/Plater.cpp

* Use a more generic option instead of SimplyPrint specific

* Merge branch 'main' into dev/simplyprint-improve

* Merge branch 'main' into dev/simplyprint-improve
This commit is contained in:
Noisyfox 2024-06-21 19:48:00 +08:00 committed by GitHub
parent b7a0b30578
commit cd6cd0786f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 505 additions and 110 deletions

View file

@ -86,6 +86,13 @@ std::unique_ptr<CurlGlobalInit> CurlGlobalInit::instance;
std::map<std::string, std::string> extra_headers;
std::mutex g_mutex;
struct form_file
{
fs::ifstream ifs;
boost::filesystem::ifstream::off_type init_offset;
size_t content_length;
};
struct Http::priv
{
enum {
@ -103,7 +110,7 @@ struct Http::priv
std::string buffer;
// Used for storing file streams added as multipart form parts
// Using a deque here because unlike vector it doesn't ivalidate pointers on insertion
std::deque<fs::ifstream> form_files;
std::deque<form_file> form_files;
std::string postfields;
std::string error_buffer; // Used for CURLOPT_ERRORBUFFER
std::string headers;
@ -130,7 +137,7 @@ struct Http::priv
void set_timeout_connect(long timeout);
void set_timeout_max(long timeout);
void form_add_file(const char *name, const fs::path &path, const char* filename);
void form_add_file(const char *name, const fs::path &path, const char* filename, boost::filesystem::ifstream::off_type offset, size_t length);
/* mime */
void mime_form_add_text(const char* name, const char* value);
void mime_form_add_file(const char* name, const char* path);
@ -254,15 +261,27 @@ int Http::priv::xfercb_legacy(void *userp, double dltotal, double dlnow, double
size_t Http::priv::form_file_read_cb(char *buffer, size_t size, size_t nitems, void *userp)
{
auto stream = reinterpret_cast<fs::ifstream*>(userp);
auto f = reinterpret_cast<form_file*>(userp);
try {
stream->read(buffer, size * nitems);
size_t max_read_size = size * nitems;
if (f->content_length == 0) {
// Unlimited
f->ifs.read(buffer, max_read_size);
} else {
unsigned long long read_size = f->ifs.tellg() - f->init_offset;
if (read_size >= f->content_length) {
return 0;
}
max_read_size = std::min(max_read_size, size_t(f->content_length - read_size));
f->ifs.read(buffer, max_read_size);
}
} catch (const std::exception &) {
return CURL_READFUNC_ABORT;
}
return stream->gcount();
return f->ifs.gcount();
}
size_t Http::priv::headers_cb(char *buffer, size_t size, size_t nitems, void *userp)
@ -286,7 +305,7 @@ void Http::priv::set_timeout_max(long timeout)
::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
}
void Http::priv::form_add_file(const char *name, const fs::path &path, const char* filename)
void Http::priv::form_add_file(const char *name, const fs::path &path, const char* filename, boost::filesystem::ifstream::off_type offset, size_t length)
{
// We can't use CURLFORM_FILECONTENT, because curl doesn't support Unicode filenames on Windows
// and so we use CURLFORM_STREAM with boost ifstream to read the file.
@ -295,18 +314,21 @@ void Http::priv::form_add_file(const char *name, const fs::path &path, const cha
filename = path.string().c_str();
}
form_files.emplace_back(path, std::ios::in | std::ios::binary);
auto &stream = form_files.back();
stream.seekg(0, std::ios::end);
size_t size = stream.tellg();
stream.seekg(0);
form_files.emplace_back(form_file{{path, std::ios::in | std::ios::binary}, offset, length});
auto &f = form_files.back();
size_t size = length;
if (length == 0) {
f.ifs.seekg(0, std::ios::end);
size = f.ifs.tellg() - offset;
}
f.ifs.seekg(offset);
if (filename != nullptr) {
::curl_formadd(&form, &form_end,
CURLFORM_COPYNAME, name,
CURLFORM_FILENAME, filename,
CURLFORM_CONTENTTYPE, "application/octet-stream",
CURLFORM_STREAM, static_cast<void*>(&stream),
CURLFORM_STREAM, static_cast<void*>(&f),
CURLFORM_CONTENTSLENGTH, static_cast<long>(size),
CURLFORM_END
);
@ -587,9 +609,9 @@ Http& Http::form_add(const std::string &name, const std::string &contents)
return *this;
}
Http& Http::form_add_file(const std::string &name, const fs::path &path)
Http& Http::form_add_file(const std::string &name, const fs::path &path, boost::filesystem::ifstream::off_type offset, size_t length)
{
if (p) { p->form_add_file(name.c_str(), path.c_str(), nullptr); }
if (p) { p->form_add_file(name.c_str(), path.c_str(), nullptr, offset, length); }
return *this;
}
@ -607,15 +629,15 @@ Http& Http::mime_form_add_file(std::string &name, const char* path)
}
Http& Http::form_add_file(const std::wstring& name, const fs::path& path)
Http& Http::form_add_file(const std::wstring& name, const fs::path& path, boost::filesystem::ifstream::off_type offset, size_t length)
{
if (p) { p->form_add_file((char*)name.c_str(), path.c_str(), nullptr); }
if (p) { p->form_add_file((char*)name.c_str(), path.c_str(), nullptr, offset, length); }
return *this;
}
Http& Http::form_add_file(const std::string &name, const fs::path &path, const std::string &filename)
Http& Http::form_add_file(const std::string &name, const fs::path &path, const std::string &filename, boost::filesystem::ifstream::off_type offset, size_t length)
{
if (p) { p->form_add_file(name.c_str(), path.c_str(), filename.c_str()); }
if (p) { p->form_add_file(name.c_str(), path.c_str(), filename.c_str(), offset, length); }
return *this;
}