Create uploader to i.nuuls.com (#1332)

This commit adds support for uploading images to i.nuuls.com from clipboard or by dragging an image into a split.

Documentation for how to self-host the image uploader is available in ENV.md

By default, you will be asked before an image upload takes place. There's an option in the dialog to not be asked again, if that option is chosen you can revert that choice in the settings dialog.
This commit is contained in:
pajlada
2020-05-09 07:34:27 -04:00
committed by GitHub
13 changed files with 323 additions and 7 deletions
+10 -7
View File
@@ -1,8 +1,11 @@
#include "widgets/helper/ResizingTextEdit.hpp"
#include "common/Common.hpp"
#include "common/CompletionModel.hpp"
#include "singletons/Settings.hpp"
#include <QMimeData>
namespace chatterino {
ResizingTextEdit::ResizingTextEdit()
@@ -257,20 +260,20 @@ void ResizingTextEdit::insertCompletion(const QString &completion)
bool ResizingTextEdit::canInsertFromMimeData(const QMimeData *source) const
{
if (source->hasImage())
{
return false;
}
else if (source->hasFormat("text/plain"))
if (source->hasImage() || source->hasFormat("text/plain"))
{
return true;
}
return false;
return QTextEdit::canInsertFromMimeData(source);
}
void ResizingTextEdit::insertFromMimeData(const QMimeData *source)
{
if (!source->hasImage())
if (source->hasImage() || source->hasUrls())
{
this->imagePasted.invoke(source);
}
else
{
insertPlainText(source->text());
}
+1
View File
@@ -19,6 +19,7 @@ public:
pajlada::Signals::Signal<QKeyEvent *> keyPressed;
pajlada::Signals::NoArgSignal focused;
pajlada::Signals::NoArgSignal focusLost;
pajlada::Signals::Signal<const QMimeData *> imagePasted;
void setCompleter(QCompleter *c);
QCompleter *getCompleter() const;
@@ -545,6 +545,9 @@ void GeneralPage::initLayout(SettingsLayout &layout)
layout.addCheckbox(
"Hide viewercount and stream length while hovering the split",
s.hideViewerCountAndDuration);
layout.addCheckbox(
"Ask for confirmation when uploading an image to i.nuuls.com",
s.askOnImageUpload);
layout.addTitle("Cache");
layout.addDescription(
+53
View File
@@ -1,6 +1,7 @@
#include "widgets/splits/Split.hpp"
#include "common/Common.hpp"
#include "common/Env.hpp"
#include "common/NetworkRequest.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/EmoteValue.hpp"
@@ -11,6 +12,7 @@
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp"
#include "util/Clipboard.hpp"
#include "util/NuulsUploader.hpp"
#include "util/Shortcut.hpp"
#include "util/StreamLink.hpp"
#include "widgets/Notebook.hpp"
@@ -205,6 +207,34 @@ Split::Split(QWidget *parent)
[this] { this->focused.invoke(); });
this->input_->ui_.textEdit->focusLost.connect(
[this] { this->focusLost.invoke(); });
this->input_->ui_.textEdit->imagePasted.connect([this](const QMimeData
*source) {
if (getSettings()->askOnImageUpload.getValue())
{
QMessageBox msgBox;
msgBox.setText("Image upload");
msgBox.setInformativeText(
"You are uploading an image to i.nuuls.com. You won't be able "
"to remove the image from the site. Are you okay with this?");
msgBox.addButton(QMessageBox::Cancel);
msgBox.addButton(QMessageBox::Yes);
msgBox.addButton("Yes, don't ask again", QMessageBox::YesRole);
msgBox.setDefaultButton(QMessageBox::Yes);
auto picked = msgBox.exec();
if (picked == QMessageBox::Cancel)
{
return;
}
else if (picked == 0) // don't ask again button
{
getSettings()->askOnImageUpload.setValue(false);
}
}
upload(source, this->getChannel(), *this->input_->ui_.textEdit);
});
setAcceptDrops(true);
}
Split::~Split()
@@ -700,6 +730,29 @@ void Split::reloadChannelAndSubscriberEmotes()
}
}
void Split::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasImage() || event->mimeData()->hasUrls())
{
event->acceptProposedAction();
}
else
{
BaseWidget::dragEnterEvent(event);
}
}
void Split::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasImage() || event->mimeData()->hasUrls())
{
this->input_->ui_.textEdit->imagePasted.invoke(event->mimeData());
}
else
{
BaseWidget::dropEvent(event);
}
}
template <typename Iter, typename RandomGenerator>
static Iter select_randomly(Iter start, Iter end, RandomGenerator &g)
{
+3
View File
@@ -86,6 +86,9 @@ protected:
void leaveEvent(QEvent *event) override;
void focusInEvent(QFocusEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;
private:
void channelNameUpdated(const QString &newChannelName);
void handleModifiers(Qt::KeyboardModifiers modifiers);