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

View file

@ -30,6 +30,12 @@ struct ByteRange {
} }
void fixupRange(int64_t fileSize) { 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) { if (fromInclusive > 0 && toExclusive == 0) {
// we have a left side of the range that is non-zero // 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 // if the RHS of the range is zero, set it to the end of the file now

View file

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