2012-07-28 20:35:12 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
2012-07-30 13:41:29 +02:00
|
|
|
|
2012-07-28 20:35:12 +02:00
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2012-07-30 13:41:29 +02:00
|
|
|
|
2012-07-28 20:35:12 +02:00
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2012-07-30 13:41:29 +02:00
|
|
|
|
2012-07-28 20:35:12 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
#include "cloudstream.h"
|
2012-07-28 20:35:12 +02:00
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <taglib/id3v2framefactory.h>
|
|
|
|
#include <taglib/mpegfile.h>
|
|
|
|
|
2012-07-28 20:35:12 +02:00
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
|
2012-07-30 13:41:29 +02:00
|
|
|
namespace {
|
2014-02-07 16:34:20 +01:00
|
|
|
static const int kTaglibPrefixCacheBytes = 64 * 1024; // Should be enough.
|
|
|
|
static const int kTaglibSuffixCacheBytes = 8 * 1024;
|
2020-09-18 16:15:19 +02:00
|
|
|
} // namespace
|
2012-07-28 20:35:12 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
CloudStream::CloudStream(const QUrl& url, const QString& filename,
|
2019-08-02 17:24:26 +02:00
|
|
|
const long length, const QString& auth)
|
2012-07-28 20:35:12 +02:00
|
|
|
: url_(url),
|
|
|
|
filename_(filename),
|
|
|
|
encoded_filename_(filename_.toUtf8()),
|
|
|
|
length_(length),
|
|
|
|
auth_(auth),
|
|
|
|
cursor_(0),
|
2019-08-02 17:24:26 +02:00
|
|
|
network_(new QNetworkAccessManager),
|
2012-07-30 13:41:29 +02:00
|
|
|
cache_(length),
|
2014-02-07 16:34:20 +01:00
|
|
|
num_requests_(0) {}
|
2012-07-28 20:35:12 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
TagLib::FileName CloudStream::name() const { return encoded_filename_.data(); }
|
2012-07-28 20:35:12 +02:00
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
bool CloudStream::CheckCache(int start, int end) {
|
2012-07-28 20:35:12 +02:00
|
|
|
for (int i = start; i <= end; ++i) {
|
|
|
|
if (!cache_.test(i)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
void CloudStream::FillCache(int start, TagLib::ByteVector data) {
|
2012-07-28 20:35:12 +02:00
|
|
|
for (int i = 0; i < data.size(); ++i) {
|
|
|
|
cache_.set(start + i, data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
TagLib::ByteVector CloudStream::GetCached(int start, int end) {
|
2012-07-28 20:35:12 +02:00
|
|
|
const uint size = end - start + 1;
|
|
|
|
TagLib::ByteVector ret(size);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
ret[i] = cache_.get(start + i);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
void CloudStream::Precache() {
|
2012-07-30 13:41:29 +02:00
|
|
|
// For reading the tags of an MP3, TagLib tends to request:
|
|
|
|
// 1. The first 1024 bytes
|
|
|
|
// 2. Somewhere between the first 2KB and first 60KB
|
|
|
|
// 3. The last KB or two.
|
|
|
|
// 4. Somewhere in the first 64KB again
|
|
|
|
//
|
2012-07-31 11:57:04 +02:00
|
|
|
// OGG Vorbis may read the last 4KB.
|
|
|
|
//
|
|
|
|
// So, if we precache the first 64KB and the last 8KB we should be sorted :-)
|
|
|
|
// Ideally, we would use bytes=0-655364,-8096 but Google Drive does not seem
|
2012-07-30 13:41:29 +02:00
|
|
|
// to support multipart byte ranges yet so we have to make do with two
|
|
|
|
// requests.
|
|
|
|
|
|
|
|
seek(0, TagLib::IOStream::Beginning);
|
|
|
|
readBlock(kTaglibPrefixCacheBytes);
|
|
|
|
seek(kTaglibSuffixCacheBytes, TagLib::IOStream::End);
|
|
|
|
readBlock(kTaglibSuffixCacheBytes);
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
TagLib::ByteVector CloudStream::readBlock(ulong length) {
|
2012-07-28 20:35:12 +02:00
|
|
|
const uint start = cursor_;
|
|
|
|
const uint end = qMin(cursor_ + length - 1, length_ - 1);
|
|
|
|
|
2012-07-31 11:57:04 +02:00
|
|
|
if (end < start) {
|
2012-07-28 20:35:12 +02:00
|
|
|
return TagLib::ByteVector();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CheckCache(start, end)) {
|
|
|
|
TagLib::ByteVector cached = GetCached(start, end);
|
|
|
|
cursor_ += cached.size();
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkRequest request = QNetworkRequest(url_);
|
2012-12-13 14:27:21 +01:00
|
|
|
if (!auth_.isEmpty()) {
|
2012-11-29 20:18:08 +01:00
|
|
|
request.setRawHeader("Authorization", auth_.toUtf8());
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
request.setRawHeader("Range",
|
|
|
|
QString("bytes=%1-%2").arg(start).arg(end).toUtf8());
|
2012-11-28 14:43:03 +01:00
|
|
|
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
|
|
|
|
QNetworkRequest::AlwaysNetwork);
|
2021-02-24 06:04:10 +01:00
|
|
|
// TODO: Use RedirectPolicyAttribute after baseline moves to Qt 5.9.
|
|
|
|
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
2012-07-28 20:35:12 +02:00
|
|
|
|
|
|
|
QNetworkReply* reply = network_->get(request);
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
|
|
|
|
SLOT(SSLErrors(QList<QSslError>)));
|
2012-07-30 13:41:29 +02:00
|
|
|
++num_requests_;
|
2012-07-28 20:35:12 +02:00
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
|
|
|
loop.exec();
|
|
|
|
reply->deleteLater();
|
|
|
|
|
2012-11-30 16:12:12 +01:00
|
|
|
int code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
if (code >= 400) {
|
|
|
|
qLog(Debug) << "Error retrieving url to tag:" << url_;
|
|
|
|
return TagLib::ByteVector();
|
|
|
|
}
|
|
|
|
|
2012-07-28 20:35:12 +02:00
|
|
|
QByteArray data = reply->readAll();
|
|
|
|
TagLib::ByteVector bytes(data.data(), data.size());
|
|
|
|
cursor_ += data.size();
|
|
|
|
|
|
|
|
FillCache(start, bytes);
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
void CloudStream::writeBlock(const TagLib::ByteVector&) {
|
2012-07-28 20:35:12 +02:00
|
|
|
qLog(Debug) << Q_FUNC_INFO << "not implemented";
|
|
|
|
}
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
void CloudStream::insert(const TagLib::ByteVector&, ulong, ulong) {
|
2012-07-28 20:35:12 +02:00
|
|
|
qLog(Debug) << Q_FUNC_INFO << "not implemented";
|
|
|
|
}
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
void CloudStream::removeBlock(ulong, ulong) {
|
2012-07-28 20:35:12 +02:00
|
|
|
qLog(Debug) << Q_FUNC_INFO << "not implemented";
|
|
|
|
}
|
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
bool CloudStream::readOnly() const {
|
2012-07-28 20:35:12 +02:00
|
|
|
qLog(Debug) << Q_FUNC_INFO;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool CloudStream::isOpen() const { return true; }
|
2012-07-28 20:35:12 +02:00
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
void CloudStream::seek(long offset, TagLib::IOStream::Position p) {
|
2012-07-28 20:35:12 +02:00
|
|
|
switch (p) {
|
|
|
|
case TagLib::IOStream::Beginning:
|
|
|
|
cursor_ = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TagLib::IOStream::Current:
|
|
|
|
cursor_ = qMin(ulong(cursor_ + offset), length_);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TagLib::IOStream::End:
|
2012-07-31 11:57:04 +02:00
|
|
|
// This should really not have qAbs(), but OGG reading needs it.
|
|
|
|
cursor_ = qMax(0UL, length_ - qAbs(offset));
|
2012-07-28 20:35:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void CloudStream::clear() { cursor_ = 0; }
|
2012-07-28 20:35:12 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
long CloudStream::tell() const { return cursor_; }
|
2012-07-28 20:35:12 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
long CloudStream::length() { return length_; }
|
2012-07-28 20:35:12 +02:00
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
void CloudStream::truncate(long) {
|
2012-07-28 20:35:12 +02:00
|
|
|
qLog(Debug) << Q_FUNC_INFO << "not implemented";
|
|
|
|
}
|
2012-11-28 14:43:03 +01:00
|
|
|
|
2012-11-28 16:25:57 +01:00
|
|
|
void CloudStream::SSLErrors(const QList<QSslError>& errors) {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QSslError& error : errors) {
|
2012-11-28 14:43:03 +01:00
|
|
|
qLog(Debug) << error.error() << error.errorString();
|
|
|
|
qLog(Debug) << error.certificate();
|
|
|
|
}
|
|
|
|
}
|