This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesource.cpp
107 lines (92 loc) · 2.81 KB
/
filesource.cpp
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
#include "filesource.h"
#include "defines.h"
#include <QDebug>
#include <QUrl>
FileSource::FileSource(QSharedPointer<Media> media,
QVector<QPair<QString,QString> > fields)
{
QString filename = media.data()->filepath();
QString mediaClass = media.data()->getClass();
QString mediaType = media.data()->getType();
QString host;
if ((mediaClass == "image") || (mediaClass == "application"))
host = "load"+QString::number((qrand()%9)+1)+"." + UPLOAD_HOSTNAME;
else
host = VIDEO_UPLOAD_HOSTNAME;
QString boundary("UPLOADERBOUNDARY");
QString nl = "\r\n";
header.append("--"); header.append(boundary); header.append(nl);
header.append("Content-Disposition: ");
header.append(QString("form-data; name=\"%1\"; filename=\"%2\"")
.arg(QString("fileupload"), QString(filename.toUtf8())));
header.append(nl);
header.append(QString("Content-Type: %1/%2").arg(mediaClass, mediaType));
header.append(nl);
header.append(nl);
footer.append(nl);
QPair<QString, QString> field;
foreach(field, fields)
{
footer.append("--"); footer.append(boundary); footer.append(nl);
footer.append("Content-Disposition: ");
footer.append(QString("form-data; name=\"%1\"").arg(field.first));
footer.append(nl);
footer.append(nl);
footer.append(field.second); footer.append(nl);
}
footer.append("--");
footer.append(boundary);
footer.append(nl);
data = QSharedPointer<QFile>(new QFile(filename));
data->open(QIODevice::ReadOnly);
curPos = 0;
}
bool FileSource::atEnd () const
{
return curPos == size();
}
qint64 FileSource::size () const
{
return (header.size() + data->size() + footer.size());
}
qint64 FileSource::readData(char* to, qint64 max)
{
QByteArray buf;
while ((curPos < header.size()) && buf.size() < max)
{
buf.append(header.at(curPos));
curPos++;
}
while ((curPos < (header.size()+data->size())) && buf.size() < max)
{
QByteArray newdata = data.data()->read(max-buf.size());
buf.append(newdata);
curPos += newdata.size();
}
int fullsize = header.size() + data->size() + footer.size();
while ((curPos >= ( header.size()+data->size()) && (curPos < fullsize)) && buf.size() < max)
{
buf.append(footer.at(curPos-header.size()-data->size()));
curPos++;
}
memcpy(to, buf.data(), buf.size());
if (buf.size())
return buf.size();
else return -1;
}
qint64 FileSource::writeData ( const char *, qint64)
{
return -1;
}
qint64 FileSource::bytesAvailable() const
{
return size() + QIODevice::bytesAvailable() - curPos;
}
bool FileSource::isSequential() const
{
return false;
}
qint64 FileSource::headerSize()
{
return header.size() + footer.size();
}