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"
|
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"
|
|
|
|
#include "auth_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 {
|
|
|
|
|
|
|
|
constexpr auto kMaxUploadFileParallelSize = MTP::kUploadSessionsCount * 512 * 1024; // max 512kb uploaded at the same time in each session
|
|
|
|
|
|
|
|
} // 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;
|
|
|
|
int32 partsCount;
|
|
|
|
mutable int32 fileSentSize;
|
|
|
|
|
|
|
|
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();
|
|
|
|
if (type() == SendMediaType::File || type() == SendMediaType::Audio) {
|
|
|
|
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) {
|
|
|
|
partsCount = (type() == SendMediaType::Photo)
|
|
|
|
? file->fileparts.size()
|
|
|
|
: file->thumbparts.size();
|
|
|
|
if (type() == SendMediaType::File || type() == SendMediaType::Audio) {
|
|
|
|
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;
|
|
|
|
if (docSize >= limit0 || !setPartSize(DocumentUploadPartSize0)) {
|
|
|
|
if (docSize > limit1 || !setPartSize(DocumentUploadPartSize1)) {
|
|
|
|
if (!setPartSize(DocumentUploadPartSize2)) {
|
|
|
|
if (!setPartSize(DocumentUploadPartSize3)) {
|
|
|
|
if (!setPartSize(DocumentUploadPartSize4)) {
|
|
|
|
LOG(("Upload Error: bad doc size: %1").arg(docSize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Uploader::File::setPartSize(uint32 partSize) {
|
|
|
|
docPartSize = partSize;
|
|
|
|
docPartsCount = (docSize / docPartSize)
|
|
|
|
+ ((docSize % docPartSize) ? 1 : 0);
|
|
|
|
return (docPartsCount <= DocumentMaxPartsCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
Uploader::Uploader() {
|
2014-05-30 08:53:19 +00:00
|
|
|
nextTimer.setSingleShot(true);
|
|
|
|
connect(&nextTimer, SIGNAL(timeout()), this, SLOT(sendNext()));
|
2014-10-30 16:23:44 +00:00
|
|
|
killSessionsTimer.setSingleShot(true);
|
|
|
|
connect(&killSessionsTimer, SIGNAL(timeout()), this, SLOT(killSessions()));
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::uploadMedia(const FullMsgId &msgId, const SendMediaReady &media) {
|
2016-11-28 15:45:07 +00:00
|
|
|
if (media.type == SendMediaType::Photo) {
|
2018-01-17 16:21:01 +00:00
|
|
|
Auth().data().photo(media.photo, media.photoThumbs);
|
2016-11-28 15:45:07 +00:00
|
|
|
} else if (media.type == SendMediaType::File || media.type == SendMediaType::Audio) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto document = media.photoThumbs.isEmpty()
|
|
|
|
? Auth().data().document(media.document)
|
|
|
|
: Auth().data().document(media.document, media.photoThumbs.begin().value());
|
2016-02-12 16:35:06 +00:00
|
|
|
if (!media.data.isEmpty()) {
|
|
|
|
document->setData(media.data);
|
|
|
|
}
|
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) {
|
2018-01-17 16:21:01 +00:00
|
|
|
auto photo = Auth().data().photo(file->photo, file->photoThumbs);
|
2017-12-25 14:17:00 +00:00
|
|
|
photo->uploadingData = std::make_unique<Data::UploadState>(file->partssize);
|
2016-11-28 15:45:07 +00:00
|
|
|
} else if (file->type == SendMediaType::File || file->type == SendMediaType::Audio) {
|
2018-01-17 16:21:01 +00:00
|
|
|
auto document = file->thumb.isNull()
|
|
|
|
? Auth().data().document(file->document)
|
|
|
|
: Auth().data().document(file->document, file->thumb);
|
2017-12-25 14:17:00 +00:00
|
|
|
document->uploadingData = std::make_unique<Data::UploadState>(document->size);
|
2016-02-12 16:35:06 +00:00
|
|
|
if (!file->content.isEmpty()) {
|
|
|
|
document->setData(file->content);
|
|
|
|
}
|
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) {
|
|
|
|
emit photoFailed(j->first);
|
|
|
|
} else if (j->second.type() == SendMediaType::File) {
|
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
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
emit documentFailed(j->first);
|
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();
|
|
|
|
}
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
void Uploader::killSessions() {
|
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
|
|
|
|
|
|
|
bool killing = killSessionsTimer.isActive();
|
2017-12-18 10:38:14 +00:00
|
|
|
if (queue.empty()) {
|
2014-10-30 16:23:44 +00:00
|
|
|
if (!killing) {
|
2014-11-05 17:43:32 +00:00
|
|
|
killSessionsTimer.start(MTPAckSendWaiting + MTPKillFileSessionTimeout);
|
2014-10-30 16:23:44 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2014-10-30 16:23:44 +00:00
|
|
|
if (killing) {
|
|
|
|
killSessionsTimer.stop();
|
|
|
|
}
|
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
|
|
|
|
? (uploadingData.type() == SendMediaType::Photo
|
|
|
|
? uploadingData.file->fileparts
|
|
|
|
: uploadingData.file->thumbparts)
|
|
|
|
: uploadingData.media.parts;
|
|
|
|
const auto partsOfId = uploadingData.file
|
|
|
|
? (uploadingData.type() == SendMediaType::Photo
|
|
|
|
? 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()) {
|
|
|
|
const auto silent = uploadingData.file
|
|
|
|
&& uploadingData.file->to.silent;
|
|
|
|
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));
|
|
|
|
emit photoReady(uploadingId, silent, file);
|
|
|
|
} else if (uploadingData.type() == SendMediaType::File
|
|
|
|
|| 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());
|
|
|
|
|
|
|
|
const auto file = (uploadingData.docSize > UseBigFilesFrom)
|
|
|
|
? 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));
|
|
|
|
emit thumbDocumentReady(
|
|
|
|
uploadingId,
|
|
|
|
silent,
|
|
|
|
file,
|
|
|
|
thumb);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2017-12-18 10:38:14 +00:00
|
|
|
emit documentReady(uploadingId, silent, file);
|
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);
|
|
|
|
if (uploadingData.docSize <= UseBigFilesFrom) {
|
|
|
|
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
|
|
|
|
|| uploadingData.type() == SendMediaType::Audio)
|
|
|
|
&& uploadingData.docSentParts <= UseBigFilesFrom) {
|
|
|
|
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;
|
2017-12-18 10:38:14 +00:00
|
|
|
if (uploadingData.docSize > UseBigFilesFrom) {
|
|
|
|
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
|
|
|
}
|
|
|
|
nextTimer.start(UploadRequestInterval);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
killSessionsTimer.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
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
emit photoProgress(fullId);
|
|
|
|
} else if (file.type() == SendMediaType::File
|
|
|
|
|| 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
|
|
|
}
|
2017-12-18 10:38:14 +00:00
|
|
|
emit documentProgress(fullId);
|
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
|