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

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 Flickr 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 "flickrmpform.h"
0017 
0018 // Qt includes
0019 
0020 #include <QFile>
0021 #include <QApplication>
0022 #include <QUrl>
0023 #include <QMimeDatabase>
0024 #include <QMimeType>
0025 #include <QtGlobal>
0026 #include <QTime>
0027 
0028 // Local includes
0029 
0030 #include "digikam_debug.h"
0031 #include "wstoolutils.h"
0032 
0033 using namespace Digikam;
0034 
0035 namespace DigikamGenericFlickrPlugin
0036 {
0037 
0038 FlickrMPForm::FlickrMPForm()
0039     : m_boundary(QByteArray("----------") + WSToolUtils::randomString(42 + 13).toLatin1())
0040 {
0041 }
0042 
0043 FlickrMPForm::~FlickrMPForm()
0044 {
0045 }
0046 
0047 void FlickrMPForm::reset()
0048 {
0049     m_buffer.resize(0);
0050 }
0051 
0052 void FlickrMPForm::finish()
0053 {
0054     QByteArray str;
0055     str += "--";
0056     str += m_boundary;
0057     str += "--";
0058     m_buffer.append(str);
0059 }
0060 
0061 bool FlickrMPForm::addPair(const QString& name, const QString& value, const QString& contentType)
0062 {
0063     QByteArray str;
0064     QString content_length = QString::fromLatin1("%1").arg(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 FlickrMPForm::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::fromLatin1("%1").arg(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::fromLocalFile(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     return true;
0142 }
0143 
0144 QString FlickrMPForm::contentType() const
0145 {
0146     return QLatin1String("multipart/form-data; boundary=") + QLatin1String(m_boundary);
0147 }
0148 
0149 QString FlickrMPForm::boundary() const
0150 {
0151     return QLatin1String(m_boundary);
0152 }
0153 
0154 QByteArray FlickrMPForm::formData() const
0155 {
0156     return m_buffer;
0157 }
0158 
0159 } // namespace DigikamGenericFlickrPlugin