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(length: 42 + 13).toLatin1();
31}
32
33MPForm::~MPForm()
34{
35}
36
37void MPForm::reset()
38{
39 m_buffer.resize(size: 0);
40}
41
42void MPForm::finish()
43{
44 QByteArray str;
45 str += "--";
46 str += m_boundary;
47 str += "--";
48 m_buffer.append(a: 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(a: str);
79 m_buffer.append(s: "\r\n");
80
81 return true;
82}
83
84bool MPForm::addFile(const QString &name, const QString &path)
85{
86 QMimeDatabase db;
87 QMimeType ptr = db.mimeTypeForUrl(url: QUrl::fromLocalFile(localfile: path));
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(flags: 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(fileName: 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(a: str);
126 // int oldSize = m_buffer.size();
127 m_buffer.append(a: imageData);
128 m_buffer.append(s: "\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(url: 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(fileName: 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(a: str);
165 // int oldSize = m_buffer.size();
166 m_buffer.append(a: fileData);
167 m_buffer.append(s: "\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}
186

source code of purpose/src/plugins/imgur/mpform.cpp