File indexing completed on 2022-09-27 13:43:09

0001 /* ============================================================
0002  *
0003  * This file is a part of KDE project
0004  *
0005  *
0006  * Date        : 2005-07-07
0007  * Description : a kipi plugin to export images to Flickr web service
0008  *
0009  * Copyright (C) 2005-2008 by Vardhman Jain <vardhman at gmail dot com>
0010  * Copyright (C) 2008-2018 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * This program is free software; you can redistribute it
0013  * and/or modify it under the terms of the GNU General
0014  * Public License as published by the Free Software Foundation;
0015  * either version 2, or (at your option) any later version.
0016  *
0017  * This program is distributed in the hope that it will be useful,
0018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0020  * GNU General Public License for more details.
0021  *
0022  * ============================================================ */
0023 
0024 #include "mpform.h"
0025 
0026 // Qt includes
0027 
0028 #include <QFile>
0029 #include <QUrl>
0030 #include <QApplication>
0031 #include <QMimeDatabase>
0032 #include <QMimeType>
0033 #include <QtGlobal>
0034 #include <QTime>
0035 
0036 // Local includes
0037 
0038 #include "kipiplugins_debug.h"
0039 #include "kputil.h"
0040 
0041 namespace KIPIFlickrPlugin
0042 {
0043 
0044 MPForm::MPForm()
0045 {
0046     m_boundary  = "----------";
0047     m_boundary += KIPIPlugins::KPRandomGenerator::randomString(42 + 13).toLatin1();
0048 }
0049 
0050 MPForm::~MPForm()
0051 {
0052 }
0053 
0054 void MPForm::reset()
0055 {
0056     m_buffer.resize(0);
0057 }
0058 
0059 void MPForm::finish()
0060 {
0061     QByteArray str;
0062     str += "--";
0063     str += m_boundary;
0064     str += "--";
0065     m_buffer.append(str);
0066 }
0067 
0068 bool MPForm::addPair(const QString& name, const QString& value, const QString& contentType)
0069 {
0070     QByteArray str;
0071     QString content_length = QString::fromLatin1("%1").arg(value.length());
0072 
0073     str += "--";
0074     str += m_boundary;
0075     str += "\r\n";
0076 
0077     if (!name.isEmpty())
0078     {
0079         str += "Content-Disposition: form-data; name=\"";
0080         str += name.toLatin1();
0081         str += "\"\r\n";
0082     }
0083 
0084     if (!contentType.isEmpty())
0085     {
0086         str += "Content-Type: " + QByteArray(contentType.toLatin1());
0087         str += "\r\n";
0088         str += "Mime-version: 1.0 ";
0089         str += "\r\n";
0090     }
0091 
0092     str += "Content-Length: ";
0093     str += content_length.toLatin1();
0094     str += "\r\n\r\n";
0095     str += value.toUtf8();
0096 
0097     m_buffer.append(str);
0098     m_buffer.append("\r\n");
0099     return true;
0100 }
0101 
0102 bool MPForm::addFile(const QString& name, const QString& path)
0103 {
0104     QMimeDatabase db;
0105     QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
0106     QString mime  = ptr.name();
0107 
0108     if (mime.isEmpty())
0109     {
0110         // if we ourselves can't determine the mime of the local file,
0111         // very unlikely the remote site will be able to identify it
0112         return false;
0113     }
0114 
0115     QFile imageFile(path);
0116 
0117     if (!imageFile.open(QIODevice::ReadOnly))
0118     {
0119         return false;
0120     }
0121 
0122     QByteArray imageData = imageFile.readAll();
0123 
0124     QByteArray str;
0125     QString file_size = QString::fromLatin1("%1").arg(imageFile.size());
0126     imageFile.close();
0127 
0128     str += "--";
0129     str += m_boundary;
0130     str += "\r\n";
0131     str += "Content-Disposition: form-data; name=\"";
0132     str += name.toLatin1();
0133     str += "\"; ";
0134     str += "filename=\"";
0135     str += QFile::encodeName(QUrl::fromLocalFile(path).fileName());
0136     str += "\"\r\n";
0137     str += "Content-Length: ";
0138     str += file_size.toLatin1();
0139     str += "\r\n";
0140     str += "Content-Type: ";
0141     str +=  mime.toLatin1();
0142     str += "\r\n\r\n";
0143 
0144     m_buffer.append(str);
0145     m_buffer.append(imageData);
0146     m_buffer.append("\r\n");
0147 
0148     return true;
0149 }
0150 
0151 QString MPForm::contentType() const
0152 {
0153     return QString(QString::fromLatin1("multipart/form-data; boundary=") + QLatin1String(m_boundary));
0154 }
0155 
0156 QString MPForm::boundary() const
0157 {
0158     return QLatin1String(m_boundary);
0159 }
0160 
0161 QByteArray MPForm::formData() const
0162 {
0163     return m_buffer;
0164 }
0165 
0166 } // namespace KIPIFlickrPlugin