1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2005-07-07
* Description : a tool to export items to Google web services
*
* SPDX-FileCopyrightText: 2005-2008 by Vardhman Jain <vardhman at gmail dot com>
* SPDX-FileCopyrightText: 2008-2020 by Caulier Gilles <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "gpmpform.h"
// Qt includes
#include <QUrl>
#include <QFile>
#include <QMimeDatabase>
#include <QMimeType>
namespace DigikamGenericGoogleServicesPlugin
{
void GPMPForm::reset()
{
m_buffer.resize(0);
}
void GPMPForm::finish()
{
QByteArray str;
str += "--";
str += m_boundary;
str += "--";
m_buffer.append(str);
}
bool GPMPForm::addPair(const QString& name,
const QString& value,
const QString& contentType)<--- Shadow argument
{
QByteArray str;
QString content_length = QString::number(value.length());
str += "--";
str += m_boundary;
str += "\r\n";
if (!name.isEmpty())
{
str += "Content-Disposition: form-data; name=\"";
str += name.toLatin1();
str += "\"\r\n";
}
if (!contentType.isEmpty())
{
str += "Content-Type: "+ QByteArray(contentType.toLatin1());
str += "\r\n";
str += "Mime-version: 1.0 ";
str += "\r\n";
}
str += "Content-Length: " ;
str += content_length.toLatin1();
str += "\r\n\r\n";
str += value.toUtf8();
str += "\r\n";
m_buffer.append(str);
return true;
}
bool GPMPForm::addFile(const QString& name, const QString& path)
{
QMimeDatabase db;
QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
QString mime = ptr.name();
if (mime.isEmpty())
{
// if we ourselves cannot determine the mime of the local file,
// very unlikely the remote site will be able to identify it
return false;
}
QFile imageFile(path);
if (!imageFile.open(QIODevice::ReadOnly))
{
return false;
}
QByteArray imageData = imageFile.readAll();
imageFile.close();
QByteArray str;
str += "--";
str += m_boundary;
str += "\r\n";
str += "Content-Disposition: form-data; name=\"";
str += name.toLatin1();
str += "\"; ";
str += "filename=\"";
str += QUrl::fromLocalFile(path).fileName().toLatin1();
str += "\"\r\n";
str += "Content-Length: ";
str += QString::number(imageFile.size()).toLatin1();
str += "\r\n";
str += "Content-Type: ";
str += mime.toLatin1();
str += "\r\n\r\n";
m_buffer.append(str);
m_buffer.append(imageData);
m_buffer.append("\r\n");
return true;
}
QString GPMPForm::contentType() const
{
return QLatin1String("multipart/related; boundary=") +
QLatin1String(m_boundary);
}
QString GPMPForm::boundary() const
{
return QLatin1String(m_boundary);
}
QByteArray GPMPForm::formData() const
{
return m_buffer;
}
} // namespace DigikamGenericGoogleServicesPlugin
|