File indexing completed on 2025-01-05 03:53:31
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2012-02-02 0007 * Description : a tool to export items to ImageShack web service 0008 * 0009 * SPDX-FileCopyrightText: 2012 by Dodon Victor <dodonvictor at gmail dot com> 0010 * SPDX-FileCopyrightText: 2013-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 "imageshackmpform.h" 0017 0018 // Qt includes 0019 0020 #include <QFile> 0021 #include <QUrl> 0022 #include <QMimeDatabase> 0023 #include <QMimeType> 0024 0025 // Local includes 0026 0027 #include "digikam_debug.h" 0028 #include "wstoolutils.h" 0029 0030 using namespace Digikam; 0031 0032 namespace DigikamGenericImageShackPlugin 0033 { 0034 0035 ImageShackMPForm::ImageShackMPForm() 0036 : m_boundary(WSToolUtils::randomString(42 + 13).toLatin1()) 0037 { 0038 reset(); 0039 } 0040 0041 ImageShackMPForm::~ImageShackMPForm() 0042 { 0043 } 0044 0045 void ImageShackMPForm::reset() 0046 { 0047 m_buffer.resize(0); 0048 QByteArray str(contentType().toLatin1()); 0049 str += "\r\nMIME-version: 1.0\r\n\r\n"; 0050 m_buffer.append(str); 0051 } 0052 0053 void ImageShackMPForm::finish() 0054 { 0055 QByteArray str; 0056 str += "--"; 0057 str += m_boundary; 0058 str += "--"; 0059 m_buffer.append(str); 0060 } 0061 0062 void ImageShackMPForm::addPair(const QString& name, const QString& value) 0063 { 0064 QByteArray str; 0065 QString content_length = QString::number(value.length()); 0066 0067 str += "--"; 0068 str += m_boundary; 0069 str += "\r\n"; 0070 0071 if (!name.isEmpty()) 0072 { 0073 str += "Content-Disposition: form-data; name=\""; 0074 str += name.toLatin1(); 0075 str += "\"\r\n"; 0076 } 0077 str += "\r\n"; 0078 str += value.toUtf8(); 0079 str += "\r\n"; 0080 0081 m_buffer.append(str); 0082 } 0083 0084 bool ImageShackMPForm::addFile(const QString& name, const QString& path) 0085 { 0086 QMimeDatabase db; 0087 QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path)); 0088 QString mime = ptr.name(); 0089 0090 if (mime.isEmpty()) 0091 return false; 0092 0093 QFile imageFile(path); 0094 0095 if (!imageFile.open(QIODevice::ReadOnly)) 0096 return false; 0097 0098 QByteArray imageData = imageFile.readAll(); 0099 0100 QString file_size = QString::number(imageFile.size()); 0101 imageFile.close(); 0102 0103 QByteArray str; 0104 str += "--"; 0105 str += m_boundary; 0106 str += "\r\n"; 0107 str += "Content-Disposition: form-data; name=\"fileupload\"; filename=\""; 0108 str += QFile::encodeName(name); 0109 str += "\"\r\n"; 0110 str += "Content-Length: "; 0111 str += file_size.toLatin1(); 0112 str += "\r\n"; 0113 str += "Content-Type: "; 0114 str += mime.toLatin1(); 0115 str += "\r\n\r\n"; 0116 0117 m_buffer.append(str); 0118 m_buffer.append(imageData); 0119 m_buffer.append("\r\n"); 0120 0121 return true; 0122 } 0123 0124 QString ImageShackMPForm::contentType() const 0125 { 0126 return QLatin1String("multipart/form-data; boundary=") + QLatin1String(m_boundary); 0127 } 0128 0129 QString ImageShackMPForm::boundary() const 0130 { 0131 return QLatin1String(m_boundary); 0132 } 0133 0134 QByteArray ImageShackMPForm::formData() const 0135 { 0136 return m_buffer; 0137 } 0138 0139 } // namespace DigikamGenericImageShackPlugin