include filesource include defines include QDebug FileSource FileSourc

 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
#include "filesource.h"
#include "defines.h"
#include <QDebug>
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 = new QFile(filename);
data->open(QIODevice::ReadOnly);
curPos = 0;
}
qint64 FileSource::size ()
{
return (header.size() + data->size() + footer.size());
}
qint64 FileSource::readData(char* to, qint64 max)
{
QByteArray buf;
qDebug () << "!!! request to read "<< max<< " bytes";
while ((curPos < header.size()) && buf.size() < max)
{
buf.append(header);
curPos += header.size(); }
while ((curPos < (header.size()+data->size())) && buf.size() < max)
{
// read data
int lpos = curPos - header.size();
data->seek(lpos);
if ( (data->size() - lpos) < max )
{
buf.append(data->read(data->size()-lpos));
curPos += data->size()-lpos;
}
else
{
int oldbufsize = buf.size();
buf.append(data->read(max - oldbufsize));
curPos += max - oldbufsize;
}
}
int fullsize = header.size() + data->size() + footer.size();
while ((curPos >= ( header.size()+data->size()) && (curPos < fullsize)) && buf.size() < max)
{
buf.append(footer);
curPos += footer.size();
}
memcpy(to, buf.data(), buf.size());
if (buf.size())
return buf.size();
else return -1;
}
qint64 FileSource::writeData ( const char *, qint64)
{
return -1;
}