File indexing completed on 2024-05-12 04:57:44

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "imageshack.h"
0010 
0011 #include <QDebug>
0012 #include <QDomDocument>
0013 #include <QDomElement>
0014 
0015 #include <KIO/StoredTransferJob>
0016 #include <KLocalizedString>
0017 #include <KPluginFactory>
0018 
0019 #include "mediamanager.h"
0020 
0021 const static QString apiKey = QLatin1String("ZMWLXQBOfb570310607355f90c601148a3203f0f");
0022 
0023 K_PLUGIN_CLASS_WITH_JSON(ImageShack, "choqok_imageshack.json")
0024 
0025 ImageShack::ImageShack(QObject *parent, const QList<QVariant> &)
0026     : Choqok::Uploader(QLatin1String("choqok_imageshack"), parent)
0027 {
0028 }
0029 
0030 ImageShack::~ImageShack()
0031 {
0032 }
0033 
0034 void ImageShack::upload(const QUrl &localUrl, const QByteArray &medium, const QByteArray &mediumType)
0035 {
0036     if (!mediumType.startsWith(QByteArray("image/"))) {
0037         Q_EMIT uploadingFailed(localUrl, i18n("Just supporting image uploading"));
0038         return;
0039     }
0040     QUrl url(QLatin1String("https://www.imageshack.us/upload_api.php"));
0041     QMap<QString, QByteArray> formdata;
0042     formdata[QLatin1String("key")] = apiKey.toLatin1();
0043     formdata[QLatin1String("rembar")] = "1";
0044 
0045     QMap<QString, QByteArray> mediafile;
0046     mediafile[QLatin1String("name")] = "fileupload";
0047     mediafile[QLatin1String("filename")] = localUrl.fileName().toUtf8();
0048     mediafile[QLatin1String("mediumType")] = mediumType;
0049     mediafile[QLatin1String("medium")] = medium;
0050     QList< QMap<QString, QByteArray> > listMediafiles;
0051     listMediafiles.append(mediafile);
0052 
0053     QByteArray data = Choqok::MediaManager::createMultipartFormData(formdata, listMediafiles);
0054 
0055     KIO::StoredTransferJob *job = KIO::storedHttpPost(data, url, KIO::HideProgressInfo) ;
0056     if (!job) {
0057         qCritical() << "Cannot create a http POST request!";
0058         return;
0059     }
0060     job->addMetaData(QLatin1String("content-type"), QLatin1String("Content-Type: multipart/form-data; boundary=AaB03x"));
0061     mUrlMap[job] = localUrl;
0062     connect(job, &KIO::StoredTransferJob::result, this, &ImageShack::slotUpload);
0063     job->start();
0064 }
0065 
0066 void ImageShack::slotUpload(KJob *job)
0067 {
0068     QUrl localUrl = mUrlMap.take(job);
0069     if (job->error()) {
0070         qCritical() << "Job Error:" << job->errorString();
0071         Q_EMIT uploadingFailed(localUrl, job->errorString());
0072         return;
0073     } else {
0074         KIO::StoredTransferJob *stj = qobject_cast<KIO::StoredTransferJob *>(job);
0075         QDomDocument doc;
0076         doc.setContent(stj->data());
0077         if (doc.firstChild().isNull()) {
0078             qWarning() << "Malformed response:" << stj->data();
0079             return;
0080         }
0081         QDomElement root = doc.documentElement();
0082         if (root.tagName() == QLatin1String("imginfo")) {
0083             QDomNode node = root.firstChild();
0084             while (!node.isNull()) {
0085                 QDomElement elm = node.toElement();
0086                 if (elm.tagName() == QLatin1String("links")) {
0087                     QDomNode node2 = node.firstChild();
0088                     while (!node2.isNull()) {
0089                         QDomElement elm2 = node2.toElement();
0090                         if (elm2.tagName() == QLatin1String("yfrog_link")) {
0091                             Q_EMIT mediumUploaded(localUrl, elm2.text());
0092                             return;
0093                         }
0094                         node2 = node2.nextSibling();
0095                     }
0096                 }
0097                 node = node.nextSibling();
0098             }
0099         } else {
0100             if (root.tagName() == QLatin1String("links")) {
0101                 QDomNode node = root.firstChild();
0102                 if (!node.isNull()) {
0103                     if (node.toElement().tagName() == QLatin1String("error")) {
0104                         Q_EMIT uploadingFailed(localUrl, node.toElement().text());
0105                         return;
0106                     }
0107                 }
0108             }
0109         }
0110         Q_EMIT uploadingFailed(localUrl, i18n("Malformed response"));
0111         qWarning() << "Response not detected:" << stj->data();
0112     }
0113 }
0114 
0115 #include "imageshack.moc"
0116 #include "moc_imageshack.cpp"