Skip to content

Commit

Permalink
Send board parameter, service and version as post form data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjell Morgenstern committed Dec 13, 2023
1 parent 11dd2c4 commit bfc3ad3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
42 changes: 26 additions & 16 deletions src/dialogs/fabuploadprogress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,30 @@ void FabUploadProgress::init(QNetworkAccessManager *manager, QString filename,
void FabUploadProgress::doUpload()
{
QSettings settings;
QString service = settings.value("service", "fritzing").toString();
mService = settings.value("service", "fritzing").toString();
QUrl upload_url("https://service.fritzing.org/fab/upload");

QUrlQuery query;
QString fritzingVersion = Version::versionString();
query.addQueryItem("fritzing_version", fritzingVersion);
query.addQueryItem("width", QString::number(mWidth));
query.addQueryItem("height", QString::number(mHeight));
query.addQueryItem("board_count", QString::number(mBoardCount));
query.addQueryItem("board_title", QString::fromUtf8(mBoardTitle.toUtf8().toBase64()));

settings.beginGroup("sketches");
QVariant settingValue = settings.value(mFilepath);
settings.endGroup();

if (auto opt = settingValue.value<UploadPair>(); settingValue.isValid() && !settingValue.isNull()) {
service = std::move(opt.service);
mService = std::move(opt.service);
if (!opt.link.isEmpty()) {
QUrl potential_url(opt.link);
if (potential_url.isValid()) {
upload_url = potential_url;
upload_url.setQuery(query);
uploadMultipart(upload_url, mFilepath);
uploadMultipart(upload_url.toString(), mFilepath);
return;
}
// Otherwise, keep using the default URL
}
}

query.addQueryItem("service", service);
QUrlQuery query;
QString fritzingVersion = Version::versionString();
query.addQueryItem("fritzing_version", fritzingVersion);
query.addQueryItem("service", mService);
upload_url.setQuery(query);
mRedirect_url = QString();

Expand Down Expand Up @@ -113,7 +107,7 @@ void FabUploadProgress::onRequestUploadFinished()
auto d = reply->readAll();
auto j = NetworkHelper::string_to_hash(d);
QUrl upload_url(QUrl::fromUserInput(j["upload_url"].toString()));
uploadMultipart(upload_url, mFilepath);
uploadMultipart(upload_url.toString(), mFilepath);
}
} else {
httpError(reply);
Expand All @@ -122,9 +116,25 @@ void FabUploadProgress::onRequestUploadFinished()
}


void FabUploadProgress::uploadMultipart(const QUrl &url, const QString &file_path)
void FabUploadProgress::uploadMultipart(const QString &urlStr, const QString &file_path)
{
auto *httpMultiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

QVector<QPair<QString, QString>> params;
params.append(qMakePair("fritzing_version", Version::versionString()));
params.append(qMakePair("service", mService));
params.append(qMakePair("width", QString::number(mWidth)));
params.append(qMakePair("height", QString::number(mHeight)));
params.append(qMakePair("board_count", QString::number(mBoardCount)));
params.append(qMakePair("board_title", QString::fromUtf8(mBoardTitle.toUtf8().toBase64())));

for (const QPair<QString, QString> &param : params) {
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"" + param.first + "\""));
textPart.setBody(param.second.toUtf8());
httpMultiPart->append(textPart);
}

auto *file = new QFile(file_path);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"upload[file]\"; filename=\"" + QFileInfo(*file).fileName() + "\""));
Expand All @@ -136,7 +146,7 @@ void FabUploadProgress::uploadMultipart(const QUrl &url, const QString &file_pat

httpMultiPart->append(imagePart);

QNetworkRequest request(url);
QNetworkRequest request((QUrl(urlStr)));
QNetworkReply *reply = mManager->post(request, httpMultiPart);

httpMultiPart->setParent(reply); // delete the multiPart with the reply
Expand Down
3 changes: 2 additions & 1 deletion src/dialogs/fabuploadprogress.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ private Q_SLOTS:
double mHeight;
int mBoardCount;
QString mBoardTitle;
QString mService;

void uploadMultipart(const QUrl &url, const QString &file_path);
void uploadMultipart(const QString &url, const QString &file_path);
void checkProcessingStatus(QUrl url);
void httpError(QNetworkReply *reply);
void apiError(QString message);
Expand Down

0 comments on commit bfc3ad3

Please sign in to comment.