2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-09-01 15:05:56 +00:00

Allow sending videos with covers.

This commit is contained in:
John Preston
2025-01-29 09:24:01 +04:00
parent 6a415cf232
commit e05bb75b8a
42 changed files with 571 additions and 104 deletions

View File

@@ -335,6 +335,11 @@ void Uploader::upload(
if (file->type == SendMediaType::ThemeFile) {
document->checkWallPaperProperties();
}
if (file->videoCover) {
session().data().processPhoto(
file->videoCover->photo,
file->videoCover->photoThumbs);
}
}
_queue.push_back({ itemId, file });
if (!_nextTimer.isActive()) {
@@ -348,9 +353,26 @@ void Uploader::failed(FullMsgId itemId) {
const auto entry = std::move(*i);
_queue.erase(i);
notifyFailed(entry);
} else if (const auto coverId = _videoIdToCoverId.take(itemId)) {
if (const auto video = _videoWaitingCover.take(*coverId)) {
const auto document = session().data().document(video->id);
if (document->uploading()) {
document->status = FileUploadFailed;
}
_documentFailed.fire_copy(video->fullId);
}
failed(*coverId);
} else if (const auto video = _videoWaitingCover.take(itemId)) {
_videoIdToCoverId.remove(video->fullId);
const auto document = session().data().document(video->id);
if (document->uploading()) {
document->status = FileUploadFailed;
}
_documentFailed.fire_copy(video->fullId);
}
cancelRequests(itemId);
maybeFinishFront();
crl::on_main(this, [=] {
maybeSend();
});
@@ -854,7 +876,8 @@ void Uploader::finishFront() {
MTP_int(entry.parts->size()),
MTP_string(photoFilename),
MTP_bytes(md5));
_photoReady.fire({
auto ready = UploadedMedia{
.id = entry.file->id,
.fullId = entry.itemId,
.info = {
.file = file,
@@ -862,7 +885,13 @@ void Uploader::finishFront() {
},
.options = options,
.edit = edit,
});
};
const auto i = _videoWaitingCover.find(entry.itemId);
if (i != end(_videoWaitingCover)) {
uploadCoverAsPhoto(i->second.fullId, std::move(ready));
} else {
_photoReady.fire(std::move(ready));
}
} else if (entry.file->type == SendMediaType::File
|| entry.file->type == SendMediaType::ThemeFile
|| entry.file->type == SendMediaType::Audio
@@ -892,7 +921,8 @@ void Uploader::finishFront() {
MTP_string(thumbFilename),
MTP_bytes(thumbMd5));
}();
_documentReady.fire({
auto ready = UploadedMedia{
.id = entry.file->id,
.fullId = entry.itemId,
.info = {
.file = file,
@@ -901,7 +931,12 @@ void Uploader::finishFront() {
},
.options = options,
.edit = edit,
});
};
if (entry.file->videoCover) {
uploadVideoCover(std::move(ready), entry.file->videoCover);
} else {
_documentReady.fire(std::move(ready));
}
} else if (entry.file->type == SendMediaType::Secure) {
_secureReady.fire({
entry.itemId,
@@ -916,4 +951,54 @@ void Uploader::partFailed(const MTP::Error &error, mtpRequestId requestId) {
failed(request.itemId);
}
void Uploader::uploadVideoCover(
UploadedMedia &&video,
std::shared_ptr<FilePrepareResult> videoCover) {
const auto coverId = FullMsgId(
videoCover->to.peer,
session().data().nextLocalMessageId());
_videoIdToCoverId.emplace(video.fullId, coverId);
_videoWaitingCover.emplace(coverId, std::move(video));
upload(coverId, videoCover);
}
void Uploader::uploadCoverAsPhoto(
FullMsgId videoId,
UploadedMedia &&cover) {
const auto coverId = cover.fullId;
_api->request(MTPmessages_UploadMedia(
MTP_flags(0),
MTPstring(), // business_connection_id
session().data().peer(videoId.peer)->input,
MTP_inputMediaUploadedPhoto(
MTP_flags(0),
cover.info.file,
MTP_vector<MTPInputDocument>(0),
MTP_int(0))
)).done([=](const MTPMessageMedia &result) {
result.match([&](const MTPDmessageMediaPhoto &data) {
const auto photo = data.vphoto();
if (!photo || photo->type() != mtpc_photo) {
failed(coverId);
return;
}
const auto &fields = photo->c_photo();
if (const auto coverId = _videoIdToCoverId.take(videoId)) {
if (auto video = _videoWaitingCover.take(*coverId)) {
video->info.videoCover = MTP_inputPhoto(
fields.vid(),
fields.vaccess_hash(),
fields.vfile_reference());
_documentReady.fire(std::move(*video));
}
}
}, [&](const auto &) {
failed(coverId);
});
}).fail([=] {
failed(coverId);
}).send();
}
} // namespace Storage