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
 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
#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;
}
bool FileSource::atEnd ()
{
qDebug() << "!!! AT END req " << (curPos == size());
return curPos == size();
}
qint64 FileSource::pos ()
{
qDebug() << "!!! pos request" << curPos;
return curPos;
}
qint64 FileSource::size ()
{
qDebug() << "!!! size req" << (header.size() + data->size() + footer.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)
{
// read header
buf.append(header.at(curPos));
curPos++;
}
while ((curPos < (header.size()+data->size())) && buf.size() < max)
{
// read data
qDebug() << "!!! cur pos is " << curPos << "and header size is" << header.size() ;
int lpos = curPos - header.size();
qDebug() << "!!!local pos is "<< lpos;
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)
{
// read footer
buf.append(footer.at(curPos-header.size()-data->size()));
curPos++;
}
memcpy(to, buf.data(), buf.size());
qDebug() << "!!! read" << buf.size();
if (buf.size())
return buf.size();
else return -1;
}
qint64 FileSource::writeData ( const char *, qint64)
{
return -1;
}
qint64 FileSource::bytesAvailable()
{
qDebug() << "!!! bytesAvailable req " << size() + QIODevice::bytesAvailable() - curPos;
return size() + QIODevice::bytesAvailable() - curPos;
}
bool FileSource::isSequential()
{
qDebug() << "!!! isSeq req";
return false;
}
bool FileSource::seek(qint64 pos)
{
qDebug() << "!!! seek request";
curPos = pos;
return true;
}