Make S3 bucket configurable

This commit is contained in:
Yuriy Puchkov
2020-06-30 12:27:52 +03:00
parent 099b9efa72
commit 46a42b4526
9 changed files with 49 additions and 72 deletions

View File

@@ -1,7 +0,0 @@
# Use default ImgS3 client_id if user did not pass
# this variable to qmake
isEmpty(IMG_S3_CLIENT_ID) {
IMG_S3_CLIENT_ID = "313baf0c7b4d3f0"
}
DEFINES += IMG_S3_CLIENT_ID=\\\"$${IMG_S3_CLIENT_ID}\\\"

View File

@@ -42,9 +42,15 @@
#include <QJsonObject>
#include <QHttpMultiPart>
ImgS3Uploader::ImgS3Uploader(const QPixmap &capture, QWidget *parent) :
ImgS3Uploader::ImgS3Uploader(const QPixmap &capture,
const QString &s3CredsUrl,
const QString &s3XApiKey,
QWidget *parent) :
QWidget(parent), m_pixmap(capture)
{
m_s3CredsUrl = s3CredsUrl;
m_s3XApiKey = s3XApiKey;
setWindowTitle(tr("Upload to ImgS3"));
setWindowIcon(QIcon(":img/app/flameshot.svg"));
@@ -108,7 +114,12 @@ void ImgS3Uploader::handleCredsReply(QNetworkReply *reply){
QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
uploadToS3(response);
} else {
m_infoLabel->setText(reply->errorString());
if(m_s3CredsUrl.length() == 0){
m_infoLabel->setText("S3 Creds URL is not found in your configuration file");
}
else {
m_infoLabel->setText(reply->errorString());
}
}
new QShortcut(Qt::Key_Escape, this, SLOT(close()));
}
@@ -209,8 +220,11 @@ void ImgS3Uploader::uploadToS3(QJsonDocument &response) {
void ImgS3Uploader::upload() {
// get creads
QUrl creds("https://api.img.rnd.namecheap.net/");
QUrl creds(m_s3CredsUrl);
QNetworkRequest requestCreds(creds);
if(m_s3XApiKey.length() > 0) {
requestCreds.setRawHeader(QByteArray("X-API-Key"), QByteArray(m_s3XApiKey.toLocal8Bit()));
}
m_NetworkAMCreds->get(requestCreds);
}

View File

@@ -33,7 +33,11 @@ class NotificationWidget;
class ImgS3Uploader : public QWidget {
Q_OBJECT
public:
explicit ImgS3Uploader(const QPixmap &capture, QWidget *parent = nullptr);
explicit ImgS3Uploader(const QPixmap &capture,
const QString &s3CredsUrl,
const QString &s3XApiKey,
QWidget *parent = nullptr
);
private slots:
void handleReply(QNetworkReply *reply);
@@ -49,6 +53,9 @@ private:
void uploadToS3(QJsonDocument &response);
private:
QString m_s3CredsUrl;
QString m_s3XApiKey;
QString m_hostName;
QPixmap m_pixmap;
QNetworkAccessManager *m_NetworkAM;

View File

@@ -17,10 +17,17 @@
#include "imgs3uploadertool.h"
#include "imgs3uploader.h"
#include <QDebug>
#include <QDir>
#include <QPainter>
#include <QSettings>
ImgS3UploaderTool::ImgS3UploaderTool(QObject *parent) : AbstractActionTool(parent) {
QSettings *pSettings = new QSettings(QDir::currentPath() + "/config.ini", QSettings::IniFormat);
pSettings->beginGroup("S3");
m_s3CredsUrl = pSettings->value("S3_CREDS_URL").toString();
m_s3XApiKey = pSettings->value("S3_X_API_KEY").toString();
}
bool ImgS3UploaderTool::closeOnButtonPressed() const {
@@ -44,7 +51,7 @@ QString ImgS3UploaderTool::description() const {
}
QWidget* ImgS3UploaderTool::widget() {
return new ImgS3Uploader(capture);
return new ImgS3Uploader(capture, m_s3CredsUrl, m_s3XApiKey);
}
CaptureTool* ImgS3UploaderTool::copy(QObject *parent) {

View File

@@ -40,4 +40,6 @@ public slots:
private:
QPixmap capture;
QString m_s3CredsUrl;
QString m_s3XApiKey;
};