| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "mpform.h" |
| 8 | #include <KIO/StoredTransferJob> |
| 9 | #include <KIO/TransferJob> |
| 10 | #include <KJob> |
| 11 | #include <KLocalizedString> |
| 12 | #include <KNotification> |
| 13 | #include <KPluginFactory> |
| 14 | #include <QClipboard> |
| 15 | #include <QDebug> |
| 16 | #include <QGuiApplication> |
| 17 | #include <QJsonArray> |
| 18 | #include <QJsonDocument> |
| 19 | #include <QJsonObject> |
| 20 | #include <QStandardPaths> |
| 21 | #include <purpose/pluginbase.h> |
| 22 | |
| 23 | Q_GLOBAL_STATIC_WITH_ARGS(const QUrl, imageImgurUrl, (QLatin1String("https://api.imgur.com/3/image" ))) |
| 24 | Q_GLOBAL_STATIC_WITH_ARGS(const QUrl, albumImgurUrl, (QLatin1String("https://api.imgur.com/3/album" ))) |
| 25 | |
| 26 | // key associated with aleixpol@kde.org |
| 27 | Q_GLOBAL_STATIC_WITH_ARGS(const QString, YOUR_CLIENT_ID, (QLatin1String("0bffa5b4ac8383c" ))) |
| 28 | |
| 29 | class ImgurShareJob : public Purpose::Job |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | public: |
| 33 | explicit ImgurShareJob(QObject *parent) |
| 34 | : Purpose::Job(parent) |
| 35 | , m_pendingJobs(0) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | void start() override |
| 40 | { |
| 41 | m_pendingJobs = 0; |
| 42 | const QJsonArray urls = data().value(key: QLatin1String("urls" )).toArray(); |
| 43 | if (urls.isEmpty()) { |
| 44 | qWarning() << "no urls to share" << urls << data(); |
| 45 | emitResult(); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if (urls.count() > 1) { |
| 50 | KIO::TransferJob *tJob = KIO::storedHttpPost(arr: "" , url: *albumImgurUrl, flags: KIO::HideProgressInfo); |
| 51 | tJob->setMetaData(QMap<QString, QString>{{QStringLiteral("customHTTPHeader" ), QStringLiteral("Authorization: Client-ID " ) + *YOUR_CLIENT_ID}}); |
| 52 | connect(sender: tJob, signal: &KJob::result, context: this, slot: &ImgurShareJob::albumCreated); |
| 53 | } else { |
| 54 | startUploading(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | QJsonObject processResponse(KJob *job) |
| 59 | { |
| 60 | KIO::StoredTransferJob *sjob = qobject_cast<KIO::StoredTransferJob *>(object: job); |
| 61 | QJsonParseError error; |
| 62 | const QJsonObject resultMap = QJsonDocument::fromJson(json: sjob->data(), error: &error).object(); |
| 63 | |
| 64 | if (job->error() || !resultMap.value(key: QLatin1String("success" )).toBool()) { |
| 65 | setError(2); |
| 66 | if (resultMap.contains(key: QLatin1String("data" ))) { |
| 67 | const QJsonObject dataMap = resultMap[QLatin1String("data" )].toObject(); |
| 68 | setErrorText(i18n("Upload failed: %1" , dataMap[QLatin1String("error" )].toString())); |
| 69 | } else { |
| 70 | setErrorText(i18n("Upload failed: %1" , error.errorString())); |
| 71 | } |
| 72 | } else { |
| 73 | return resultMap[QLatin1String("data" )].toObject(); |
| 74 | } |
| 75 | emitResult(); |
| 76 | return {}; |
| 77 | } |
| 78 | |
| 79 | void albumCreated(KJob *job) |
| 80 | { |
| 81 | const QJsonObject dataMap = processResponse(job); |
| 82 | if (!dataMap.isEmpty()) { |
| 83 | m_albumId = dataMap[QLatin1String("id" )].toString(); |
| 84 | m_albumDeleteHash = dataMap[QLatin1String("deletehash" )].toString(); |
| 85 | startUploading(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void startUploading() |
| 90 | { |
| 91 | Q_EMIT infoMessage(job: this, i18n("Uploading files to imgur..." )); |
| 92 | const QJsonArray urls = data().value(key: QLatin1String("urls" )).toArray(); |
| 93 | for (const QJsonValue &val : urls) { |
| 94 | QString u = val.toString(); |
| 95 | KIO::StoredTransferJob *job = KIO::storedGet(url: QUrl(u)); |
| 96 | connect(sender: job, signal: &KJob::finished, context: this, slot: &ImgurShareJob::fileFetched); |
| 97 | m_pendingJobs++; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void fileFetched(KJob *j) |
| 102 | { |
| 103 | if (j->error()) { |
| 104 | setError(j->error()); |
| 105 | setErrorText(j->errorText()); |
| 106 | emitResult(); |
| 107 | |
| 108 | qDebug() << "error:" << j->errorText() << j->errorString(); |
| 109 | |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | MPForm form; |
| 114 | KIO::StoredTransferJob *job = qobject_cast<KIO::StoredTransferJob *>(object: j); |
| 115 | form.addFile(QStringLiteral("image" ), filePath: job->url(), fileData: job->data()); |
| 116 | form.addPair(QStringLiteral("album" ), value: m_albumDeleteHash, type: {}); |
| 117 | form.finish(); |
| 118 | |
| 119 | KIO::StoredTransferJob *tJob = KIO::storedHttpPost(arr: form.formData(), url: *imageImgurUrl, flags: KIO::HideProgressInfo); |
| 120 | tJob->setMetaData(QMap<QString, QString>{ |
| 121 | {QStringLiteral("content-type" ), QString::fromLocal8Bit(ba: form.contentType())}, |
| 122 | {QStringLiteral("customHTTPHeader" ), QStringLiteral("Authorization: Client-ID " ) + *YOUR_CLIENT_ID}, |
| 123 | }); |
| 124 | connect(sender: tJob, signal: &KJob::result, context: this, slot: &ImgurShareJob::imageUploaded); |
| 125 | } |
| 126 | |
| 127 | void imageUploaded(KJob *job) |
| 128 | { |
| 129 | const QJsonObject dataMap = processResponse(job); |
| 130 | if (!dataMap.isEmpty()) { |
| 131 | const QString url = dataMap[QStringLiteral("link" )].toString(); |
| 132 | Q_EMIT infoMessage(job: this, message: url); |
| 133 | const QString deletehash = dataMap[QStringLiteral("deletehash" )].toString(); |
| 134 | Q_EMIT infoMessage(job: this, message: deletehash); |
| 135 | --m_pendingJobs; |
| 136 | |
| 137 | if (m_pendingJobs == 0) { |
| 138 | const QString finalUrl = m_albumId.isEmpty() ? url : QStringLiteral("https://imgur.com/a/" ) + m_albumId; |
| 139 | const QString deleteUrl = QStringLiteral("https://imgur.com/delete/" ) + deletehash; |
| 140 | |
| 141 | QGuiApplication::clipboard()->setText(url); |
| 142 | KNotification::event(eventId: KNotification::Notification, |
| 143 | i18n("Imgur Upload" ), |
| 144 | i18n("The shared image link (<a href=\"%1\">%1</a>) has been copied to the clipboard.<br><br>If you would like to remove " |
| 145 | "the uploaded image, visit <a href=\"%2\">%2</a>" , |
| 146 | finalUrl, |
| 147 | deleteUrl), |
| 148 | flags: KNotification::Persistent); |
| 149 | |
| 150 | emitResult(); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | QString m_albumId; |
| 157 | QString m_albumDeleteHash; |
| 158 | int m_pendingJobs; |
| 159 | }; |
| 160 | |
| 161 | class ImgurPlugin : public Purpose::PluginBase |
| 162 | { |
| 163 | Q_OBJECT |
| 164 | public: |
| 165 | using PluginBase::PluginBase; |
| 166 | Purpose::Job *createJob() const override |
| 167 | { |
| 168 | return new ImgurShareJob(nullptr); |
| 169 | } |
| 170 | }; |
| 171 | |
| 172 | K_PLUGIN_CLASS_WITH_JSON(ImgurPlugin, "imgurplugin.json" ) |
| 173 | |
| 174 | #include "imgurplugin.moc" |
| 175 | |