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

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 images to Smugmug web service
0008  *
0009  * SPDX-FileCopyrightText: 2005-2008 by Vardhman Jain <vardhman at gmail dot com>
0010  * SPDX-FileCopyrightText: 2008-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 "smugmpform.h"
0017 
0018 // Qt includes
0019 
0020 #include <QFile>
0021 #include <QMimeDatabase>
0022 #include <QMimeType>
0023 #include <QUrl>
0024 #include <QApplication>
0025 
0026 // Local includes
0027 
0028 #include "digikam_debug.h"
0029 #include "wstoolutils.h"
0030 
0031 using namespace Digikam;
0032 
0033 namespace DigikamGenericSmugPlugin
0034 {
0035 
0036 SmugMPForm::SmugMPForm()
0037     : m_boundary(QByteArray("----------") + WSToolUtils::randomString(42 + 13).toLatin1())
0038 {
0039 }
0040 
0041 SmugMPForm::~SmugMPForm()
0042 {
0043 }
0044 
0045 void SmugMPForm::reset()
0046 {
0047     m_buffer.resize(0);
0048 }
0049 
0050 void SmugMPForm::finish()
0051 {
0052     QByteArray str;
0053     str += "--";
0054     str += m_boundary;
0055     str += "--";
0056     m_buffer.append(str);
0057 }
0058 
0059 bool SmugMPForm::addPair(const QString& name,
0060                          const QString& value,
0061                          const QString& contentType)
0062 {
0063     QByteArray str;
0064     QString content_length = QString::number(value.length());
0065 
0066     str += "--";
0067     str += m_boundary;
0068     str += "\r\n";
0069 
0070     if (!name.isEmpty())
0071     {
0072         str += "Content-Disposition: form-data; name=\"";
0073         str += name.toLatin1();
0074         str += "\"\r\n";
0075     }
0076 
0077     if (!contentType.isEmpty())
0078     {
0079         str += "Content-Type: " + QByteArray(contentType.toLatin1());
0080         str += "\r\n";
0081         str += "Mime-version: 1.0 ";
0082         str += "\r\n";
0083     }
0084 
0085     str += "Content-Length: ";
0086     str += content_length.toLatin1();
0087     str += "\r\n\r\n";
0088     str += value.toUtf8();
0089 
0090     m_buffer.append(str);
0091     m_buffer.append("\r\n");
0092     return true;
0093 }
0094 
0095 bool SmugMPForm::addFile(const QString& name, const QString& path)
0096 {
0097     QMimeDatabase db;
0098     QMimeType mimeType = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
0099     QString mime       = mimeType.name();
0100 
0101     if (mime.isEmpty())
0102     {
0103         // if we ourselves can't determine the mime of the local file,
0104         // very unlikely the remote site will be able to identify it
0105 
0106         return false;
0107     }
0108 
0109     QFile imageFile(path);
0110 
0111     if (!imageFile.open(QIODevice::ReadOnly))
0112     {
0113         return false;
0114     }
0115 
0116     QByteArray imageData = imageFile.readAll();
0117 
0118     QByteArray str;
0119     QString file_size = QString::number(imageFile.size());
0120     imageFile.close();
0121 
0122     str += "--";
0123     str += m_boundary;
0124     str += "\r\n";
0125     str += "Content-Disposition: form-data; name=\"";
0126     str += name.toLatin1();
0127     str += "\"; ";
0128     str += "filename=\"";
0129     str += QFile::encodeName(QUrl(path).fileName());
0130     str += "\"\r\n";
0131     str += "Content-Length: ";
0132     str += file_size.toLatin1();
0133     str += "\r\n";
0134     str += "Content-Type: ";
0135     str +=  mime.toLatin1();
0136     str += "\r\n\r\n";
0137 
0138     m_buffer.append(str);
0139     m_buffer.append(imageData);
0140     m_buffer.append("\r\n");
0141 
0142     return true;
0143 }
0144 
0145 QString SmugMPForm::contentType() const
0146 {
0147     return QLatin1String("multipart/form-data; boundary=") + QLatin1String(m_boundary);
0148 }
0149 
0150 QString SmugMPForm::boundary() const
0151 {
0152     return QLatin1String(m_boundary);
0153 }
0154 
0155 QByteArray SmugMPForm::formData() const
0156 {
0157     return m_buffer;
0158 }
0159 
0160 } // namespace DigikamGenericSmugPlugin