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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-12-28
0007  * Description : a tool to export items to Facebook 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  * SPDX-FileCopyrightText: 2008-2009 by Luka Renko <lure at kubuntu dot org>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "fbmpform.h"
0018 
0019 // Qt includes
0020 
0021 #include <QFile>
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 DigikamGenericFaceBookPlugin
0034 {
0035 
0036 FbMPForm::FbMPForm()
0037     : m_boundary(WSToolUtils::randomString(42 + 13).toLatin1())
0038 {
0039     reset();
0040 }
0041 
0042 FbMPForm::~FbMPForm()
0043 {
0044 }
0045 
0046 void FbMPForm::reset()
0047 {
0048     m_buffer.resize(0);
0049     QByteArray str(contentType().toLatin1());
0050     str += "\r\n";
0051     str += "MIME-version: 1.0";
0052     str += "\r\n\r\n";
0053     m_buffer.append(str);
0054 }
0055 
0056 void FbMPForm::finish()
0057 {
0058     QByteArray str;
0059     str += "--";
0060     str += m_boundary;
0061     str += "--";
0062     m_buffer.append(str);
0063 }
0064 
0065 void FbMPForm::addPair(const QString& name, const QString& value)
0066 {
0067     QByteArray str;
0068     QString content_length = QString::number(value.length());
0069 
0070     str += "--";
0071     str += m_boundary;
0072     str += "\r\n";
0073 
0074     if (!name.isEmpty())
0075     {
0076         str += "Content-Disposition: form-data; name=\"";
0077         str += name.toLatin1();
0078         str += "\"\r\n";
0079     }
0080 
0081     str += "\r\n";
0082     str += value.toUtf8();
0083     str += "\r\n";
0084 
0085     m_buffer.append(str);
0086 }
0087 
0088 bool FbMPForm::addFile(const QString& name, const QString& path)
0089 {
0090     QMimeDatabase db;
0091     QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
0092     QString mime  = ptr.name();
0093 
0094     if (mime.isEmpty())
0095         return false;
0096 
0097     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "mime = " << mime.toLatin1();
0098 
0099     QFile imageFile(path);
0100 
0101     if (!imageFile.open(QIODevice::ReadOnly))
0102         return false;
0103 
0104     QByteArray imageData = imageFile.readAll();
0105 
0106     //QString file_size = QString::number(imageFile.size());
0107     imageFile.close();
0108 
0109     QByteArray str;
0110     str += "--";
0111     str += m_boundary;
0112     str += "\r\n";
0113     str += "Content-Disposition: form-data; filename=\"";
0114     str += QFile::encodeName(name);
0115     str += "\"\r\n";
0116     //str += "Content-Length: ";
0117     //str += file_size.toAscii();
0118     //str += "\r\n";
0119     str += "Content-Type: ";
0120     str += mime.toLatin1();
0121     str += "\r\n\r\n";
0122 
0123     qCDebug(DIGIKAM_WEBSERVICES_LOG) << str;
0124 
0125     m_buffer.append(str);
0126     m_buffer.append(imageData);
0127     m_buffer.append("\r\n");
0128 
0129     return true;
0130 }
0131 
0132 QString FbMPForm::contentType() const
0133 {
0134     return QLatin1String("multipart/form-data; boundary=") + QLatin1String(m_boundary);
0135 }
0136 
0137 QString FbMPForm::boundary() const
0138 {
0139     return QLatin1String(m_boundary);
0140 }
0141 
0142 QByteArray FbMPForm::formData() const
0143 {
0144     return m_buffer;
0145 }
0146 
0147 } // namespace DigikamGenericFaceBookPlugin