2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/file_upload.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
#include "storage/localimageloader.h"
|
2018-09-04 19:33:57 +00:00
|
|
|
#include "storage/file_download.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_photo.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "data/data_session.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
namespace Storage {
|
2017-03-23 16:11:35 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
// max 512kb uploaded at the same time in each session
|
|
|
|
constexpr auto kMaxUploadFileParallelSize = MTP::kUploadSessionsCount * 512 * 1024;
|
|
|
|
|
|
|
|
constexpr auto kDocumentMaxPartsCount = 3000;
|
|
|
|
|
|
|
|
// 32kb for tiny document ( < 1mb )
|
|
|
|
constexpr auto kDocumentUploadPartSize0 = 32 * 1024;
|
|
|
|
|
|
|
|
// 64kb for little document ( <= 32mb )
|
|
|
|
constexpr auto kDocumentUploadPartSize1 = 64 * 1024;
|
|
|
|
|
|
|
|
// 128kb for small document ( <= 375mb )
|
|
|
|
constexpr auto kDocumentUploadPartSize2 = 128 * 1024;
|
|
|
|
|
|
|
|
// 256kb for medium document ( <= 750mb )
|
|
|
|
constexpr auto kDocumentUploadPartSize3 = 256 * 1024;
|
|
|
|
|
|
|
|
// 512kb for large document ( <= 1500mb )
|
|
|
|
constexpr auto kDocumentUploadPartSize4 = 512 * 1024;
|
|
|
|
|
|
|
|
// One part each half second, if not uploaded faster.
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kUploadRequestInterval = crl::time(500);
|
2017-03-23 16:11:35 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
// How much time without upload causes additional session kill.
|
2019-12-02 13:10:19 +00:00
|
|
|
constexpr auto kKillSessionTimeout = 15 * crl::time(000);
|
2019-01-21 13:42:21 +00:00
|
|
|
|
2017-03-23 16:11:35 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
struct Uploader::File {
|
|
|
|
File(const SendMediaReady &media);
|
|
|
|
File(const std::shared_ptr<FileLoadResult> &file);
|
|
|
|
|
|
|
|
void setDocSize(int32 size);
|
|
|
|
bool setPartSize(uint32 partSize);
|
|
|
|
|
|
|
|
std::shared_ptr<FileLoadResult> file;
|
|
|
|
SendMediaReady media;
|
2018-03-28 20:40:42 +00:00
|
|
|
int32 partsCount = 0;
|
|
|
|
mutable int32 fileSentSize = 0;
|
2017-12-19 16:57:42 +00:00
|
|
|
|
|
|
|
uint64 id() const;
|
|
|
|
SendMediaType type() const;
|
|
|
|
uint64 thumbId() const;
|
|
|
|
const QString &filename() const;
|
|
|
|
|
|
|
|
HashMd5 md5Hash;
|
|
|
|
|
|
|
|
std::unique_ptr<QFile> docFile;
|
|
|
|
int32 docSentParts = 0;
|
|
|
|
int32 docSize = 0;
|
|
|
|
int32 docPartSize = 0;
|
|
|
|
int32 docPartsCount = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Uploader::File::File(const SendMediaReady &media) : media(media) {
|
|
|
|
partsCount = media.parts.size();
|
2019-02-08 16:20:08 +00:00
|
|
|
if (type() == SendMediaType::File
|
2019-09-03 08:25:19 +00:00
|
|
|
|| type() == SendMediaType::ThemeFile
|
2019-02-08 16:20:08 +00:00
|
|
|
|| type() == SendMediaType::Audio) {
|
2017-12-19 16:57:42 +00:00
|
|
|
setDocSize(media.file.isEmpty()
|
|
|
|
? media.data.size()
|
|
|
|
: media.filesize);
|
|
|
|
} else {
|
|
|
|
docSize = docPartSize = docPartsCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Uploader::File::File(const std::shared_ptr<FileLoadResult> &file)
|
|
|
|
: file(file) {
|
2018-03-25 11:37:57 +00:00
|
|
|
partsCount = (type() == SendMediaType::Photo
|
|
|
|
|| type() == SendMediaType::Secure)
|
2017-12-19 16:57:42 +00:00
|
|
|
? file->fileparts.size()
|
|
|
|
: file->thumbparts.size();
|
2019-02-08 16:20:08 +00:00
|
|
|
if (type() == SendMediaType::File
|
2019-09-03 08:25:19 +00:00
|
|
|
|| type() == SendMediaType::ThemeFile
|
2019-02-08 16:20:08 +00:00
|
|
|
|| type() == SendMediaType::Audio) {
|
2017-12-19 16:57:42 +00:00
|
|
|
setDocSize(file->filesize);
|
|
|
|
} else {
|
|
|
|
docSize = docPartSize = docPartsCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Uploader::File::setDocSize(int32 size) {
|
|
|
|
docSize = size;
|
|
|
|
constexpr auto limit0 = 1024 * 1024;
|
|
|
|
constexpr auto limit1 = 32 * limit0;
|
2019-01-18 11:26:43 +00:00
|
|
|
if (docSize >= limit0 || !setPartSize(kDocumentUploadPartSize0)) {
|
|
|
|
if (docSize > limit1 || !setPartSize(kDocumentUploadPartSize1)) {
|
|
|
|
if (!setPartSize(kDocumentUploadPartSize2)) {
|
|
|
|
if (!setPartSize(kDocumentUploadPartSize3)) {
|
|
|
|
if (!setPartSize(kDocumentUploadPartSize4)) {
|
2017-12-19 16:57:42 +00:00
|
|
|
LOG(("Upload Error: bad doc size: %1").arg(docSize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Uploader::File::setPartSize(uint32 partSize) {
|
|
|
|
docPartSize = partSize;
|
|
|
|
docPartsCount = (docSize / docPartSize)
|
|
|
|
+ ((docSize % docPartSize) ? 1 : 0);
|
2019-01-18 11:26:43 +00:00
|
|
|
return (docPartsCount <= kDocumentMaxPartsCount);
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 Uploader::File::id() const {
|
|
|
|
return file ? file->id : media.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
SendMediaType Uploader::File::type() const {
|
|
|
|
return file ? file->type : media.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 Uploader::File::thumbId() const {
|
|
|
|
return file ? file->thumbId : media.thumbId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &Uploader::File::filename() const {
|
|
|
|
return file ? file->filename : media.filename;
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:50:41 +00:00
|
|
|
Uploader::Uploader(not_null<ApiWrap*> api)
|
|
|
|
: _api(api) {
|
2014-05-30 08:53:19 +00:00
|
|
|
nextTimer.setSingleShot(true);
|
|
|
|
connect(&nextTimer, SIGNAL(timeout()), this, SLOT(sendNext()));
|
2018-05-17 19:58:00 +00:00
|
|
|
stopSessionsTimer.setSingleShot(true);
|
|
|
|
connect(&stopSessionsTimer, SIGNAL(timeout()), this, SLOT(stopSessions()));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
void Uploader::uploadMedia(
|
|
|
|
const FullMsgId &msgId,
|
|
|
|
const SendMediaReady &media) {
|
2016-11-28 15:45:07 +00:00
|
|
|
if (media.type == SendMediaType::Photo) {
|
2019-01-18 12:27:37 +00:00
|
|
|
Auth().data().processPhoto(media.photo, media.photoThumbs);
|
|
|
|
} else if (media.type == SendMediaType::File
|
2019-09-03 08:25:19 +00:00
|
|
|
|| media.type == SendMediaType::ThemeFile
|
2019-01-18 12:27:37 +00:00
|
|
|
|| media.type == SendMediaType::Audio) {
|
2019-01-25 14:37:28 +00:00
|
|
|
const auto document = media.photoThumbs.empty()
|
2019-01-18 12:27:37 +00:00
|
|
|
? Auth().data().processDocument(media.document)
|
|
|
|
: Auth().data().processDocument(
|
2018-10-12 16:41:51 +00:00
|
|
|
media.document,
|
2019-01-25 14:37:28 +00:00
|
|
|
base::duplicate(media.photoThumbs.front().second));
|
2016-02-12 16:35:06 +00:00
|
|
|
if (!media.data.isEmpty()) {
|
2019-09-06 11:33:51 +00:00
|
|
|
document->setDataAndCache(media.data);
|
2019-09-03 08:25:19 +00:00
|
|
|
if (media.type == SendMediaType::ThemeFile) {
|
2019-02-08 16:20:08 +00:00
|
|
|
document->checkWallPaperProperties();
|
|
|
|
}
|
2016-02-12 16:35:06 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
if (!media.file.isEmpty()) {
|
2017-03-04 11:28:21 +00:00
|
|
|
document->setLocation(FileLocation(media.file));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
queue.emplace(msgId, File(media));
|
2014-05-30 08:53:19 +00:00
|
|
|
sendNext();
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
void Uploader::upload(
|
|
|
|
const FullMsgId &msgId,
|
|
|
|
const std::shared_ptr<FileLoadResult> &file) {
|
2016-11-28 15:45:07 +00:00
|
|
|
if (file->type == SendMediaType::Photo) {
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto photo = Auth().data().processPhoto(
|
|
|
|
file->photo,
|
|
|
|
file->photoThumbs);
|
|
|
|
photo->uploadingData = std::make_unique<Data::UploadState>(
|
|
|
|
file->partssize);
|
|
|
|
} else if (file->type == SendMediaType::File
|
2019-09-03 08:25:19 +00:00
|
|
|
|| file->type == SendMediaType::ThemeFile
|
2019-01-18 12:27:37 +00:00
|
|
|
|| file->type == SendMediaType::Audio) {
|
|
|
|
const auto document = file->thumb.isNull()
|
|
|
|
? Auth().data().processDocument(file->document)
|
|
|
|
: Auth().data().processDocument(
|
2018-10-12 16:41:51 +00:00
|
|
|
file->document,
|
2018-10-25 08:47:06 +00:00
|
|
|
std::move(file->thumb));
|
2019-01-18 12:27:37 +00:00
|
|
|
document->uploadingData = std::make_unique<Data::UploadState>(
|
|
|
|
document->size);
|
2019-03-01 11:16:55 +00:00
|
|
|
document->setGoodThumbnailOnUpload(
|
2018-10-25 08:47:06 +00:00
|
|
|
std::move(file->goodThumbnail),
|
|
|
|
std::move(file->goodThumbnailBytes));
|
2016-02-12 16:35:06 +00:00
|
|
|
if (!file->content.isEmpty()) {
|
2019-09-06 11:33:51 +00:00
|
|
|
document->setDataAndCache(file->content);
|
2019-09-03 08:25:19 +00:00
|
|
|
if (file->type == SendMediaType::ThemeFile) {
|
2019-02-08 16:20:08 +00:00
|
|
|
document->checkWallPaperProperties();
|
|
|
|
}
|
2016-02-12 16:35:06 +00:00
|
|
|
}
|
2015-10-27 02:39:02 +00:00
|
|
|
if (!file->filepath.isEmpty()) {
|
2017-03-04 11:28:21 +00:00
|
|
|
document->setLocation(FileLocation(file->filepath));
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
queue.emplace(msgId, File(file));
|
2015-10-27 02:39:02 +00:00
|
|
|
sendNext();
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::currentFailed() {
|
2017-12-18 10:38:14 +00:00
|
|
|
auto j = queue.find(uploadingId);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (j != queue.end()) {
|
2017-12-18 10:38:14 +00:00
|
|
|
if (j->second.type() == SendMediaType::Photo) {
|
2018-03-25 11:37:57 +00:00
|
|
|
_photoFailed.fire_copy(j->first);
|
|
|
|
} else if (j->second.type() == SendMediaType::File
|
2019-09-03 08:25:19 +00:00
|
|
|
|| j->second.type() == SendMediaType::ThemeFile
|
2018-03-25 11:37:57 +00:00
|
|
|
|| j->second.type() == SendMediaType::Audio) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto document = Auth().data().document(j->second.id());
|
2017-12-25 14:17:00 +00:00
|
|
|
if (document->uploading()) {
|
2017-12-18 10:38:14 +00:00
|
|
|
document->status = FileUploadFailed;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-03-25 11:37:57 +00:00
|
|
|
_documentFailed.fire_copy(j->first);
|
|
|
|
} else if (j->second.type() == SendMediaType::Secure) {
|
|
|
|
_secureFailed.fire_copy(j->first);
|
|
|
|
} else {
|
|
|
|
Unexpected("Type in Uploader::currentFailed.");
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
queue.erase(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
requestsSent.clear();
|
|
|
|
docRequestsSent.clear();
|
2014-10-30 16:23:44 +00:00
|
|
|
dcMap.clear();
|
2017-12-18 10:38:14 +00:00
|
|
|
uploadingId = FullMsgId();
|
2014-05-30 08:53:19 +00:00
|
|
|
sentSize = 0;
|
2017-03-23 16:11:35 +00:00
|
|
|
for (int i = 0; i < MTP::kUploadSessionsCount; ++i) {
|
2014-10-30 16:23:44 +00:00
|
|
|
sentSizes[i] = 0;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
sendNext();
|
|
|
|
}
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void Uploader::stopSessions() {
|
2017-03-23 16:11:35 +00:00
|
|
|
for (int i = 0; i < MTP::kUploadSessionsCount; ++i) {
|
2017-02-25 16:44:02 +00:00
|
|
|
MTP::stopSession(MTP::uploadDcId(i));
|
2014-10-30 16:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::sendNext() {
|
2017-12-18 10:38:14 +00:00
|
|
|
if (sentSize >= kMaxUploadFileParallelSize || _pausedId.msg) return;
|
2014-10-30 16:23:44 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
bool stopping = stopSessionsTimer.isActive();
|
2017-12-18 10:38:14 +00:00
|
|
|
if (queue.empty()) {
|
2018-05-17 19:58:00 +00:00
|
|
|
if (!stopping) {
|
2019-12-02 13:10:19 +00:00
|
|
|
stopSessionsTimer.start(kKillSessionTimeout);
|
2014-10-30 16:23:44 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
if (stopping) {
|
|
|
|
stopSessionsTimer.stop();
|
2014-10-30 16:23:44 +00:00
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
auto i = uploadingId.msg ? queue.find(uploadingId) : queue.begin();
|
|
|
|
if (!uploadingId.msg) {
|
|
|
|
uploadingId = i->first;
|
2014-05-30 08:53:19 +00:00
|
|
|
} else if (i == queue.end()) {
|
2016-02-12 16:35:06 +00:00
|
|
|
i = queue.begin();
|
2017-12-18 10:38:14 +00:00
|
|
|
uploadingId = i->first;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
auto &uploadingData = i->second;
|
|
|
|
|
|
|
|
auto todc = 0;
|
|
|
|
for (auto dc = 1; dc != MTP::kUploadSessionsCount; ++dc) {
|
2014-10-30 16:23:44 +00:00
|
|
|
if (sentSizes[dc] < sentSizes[todc]) {
|
|
|
|
todc = dc;
|
|
|
|
}
|
|
|
|
}
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
auto &parts = uploadingData.file
|
2018-03-25 11:37:57 +00:00
|
|
|
? ((uploadingData.type() == SendMediaType::Photo
|
|
|
|
|| uploadingData.type() == SendMediaType::Secure)
|
2017-12-18 10:38:14 +00:00
|
|
|
? uploadingData.file->fileparts
|
|
|
|
: uploadingData.file->thumbparts)
|
|
|
|
: uploadingData.media.parts;
|
|
|
|
const auto partsOfId = uploadingData.file
|
2018-03-25 11:37:57 +00:00
|
|
|
? ((uploadingData.type() == SendMediaType::Photo
|
|
|
|
|| uploadingData.type() == SendMediaType::Secure)
|
2017-12-18 10:38:14 +00:00
|
|
|
? uploadingData.file->id
|
|
|
|
: uploadingData.file->thumbId)
|
|
|
|
: uploadingData.media.thumbId;
|
2015-10-27 02:39:02 +00:00
|
|
|
if (parts.isEmpty()) {
|
2017-12-18 10:38:14 +00:00
|
|
|
if (uploadingData.docSentParts >= uploadingData.docPartsCount) {
|
|
|
|
if (requestsSent.empty() && docRequestsSent.empty()) {
|
2019-08-12 12:11:34 +00:00
|
|
|
const auto options = uploadingData.file
|
|
|
|
? uploadingData.file->to.options
|
|
|
|
: Api::SendOptions();
|
2019-03-29 10:51:19 +00:00
|
|
|
const auto edit = uploadingData.file &&
|
|
|
|
uploadingData.file->edit;
|
2017-12-18 10:38:14 +00:00
|
|
|
if (uploadingData.type() == SendMediaType::Photo) {
|
|
|
|
auto photoFilename = uploadingData.filename();
|
2017-03-10 15:45:22 +00:00
|
|
|
if (!photoFilename.endsWith(qstr(".jpg"), Qt::CaseInsensitive)) {
|
|
|
|
// Server has some extensions checking for inputMediaUploadedPhoto,
|
|
|
|
// so force the extension to be .jpg anyway. It doesn't matter,
|
|
|
|
// because the filename from inputFile is not used anywhere.
|
|
|
|
photoFilename += qstr(".jpg");
|
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
const auto md5 = uploadingData.file
|
|
|
|
? uploadingData.file->filemd5
|
|
|
|
: uploadingData.media.jpeg_md5;
|
|
|
|
const auto file = MTP_inputFile(
|
|
|
|
MTP_long(uploadingData.id()),
|
|
|
|
MTP_int(uploadingData.partsCount),
|
|
|
|
MTP_string(photoFilename),
|
|
|
|
MTP_bytes(md5));
|
2019-08-12 12:11:34 +00:00
|
|
|
_photoReady.fire({ uploadingId, options, file, edit });
|
2017-12-18 10:38:14 +00:00
|
|
|
} else if (uploadingData.type() == SendMediaType::File
|
2019-09-03 08:25:19 +00:00
|
|
|
|| uploadingData.type() == SendMediaType::ThemeFile
|
2017-12-18 10:38:14 +00:00
|
|
|
|| uploadingData.type() == SendMediaType::Audio) {
|
2014-05-30 08:53:19 +00:00
|
|
|
QByteArray docMd5(32, Qt::Uninitialized);
|
2017-12-18 10:38:14 +00:00
|
|
|
hashMd5Hex(uploadingData.md5Hash.result(), docMd5.data());
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
const auto file = (uploadingData.docSize > kUseBigFilesFrom)
|
2017-12-18 10:38:14 +00:00
|
|
|
? MTP_inputFileBig(
|
|
|
|
MTP_long(uploadingData.id()),
|
|
|
|
MTP_int(uploadingData.docPartsCount),
|
|
|
|
MTP_string(uploadingData.filename()))
|
|
|
|
: MTP_inputFile(
|
|
|
|
MTP_long(uploadingData.id()),
|
|
|
|
MTP_int(uploadingData.docPartsCount),
|
|
|
|
MTP_string(uploadingData.filename()),
|
|
|
|
MTP_bytes(docMd5));
|
|
|
|
if (uploadingData.partsCount) {
|
|
|
|
const auto thumbFilename = uploadingData.file
|
|
|
|
? uploadingData.file->thumbname
|
|
|
|
: (qsl("thumb.") + uploadingData.media.thumbExt);
|
|
|
|
const auto thumbMd5 = uploadingData.file
|
|
|
|
? uploadingData.file->thumbmd5
|
|
|
|
: uploadingData.media.jpeg_md5;
|
|
|
|
const auto thumb = MTP_inputFile(
|
|
|
|
MTP_long(uploadingData.thumbId()),
|
|
|
|
MTP_int(uploadingData.partsCount),
|
|
|
|
MTP_string(thumbFilename),
|
|
|
|
MTP_bytes(thumbMd5));
|
2018-03-25 11:37:57 +00:00
|
|
|
_thumbDocumentReady.fire({
|
2017-12-18 10:38:14 +00:00
|
|
|
uploadingId,
|
2019-08-12 12:11:34 +00:00
|
|
|
options,
|
2017-12-18 10:38:14 +00:00
|
|
|
file,
|
2019-03-29 08:39:23 +00:00
|
|
|
thumb,
|
2019-03-29 10:51:19 +00:00
|
|
|
edit });
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2019-03-29 10:51:19 +00:00
|
|
|
_documentReady.fire({
|
|
|
|
uploadingId,
|
2019-08-12 12:11:34 +00:00
|
|
|
options,
|
2019-03-29 10:51:19 +00:00
|
|
|
file,
|
|
|
|
edit });
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-03-25 11:37:57 +00:00
|
|
|
} else if (uploadingData.type() == SendMediaType::Secure) {
|
|
|
|
_secureReady.fire({
|
|
|
|
uploadingId,
|
|
|
|
uploadingData.id(),
|
|
|
|
uploadingData.partsCount });
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
queue.erase(uploadingId);
|
|
|
|
uploadingId = FullMsgId();
|
2014-05-30 08:53:19 +00:00
|
|
|
sendNext();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
auto &content = uploadingData.file
|
|
|
|
? uploadingData.file->content
|
|
|
|
: uploadingData.media.data;
|
2014-05-30 08:53:19 +00:00
|
|
|
QByteArray toSend;
|
2015-10-27 02:39:02 +00:00
|
|
|
if (content.isEmpty()) {
|
2017-12-18 10:38:14 +00:00
|
|
|
if (!uploadingData.docFile) {
|
|
|
|
const auto filepath = uploadingData.file
|
|
|
|
? uploadingData.file->filepath
|
|
|
|
: uploadingData.media.file;
|
|
|
|
uploadingData.docFile = std::make_unique<QFile>(filepath);
|
|
|
|
if (!uploadingData.docFile->open(QIODevice::ReadOnly)) {
|
2014-05-30 08:53:19 +00:00
|
|
|
currentFailed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
toSend = uploadingData.docFile->read(uploadingData.docPartSize);
|
2019-01-18 11:26:43 +00:00
|
|
|
if (uploadingData.docSize <= kUseBigFilesFrom) {
|
2017-12-18 10:38:14 +00:00
|
|
|
uploadingData.md5Hash.feed(toSend.constData(), toSend.size());
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-12-18 10:38:14 +00:00
|
|
|
const auto offset = uploadingData.docSentParts
|
|
|
|
* uploadingData.docPartSize;
|
|
|
|
toSend = content.mid(offset, uploadingData.docPartSize);
|
|
|
|
if ((uploadingData.type() == SendMediaType::File
|
2019-09-03 08:25:19 +00:00
|
|
|
|| uploadingData.type() == SendMediaType::ThemeFile
|
2017-12-18 10:38:14 +00:00
|
|
|
|| uploadingData.type() == SendMediaType::Audio)
|
2019-01-18 11:26:43 +00:00
|
|
|
&& uploadingData.docSentParts <= kUseBigFilesFrom) {
|
2017-12-18 10:38:14 +00:00
|
|
|
uploadingData.md5Hash.feed(toSend.constData(), toSend.size());
|
2014-07-04 11:12:54 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
if ((toSend.size() > uploadingData.docPartSize)
|
|
|
|
|| ((toSend.size() < uploadingData.docPartSize
|
|
|
|
&& uploadingData.docSentParts + 1 != uploadingData.docPartsCount))) {
|
2014-05-30 08:53:19 +00:00
|
|
|
currentFailed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mtpRequestId requestId;
|
2019-01-18 11:26:43 +00:00
|
|
|
if (uploadingData.docSize > kUseBigFilesFrom) {
|
2017-12-18 10:38:14 +00:00
|
|
|
requestId = MTP::send(
|
|
|
|
MTPupload_SaveBigFilePart(
|
|
|
|
MTP_long(uploadingData.id()),
|
|
|
|
MTP_int(uploadingData.docSentParts),
|
|
|
|
MTP_int(uploadingData.docPartsCount),
|
|
|
|
MTP_bytes(toSend)),
|
|
|
|
rpcDone(&Uploader::partLoaded),
|
|
|
|
rpcFail(&Uploader::partFailed),
|
|
|
|
MTP::uploadDcId(todc));
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2017-12-18 10:38:14 +00:00
|
|
|
requestId = MTP::send(
|
|
|
|
MTPupload_SaveFilePart(
|
|
|
|
MTP_long(uploadingData.id()),
|
|
|
|
MTP_int(uploadingData.docSentParts),
|
|
|
|
MTP_bytes(toSend)),
|
|
|
|
rpcDone(&Uploader::partLoaded),
|
|
|
|
rpcFail(&Uploader::partFailed),
|
|
|
|
MTP::uploadDcId(todc));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
docRequestsSent.emplace(requestId, uploadingData.docSentParts);
|
|
|
|
dcMap.emplace(requestId, todc);
|
|
|
|
sentSize += uploadingData.docPartSize;
|
|
|
|
sentSizes[todc] += uploadingData.docPartSize;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
uploadingData.docSentParts++;
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2017-12-18 10:38:14 +00:00
|
|
|
auto part = parts.begin();
|
|
|
|
|
|
|
|
const auto requestId = MTP::send(
|
|
|
|
MTPupload_SaveFilePart(
|
|
|
|
MTP_long(partsOfId),
|
|
|
|
MTP_int(part.key()),
|
|
|
|
MTP_bytes(part.value())),
|
|
|
|
rpcDone(&Uploader::partLoaded),
|
|
|
|
rpcFail(&Uploader::partFailed),
|
|
|
|
MTP::uploadDcId(todc));
|
|
|
|
requestsSent.emplace(requestId, part.value());
|
|
|
|
dcMap.emplace(requestId, todc);
|
2014-05-30 08:53:19 +00:00
|
|
|
sentSize += part.value().size();
|
2014-10-30 16:23:44 +00:00
|
|
|
sentSizes[todc] += part.value().size();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-10-27 02:39:02 +00:00
|
|
|
parts.erase(part);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2019-01-18 11:26:43 +00:00
|
|
|
nextTimer.start(kUploadRequestInterval);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::cancel(const FullMsgId &msgId) {
|
2017-12-18 10:38:14 +00:00
|
|
|
uploaded.erase(msgId);
|
|
|
|
if (uploadingId == msgId) {
|
2014-05-30 08:53:19 +00:00
|
|
|
currentFailed();
|
|
|
|
} else {
|
2017-12-18 10:38:14 +00:00
|
|
|
queue.erase(msgId);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::pause(const FullMsgId &msgId) {
|
2017-12-18 10:38:14 +00:00
|
|
|
_pausedId = msgId;
|
2015-12-24 19:26:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::unpause() {
|
2017-12-18 10:38:14 +00:00
|
|
|
_pausedId = FullMsgId();
|
2015-12-24 19:26:28 +00:00
|
|
|
sendNext();
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::confirm(const FullMsgId &msgId) {
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::clear() {
|
2014-05-30 08:53:19 +00:00
|
|
|
uploaded.clear();
|
|
|
|
queue.clear();
|
2017-12-18 15:52:58 +00:00
|
|
|
for (const auto &requestData : requestsSent) {
|
|
|
|
MTP::cancel(requestData.first);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
requestsSent.clear();
|
2017-12-18 15:52:58 +00:00
|
|
|
for (const auto &requestData : docRequestsSent) {
|
|
|
|
MTP::cancel(requestData.first);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
docRequestsSent.clear();
|
2014-10-30 16:23:44 +00:00
|
|
|
dcMap.clear();
|
2014-05-30 08:53:19 +00:00
|
|
|
sentSize = 0;
|
2017-03-23 16:11:35 +00:00
|
|
|
for (int i = 0; i < MTP::kUploadSessionsCount; ++i) {
|
2017-02-25 16:44:02 +00:00
|
|
|
MTP::stopSession(MTP::uploadDcId(i));
|
2014-10-30 16:23:44 +00:00
|
|
|
sentSizes[i] = 0;
|
|
|
|
}
|
2018-05-17 19:58:00 +00:00
|
|
|
stopSessionsTimer.stop();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::partLoaded(const MTPBool &result, mtpRequestId requestId) {
|
2017-12-18 10:38:14 +00:00
|
|
|
auto j = docRequestsSent.end();
|
|
|
|
auto i = requestsSent.find(requestId);
|
2014-05-30 08:53:19 +00:00
|
|
|
if (i == requestsSent.cend()) {
|
|
|
|
j = docRequestsSent.find(requestId);
|
|
|
|
}
|
|
|
|
if (i != requestsSent.cend() || j != docRequestsSent.cend()) {
|
2015-10-29 00:16:52 +00:00
|
|
|
if (mtpIsFalse(result)) { // failed to upload current file
|
2014-05-30 08:53:19 +00:00
|
|
|
currentFailed();
|
|
|
|
return;
|
|
|
|
} else {
|
2017-12-18 10:38:14 +00:00
|
|
|
auto dcIt = dcMap.find(requestId);
|
2014-10-30 16:23:44 +00:00
|
|
|
if (dcIt == dcMap.cend()) { // must not happen
|
|
|
|
currentFailed();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
auto dc = dcIt->second;
|
2014-10-30 16:23:44 +00:00
|
|
|
dcMap.erase(dcIt);
|
|
|
|
|
2015-12-24 19:26:28 +00:00
|
|
|
int32 sentPartSize = 0;
|
2017-12-18 10:38:14 +00:00
|
|
|
auto k = queue.find(uploadingId);
|
|
|
|
Assert(k != queue.cend());
|
|
|
|
auto &[fullId, file] = *k;
|
2014-05-30 08:53:19 +00:00
|
|
|
if (i != requestsSent.cend()) {
|
2017-12-18 10:38:14 +00:00
|
|
|
sentPartSize = i->second.size();
|
2014-05-30 08:53:19 +00:00
|
|
|
requestsSent.erase(i);
|
|
|
|
} else {
|
2017-12-18 10:38:14 +00:00
|
|
|
sentPartSize = file.docPartSize;
|
2014-05-30 08:53:19 +00:00
|
|
|
docRequestsSent.erase(j);
|
|
|
|
}
|
2015-12-24 19:26:28 +00:00
|
|
|
sentSize -= sentPartSize;
|
|
|
|
sentSizes[dc] -= sentPartSize;
|
2017-12-18 10:38:14 +00:00
|
|
|
if (file.type() == SendMediaType::Photo) {
|
|
|
|
file.fileSentSize += sentPartSize;
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto photo = Auth().data().photo(file.id());
|
2017-12-18 10:38:14 +00:00
|
|
|
if (photo->uploading() && file.file) {
|
|
|
|
photo->uploadingData->size = file.file->partssize;
|
|
|
|
photo->uploadingData->offset = file.fileSentSize;
|
2015-12-24 19:26:28 +00:00
|
|
|
}
|
2018-03-25 11:37:57 +00:00
|
|
|
_photoProgress.fire_copy(fullId);
|
2017-12-18 10:38:14 +00:00
|
|
|
} else if (file.type() == SendMediaType::File
|
2019-09-03 08:25:19 +00:00
|
|
|
|| file.type() == SendMediaType::ThemeFile
|
2017-12-18 10:38:14 +00:00
|
|
|
|| file.type() == SendMediaType::Audio) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto document = Auth().data().document(file.id());
|
2017-12-18 10:38:14 +00:00
|
|
|
if (document->uploading()) {
|
|
|
|
const auto doneParts = file.docSentParts
|
|
|
|
- int(docRequestsSent.size());
|
2017-12-27 18:57:07 +00:00
|
|
|
document->uploadingData->offset = std::min(
|
2017-12-25 14:17:00 +00:00
|
|
|
document->uploadingData->size,
|
|
|
|
doneParts * file.docPartSize);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-03-25 11:37:57 +00:00
|
|
|
_documentProgress.fire_copy(fullId);
|
|
|
|
} else if (file.type() == SendMediaType::Secure) {
|
2018-03-28 20:40:42 +00:00
|
|
|
file.fileSentSize += sentPartSize;
|
|
|
|
_secureProgress.fire_copy({
|
|
|
|
fullId,
|
|
|
|
file.fileSentSize,
|
|
|
|
file.file->partssize });
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendNext();
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
bool Uploader::partFailed(const RPCError &error, mtpRequestId requestId) {
|
2016-04-08 10:44:35 +00:00
|
|
|
if (MTP::isDefaultHandledError(error)) return false;
|
2015-04-04 20:01:34 +00:00
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
// failed to upload current file
|
|
|
|
if ((requestsSent.find(requestId) != requestsSent.cend())
|
|
|
|
|| (docRequestsSent.find(requestId) != docRequestsSent.cend())) {
|
2014-05-30 08:53:19 +00:00
|
|
|
currentFailed();
|
|
|
|
}
|
|
|
|
sendNext();
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-04 14:54:32 +00:00
|
|
|
|
|
|
|
Uploader::~Uploader() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Storage
|