Purpose

mpform.cpp
1/*
2 This file is a part of kipi-plugins project
3 http://www.kipi-plugins.org
4
5 Date : 2005-07-07
6 Description : a tool to export images to imgur.com
7
8 SPDX-FileCopyrightText: 2005-2008 Vardhman Jain <vardhman at gmail dot com>
9 SPDX-FileCopyrightText: 2008-2013 Gilles Caulier <caulier dot gilles at gmail dot com>
10 SPDX-FileCopyrightText: 2010-2012 Marius Orcsik <marius at habarnam dot ro>
11
12 SPDX-License-Identifier: GPL-2.0-or-later
13*/
14
15#include "mpform.h"
16
17// Qt includes
18
19#include <QDebug>
20#include <QFile>
21#include <QMimeDatabase>
22
23// KDE includes
24
25#include <KRandom>
26
27MPForm::MPForm()
28{
29 m_boundary = "----------";
30 m_boundary += KRandom::randomString(42 + 13).toLatin1();
31}
32
33MPForm::~MPForm()
34{
35}
36
37void MPForm::reset()
38{
39 m_buffer.resize(0);
40}
41
42void MPForm::finish()
43{
44 QByteArray str;
45 str += "--";
46 str += m_boundary;
47 str += "--";
48 m_buffer.append(str);
49}
50
51bool MPForm::addPair(const QString &name, const QString &value, const QString &contentType)
52{
53 QByteArray str;
54 QByteArray content_length = QByteArray::number(value.length());
55
56 str += "--";
57 str += m_boundary;
58 str += "\r\n";
59
60 if (!name.isEmpty()) {
61 str += "Content-Disposition: form-data; name=\"";
62 str += name.toLatin1();
63 str += "\"\r\n";
64 }
65
66 if (!contentType.isEmpty()) {
67 str += "Content-Type: " + contentType.toLatin1();
68 str += "\r\n";
69 str += "Mime-version: 1.0 ";
70 str += "\r\n";
71 }
72
73 str += "Content-Length: ";
74 str += content_length;
75 str += "\r\n\r\n";
76 str += value.toUtf8();
77
78 m_buffer.append(str);
79 m_buffer.append("\r\n");
80
81 return true;
82}
83
84bool MPForm::addFile(const QString &name, const QString &path)
85{
88 QString mime = ptr.name();
89
90 if (mime.isEmpty()) {
91 // if we ourselves can't determine the mime of the local file,
92 // very unlikely the remote site will be able to identify it
93 return false;
94 }
95
96 QFile imageFile(path);
97
98 if (!imageFile.open(QIODevice::ReadOnly)) {
99 qWarning() << "Couldn't open" << path;
100 return false;
101 }
102
103 QByteArray imageData = imageFile.readAll();
104
105 QByteArray str;
106 QByteArray file_size = QByteArray::number(imageFile.size());
107 imageFile.close();
108
109 str += "--";
110 str += m_boundary;
111 str += "\r\n";
112 str += "Content-Disposition: form-data; name=\"";
113 str += name.toLatin1();
114 str += "\"; ";
115 str += "filename=\"";
116 str += QFile::encodeName(imageFile.fileName());
117 str += "\"\r\n";
118 str += "Content-Length: ";
119 str += file_size;
120 str += "\r\n";
121 str += "Content-Type: ";
122 str += mime.toLatin1();
123 str += "\r\n\r\n";
124
125 m_buffer.append(str);
126 // int oldSize = m_buffer.size();
127 m_buffer.append(imageData);
128 m_buffer.append("\r\n");
129
130 return true;
131}
132
133bool MPForm::addFile(const QString &name, const QUrl &fileUrl, const QByteArray &fileData)
134{
135 QMimeDatabase db;
136 QMimeType ptr = db.mimeTypeForUrl(fileUrl);
137 QString mime = ptr.name();
138
139 if (mime.isEmpty()) {
140 // if we ourselves can't determine the mime of the local file,
141 // very unlikely the remote site will be able to identify it
142 return false;
143 }
144
145 QByteArray str;
146 QByteArray file_size = QByteArray::number(fileData.size());
147
148 str += "--";
149 str += m_boundary;
150 str += "\r\n";
151 str += "Content-Disposition: form-data; name=\"";
152 str += name.toLatin1();
153 str += "\"; ";
154 str += "filename=\"";
155 str += QFile::encodeName(fileUrl.fileName());
156 str += "\"\r\n";
157 str += "Content-Length: ";
158 str += file_size;
159 str += "\r\n";
160 str += "Content-Type: ";
161 str += mime.toLatin1();
162 str += "\r\n\r\n";
163
164 m_buffer.append(str);
165 // int oldSize = m_buffer.size();
166 m_buffer.append(fileData);
167 m_buffer.append("\r\n");
168
169 return true;
170}
171
172QByteArray MPForm::contentType() const
173{
174 return "Content-Type: multipart/form-data; boundary=" + m_boundary;
175}
176
177QByteArray MPForm::boundary() const
178{
179 return m_boundary;
180}
181
182QByteArray MPForm::formData() const
183{
184 return m_buffer;
185}
QString path(const QString &relativePath)
KCOREADDONS_EXPORT QString randomString(int length)
QString name(StandardAction id)
QByteArray & append(QByteArrayView data)
QByteArray number(double n, char format, int precision)
void resize(qsizetype newSize, char c)
qsizetype size() const const
QByteArray encodeName(const QString &fileName)
QMimeType mimeTypeForUrl(const QUrl &url) const const
bool isEmpty() const const
qsizetype length() const const
QByteArray toLatin1() const const
QByteArray toUtf8() const const
QString fileName(ComponentFormattingOptions options) const const
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.