2019-03-04 12:26:29 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "media/streaming/media_streaming_loader_local.h"
|
|
|
|
|
|
|
|
#include "storage/cache/storage_cache_types.h"
|
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QBuffer>
|
|
|
|
|
2019-03-04 12:26:29 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Streaming {
|
2019-03-20 10:17:40 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This is the maximum file size in Telegram API.
|
2022-05-10 14:22:28 +00:00
|
|
|
constexpr auto kMaxFileSize = 8000 * int64(512 * 1024);
|
2019-03-20 10:17:40 +00:00
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
[[nodiscard]] int64 ValidateLocalSize(int64 size) {
|
|
|
|
return (size > 0 && size <= kMaxFileSize) ? size : 0;
|
2019-03-20 10:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2019-03-04 12:26:29 +00:00
|
|
|
|
|
|
|
LoaderLocal::LoaderLocal(std::unique_ptr<QIODevice> device)
|
2019-03-20 10:17:40 +00:00
|
|
|
: _device(std::move(device))
|
|
|
|
, _size(ValidateLocalSize(_device->size())) {
|
2019-03-04 12:26:29 +00:00
|
|
|
Expects(_device != nullptr);
|
|
|
|
|
2019-03-20 10:17:40 +00:00
|
|
|
if (!_size || !_device->open(QIODevice::ReadOnly)) {
|
2019-03-04 12:26:29 +00:00
|
|
|
fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:28:18 +00:00
|
|
|
Storage::Cache::Key LoaderLocal::baseCacheKey() const {
|
|
|
|
return {};
|
2019-03-04 12:26:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
int64 LoaderLocal::size() const {
|
2019-03-20 10:17:40 +00:00
|
|
|
return _size;
|
2019-03-04 12:26:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
void LoaderLocal::load(int64 offset) {
|
2019-03-04 12:26:29 +00:00
|
|
|
if (_device->pos() != offset && !_device->seek(offset)) {
|
|
|
|
fail();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto result = _device->read(kPartSize);
|
|
|
|
if (result.isEmpty()
|
|
|
|
|| ((result.size() != kPartSize)
|
|
|
|
&& (offset + result.size() != size()))) {
|
|
|
|
fail();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
crl::on_main(this, [=, result = std::move(result)]() mutable {
|
|
|
|
_parts.fire({ offset, std::move(result) });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoaderLocal::fail() {
|
|
|
|
crl::on_main(this, [=] {
|
|
|
|
_parts.fire({ LoadedPart::kFailedOffset });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
void LoaderLocal::cancel(int64 offset) {
|
2019-03-04 12:26:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 10:27:20 +00:00
|
|
|
void LoaderLocal::resetPriorities() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoaderLocal::setPriority(int priority) {
|
2019-03-04 12:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoaderLocal::stop() {
|
|
|
|
}
|
|
|
|
|
2019-12-23 11:13:32 +00:00
|
|
|
void LoaderLocal::tryRemoveFromQueue() {
|
|
|
|
}
|
|
|
|
|
2019-03-04 12:26:29 +00:00
|
|
|
rpl::producer<LoadedPart> LoaderLocal::parts() const {
|
|
|
|
return _parts.events();
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:25:00 +00:00
|
|
|
void LoaderLocal::attachDownloader(
|
2019-12-04 06:51:21 +00:00
|
|
|
not_null<Storage::StreamedFileDownloader*> downloader) {
|
2019-04-12 09:25:00 +00:00
|
|
|
Unexpected("Downloader attached to a local streaming loader.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoaderLocal::clearAttachedDownloader() {
|
|
|
|
Unexpected("Downloader detached from a local streaming loader.");
|
|
|
|
}
|
|
|
|
|
2019-03-04 12:26:29 +00:00
|
|
|
std::unique_ptr<LoaderLocal> MakeFileLoader(const QString &path) {
|
|
|
|
return std::make_unique<LoaderLocal>(std::make_unique<QFile>(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<LoaderLocal> MakeBytesLoader(const QByteArray &bytes) {
|
|
|
|
auto device = std::make_unique<QBuffer>();
|
|
|
|
auto copy = new QByteArray(bytes);
|
|
|
|
QObject::connect(device.get(), &QBuffer::destroyed, [=] {
|
|
|
|
delete copy;
|
|
|
|
});
|
|
|
|
device->setBuffer(copy);
|
|
|
|
return std::make_unique<LoaderLocal>(std::move(device));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Streaming
|
|
|
|
} // namespace Media
|