File indexing completed on 2025-01-19 03:52:59

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-11-18
0007  * Description : a tool to export items to Google web services
0008  *
0009  * SPDX-FileCopyrightText: 2013      by Pankaj Kumar <me at panks dot me>
0010  * SPDX-FileCopyrightText: 2013-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "gdmpform.h"
0017 
0018 // Qt includes
0019 
0020 #include <QJsonDocument>
0021 #include <QJsonParseError>
0022 #include <QJsonObject>
0023 #include <QJsonValue>
0024 #include <QJsonArray>
0025 #include <QUrl>
0026 #include <QFile>
0027 #include <QMimeDatabase>
0028 #include <QMimeType>
0029 
0030 // local includes
0031 
0032 #include "digikam_debug.h"
0033 #include "wstoolutils.h"
0034 
0035 using namespace Digikam;
0036 
0037 namespace DigikamGenericGoogleServicesPlugin
0038 {
0039 
0040 GDMPForm::GDMPForm()
0041     : m_boundary(WSToolUtils::randomString(42 + 13).toLatin1())
0042 {
0043     reset();
0044 }
0045 
0046 GDMPForm::~GDMPForm()
0047 {
0048 }
0049 
0050 void GDMPForm::reset()
0051 {
0052     m_buffer.resize(0);
0053 }
0054 
0055 void GDMPForm::finish()
0056 {
0057     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "in finish";
0058     QByteArray str;
0059     str += "--";
0060     str += m_boundary;
0061     str += "--";
0062     m_buffer.append(str);
0063     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "finish:" << m_buffer;
0064 }
0065 
0066 void GDMPForm::addPair(const QString& name,
0067                        const QString& description,
0068                        const QString& path,
0069                        const QString& id)
0070 {
0071     QMimeDatabase db;
0072     QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
0073     QString mime  = ptr.name();
0074     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "in add pair:"
0075                                  << name << " "
0076                                  << description
0077                                  << " " << path
0078                                  << " " << id
0079                                  << " " << mime;
0080 
0081     // Generate JSON
0082     QJsonObject photoInfo;
0083     photoInfo.insert(QLatin1String("title"),       QJsonValue(name));
0084     photoInfo.insert(QLatin1String("description"), QJsonValue(description));
0085     photoInfo.insert(QLatin1String("mimeType"),    QJsonValue(mime));
0086 
0087     QVariantMap parentId;
0088     parentId.insert(QLatin1String("id"), id);
0089     QVariantList parents;
0090     parents << parentId;
0091     photoInfo.insert(QLatin1String("parents"),     QJsonValue(QJsonArray::fromVariantList(parents)));
0092 
0093     QJsonDocument doc(photoInfo);
0094     QByteArray json = doc.toJson();
0095 
0096     // Append to the multipart
0097     QByteArray str;
0098     str += "--";
0099     str += m_boundary;
0100     str += "\r\n";
0101     str += "Content-Type:application/json; charset=UTF-8\r\n\r\n";
0102     str += json;
0103     str += "\r\n";
0104     m_buffer.append(str);
0105 }
0106 
0107 bool GDMPForm::addFile(const QString& path)
0108 {
0109     QByteArray str;
0110     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "in addfile" << path;
0111 
0112     QMimeDatabase db;
0113     QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
0114     QString mime  = ptr.name();
0115     str += "--";
0116     str += m_boundary;
0117     str += "\r\n";
0118     str += "Content-Type: ";
0119     str += mime.toLatin1();
0120     str += "\r\n\r\n";
0121 
0122     QFile imageFile(path);
0123 
0124     if (!imageFile.open(QIODevice::ReadOnly))
0125     {
0126         return false;
0127     }
0128 
0129     QByteArray imageData = imageFile.readAll();
0130     m_file_size          = QString::number(imageFile.size());
0131 
0132     imageFile.close();
0133 
0134     m_buffer.append(str);
0135     m_buffer.append(imageData);
0136     m_buffer.append("\r\n");
0137 
0138     return true;
0139 }
0140 
0141 QByteArray GDMPForm::formData() const
0142 {
0143     return m_buffer;
0144 }
0145 
0146 QString GDMPForm::boundary() const
0147 {
0148     return QLatin1String(m_boundary);
0149 }
0150 
0151 QString GDMPForm::contentType() const
0152 {
0153     return QLatin1String("multipart/related;boundary=") +
0154            QLatin1String(m_boundary);
0155 }
0156 
0157 QString GDMPForm::getFileSize() const
0158 {
0159     return m_file_size;
0160 }
0161 
0162 } // namespace DigikamGenericGoogleServicesPlugin