File indexing completed on 2025-01-05 03:53:29

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2005-07-07
0007  * Description : a tool to export items to Google web services
0008  *
0009  * SPDX-FileCopyrightText: 2005-2008 by Vardhman Jain <vardhman at gmail dot com>
0010  * SPDX-FileCopyrightText: 2008-2020 by Caulier Gilles <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "gpmpform.h"
0017 
0018 // Qt includes
0019 
0020 #include <QUrl>
0021 #include <QFile>
0022 #include <QMimeDatabase>
0023 #include <QMimeType>
0024 
0025 //local includes
0026 
0027 #include "wstoolutils.h"
0028 
0029 using namespace Digikam;
0030 
0031 namespace DigikamGenericGoogleServicesPlugin
0032 {
0033 
0034 GPMPForm::GPMPForm()
0035     : m_boundary(QByteArray("----------") + WSToolUtils::randomString(42 + 13).toLatin1())
0036 {
0037 }
0038 
0039 GPMPForm::~GPMPForm()
0040 {
0041 }
0042 
0043 void GPMPForm::reset()
0044 {
0045     m_buffer.resize(0);
0046 }
0047 
0048 void GPMPForm::finish()
0049 {
0050     QByteArray str;
0051     str += "--";
0052     str += m_boundary;
0053     str += "--";
0054 
0055     m_buffer.append(str);
0056 }
0057 
0058 bool GPMPForm::addPair(const QString& name,
0059                        const QString& value,
0060                        const QString& contentType)
0061 {
0062     QByteArray str;
0063     QString content_length = QString::number(value.length());
0064     str += "--";
0065     str += m_boundary;
0066     str += "\r\n";
0067 
0068     if (!name.isEmpty())
0069     {
0070         str += "Content-Disposition: form-data; name=\"";
0071         str += name.toLatin1();
0072         str += "\"\r\n";
0073     }
0074 
0075     if (!contentType.isEmpty())
0076     {
0077         str += "Content-Type: "+ QByteArray(contentType.toLatin1());
0078         str += "\r\n";
0079         str += "Mime-version: 1.0 ";
0080         str += "\r\n";
0081     }
0082 
0083     str += "Content-Length: " ;
0084     str += content_length.toLatin1();
0085     str += "\r\n\r\n";
0086     str += value.toUtf8();
0087     str += "\r\n";
0088 
0089     m_buffer.append(str);
0090 
0091     return true;
0092 }
0093 
0094 bool GPMPForm::addFile(const QString& name, const QString& path)
0095 {
0096     QMimeDatabase db;
0097     QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
0098     QString mime  = ptr.name();
0099 
0100     if (mime.isEmpty())
0101     {
0102         // if we ourselves can't determine the mime of the local file,
0103         // very unlikely the remote site will be able to identify it
0104         return false;
0105     }
0106 
0107     QFile imageFile(path);
0108 
0109     if (!imageFile.open(QIODevice::ReadOnly))
0110         return false;
0111 
0112     QByteArray imageData = imageFile.readAll();
0113     imageFile.close();
0114 
0115     QByteArray str;
0116     str += "--";
0117     str += m_boundary;
0118     str += "\r\n";
0119     str += "Content-Disposition: form-data; name=\"";
0120     str += name.toLatin1();
0121     str += "\"; ";
0122     str += "filename=\"";
0123     str += QUrl::fromLocalFile(path).fileName().toLatin1();
0124     str += "\"\r\n";
0125     str += "Content-Length: ";
0126     str += QString::number(imageFile.size()).toLatin1();
0127     str += "\r\n";
0128     str += "Content-Type: ";
0129     str += mime.toLatin1();
0130     str += "\r\n\r\n";
0131 
0132     m_buffer.append(str);
0133     m_buffer.append(imageData);
0134     m_buffer.append("\r\n");
0135 
0136     return true;
0137 }
0138 
0139 QString GPMPForm::contentType() const
0140 {
0141     return QLatin1String("multipart/related; boundary=") +
0142            QLatin1String(m_boundary);
0143 }
0144 
0145 QString GPMPForm::boundary() const
0146 {
0147     return QLatin1String(m_boundary);
0148 }
0149 
0150 QByteArray GPMPForm::formData() const
0151 {
0152     return m_buffer;
0153 }
0154 
0155 } // namespace DigikamGenericGoogleServicesPlugin