Fix byte range for file resource requests

This commit is contained in:
Atlante45 2017-04-21 19:16:56 -07:00
parent 194541b2d0
commit 043c587395
3 changed files with 19 additions and 26 deletions

View file

@ -68,11 +68,9 @@ void SendAssetTask::run() {
QFile file { filePath };
if (file.open(QIODevice::ReadOnly)) {
if (!byteRange.isSet()) {
// if the byte range is not set, force it to be from 0 to the end of the file
byteRange.fromInclusive = 0;
byteRange.toExclusive = file.size();
}
// first fixup the range based on the now known file size
byteRange.fixupRange(file.size());
// check if we're being asked to read data that we just don't have
// because of the file size
@ -82,10 +80,6 @@ void SendAssetTask::run() {
<< byteRange.fromInclusive << ":" << byteRange.toExclusive;
} else {
// we have a valid byte range, handle it and send the asset
// first fixup the range based on the now known file size
byteRange.fixupRange(file.size());
auto size = byteRange.size();
if (byteRange.fromInclusive >= 0) {

View file

@ -30,6 +30,12 @@ struct ByteRange {
}
void fixupRange(int64_t fileSize) {
if (!isSet()) {
// if the byte range is not set, force it to be from 0 to the end of the file
fromInclusive = 0;
toExclusive = fileSize;
}
if (fromInclusive > 0 && toExclusive == 0) {
// we have a left side of the range that is non-zero
// if the RHS of the range is zero, set it to the end of the file now

View file

@ -34,24 +34,17 @@ void FileResourceRequest::doSend() {
if (file.size() < _byteRange.fromInclusive || file.size() < _byteRange.toExclusive) {
_result = ResourceRequest::InvalidByteRange;
} else {
if (!_byteRange.isSet()) {
// no byte range, read the whole file
_data = file.readAll();
// fix it up based on the known size of the file
_byteRange.fixupRange(file.size());
if (_byteRange.fromInclusive >= 0) {
// this is a positive byte range, simply skip to that part of the file and read from there
file.seek(_byteRange.fromInclusive);
_data = file.read(_byteRange.size());
} else {
// we have a byte range to handle
// fix it up based on the known size of the file
_byteRange.fixupRange(file.size());
if (_byteRange.fromInclusive > 0) {
// this is a positive byte range, simply skip to that part of the file and read from there
file.seek(_byteRange.fromInclusive);
_data = file.read(_byteRange.size());
} else {
// this is a negative byte range, we'll need to grab data from the end of the file first
file.seek(file.size() + _byteRange.fromInclusive);
_data = file.read(_byteRange.size());
}
// this is a negative byte range, we'll need to grab data from the end of the file first
file.seek(file.size() + _byteRange.fromInclusive);
_data = file.read(_byteRange.size());
}
_result = ResourceRequest::Success;