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

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