mirror of
https://github.com/overte-org/overte.git
synced 2025-04-18 00:26:33 +02:00
add byte range handling to FileResourceRequest
This commit is contained in:
parent
8145e416f9
commit
229a481232
2 changed files with 32 additions and 1 deletions
|
@ -11,6 +11,8 @@
|
|||
|
||||
#include "FileResourceRequest.h"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <QFile>
|
||||
|
||||
void FileResourceRequest::doSend() {
|
||||
|
@ -25,7 +27,35 @@ void FileResourceRequest::doSend() {
|
|||
QFile file(filename);
|
||||
if (file.exists()) {
|
||||
if (file.open(QFile::ReadOnly)) {
|
||||
_data = file.readAll();
|
||||
|
||||
if (!_byteRange.isSet()) {
|
||||
// no byte range, read the whole file
|
||||
_data = file.readAll();
|
||||
} else {
|
||||
if (file.size() < std::abs(_byteRange.fromInclusive) || file.size() < _byteRange.toExclusive) {
|
||||
_result = ResourceRequest::InvalidByteRange;
|
||||
} else {
|
||||
// we have a byte range to handle
|
||||
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.fromInclusive);
|
||||
|
||||
if (_byteRange.toExclusive > 0) {
|
||||
// there is additional data to read from the front of the file
|
||||
// handle that now
|
||||
file.seek(0);
|
||||
_data.append(file.read(_byteRange.toExclusive));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
_result = ResourceRequest::Success;
|
||||
} else {
|
||||
_result = ResourceRequest::AccessDenied;
|
||||
|
|
|
@ -37,6 +37,7 @@ public:
|
|||
Timeout,
|
||||
ServerUnavailable,
|
||||
AccessDenied,
|
||||
InvalidByteRange,
|
||||
InvalidURL,
|
||||
NotFound
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue