| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- /*
- 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"
- #include <QtCore/QBuffer>
- namespace Media {
- namespace Streaming {
- namespace {
- // This is the maximum file size in Telegram API.
- constexpr auto kMaxFileSize = 8000 * int64(512 * 1024);
- [[nodiscard]] int64 ValidateLocalSize(int64 size) {
- return (size > 0 && size <= kMaxFileSize) ? size : 0;
- }
- } // namespace
- LoaderLocal::LoaderLocal(std::unique_ptr<QIODevice> device)
- : _device(std::move(device))
- , _size(ValidateLocalSize(_device->size())) {
- Expects(_device != nullptr);
- if (!_size || !_device->open(QIODevice::ReadOnly)) {
- fail();
- }
- }
- Storage::Cache::Key LoaderLocal::baseCacheKey() const {
- return {};
- }
- int64 LoaderLocal::size() const {
- return _size;
- }
- void LoaderLocal::load(int64 offset) {
- 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 });
- });
- }
- void LoaderLocal::cancel(int64 offset) {
- }
- void LoaderLocal::resetPriorities() {
- }
- void LoaderLocal::setPriority(int priority) {
- }
- void LoaderLocal::stop() {
- }
- void LoaderLocal::tryRemoveFromQueue() {
- }
- rpl::producer<LoadedPart> LoaderLocal::parts() const {
- return _parts.events();
- }
- rpl::producer<SpeedEstimate> LoaderLocal::speedEstimate() const {
- return rpl::never<SpeedEstimate>();
- }
- void LoaderLocal::attachDownloader(
- not_null<Storage::StreamedFileDownloader*> downloader) {
- Unexpected("Downloader attached to a local streaming loader.");
- }
- void LoaderLocal::clearAttachedDownloader() {
- Unexpected("Downloader detached from a local streaming loader.");
- }
- 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
|