2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

Revert "Remove SendMediaReady legacy helper."

This reverts commit 91f8989f70.
This commit is contained in:
John Preston
2024-04-04 18:36:07 +04:00
parent f6f8eefaa0
commit 09f07a7a9d
16 changed files with 354 additions and 183 deletions

View File

@@ -59,15 +59,22 @@ constexpr auto kKillSessionTimeout = 15 * crl::time(1000);
} // namespace
struct Uploader::File {
explicit File(const std::shared_ptr<FilePrepareResult> &file);
File(const SendMediaReady &media);
File(const std::shared_ptr<FileLoadResult> &file);
void setDocSize(int64 size);
bool setPartSize(uint32 partSize);
std::shared_ptr<FilePrepareResult> file;
std::shared_ptr<FileLoadResult> file;
SendMediaReady media;
int32 partsCount = 0;
mutable int64 fileSentSize = 0;
uint64 id() const;
SendMediaType type() const;
uint64 thumbId() const;
const QString &filename() const;
HashMd5 md5Hash;
std::unique_ptr<QFile> docFile;
@@ -78,15 +85,27 @@ struct Uploader::File {
};
Uploader::File::File(const std::shared_ptr<FilePrepareResult> &file)
Uploader::File::File(const SendMediaReady &media) : media(media) {
partsCount = media.parts.size();
if (type() == SendMediaType::File
|| type() == SendMediaType::ThemeFile
|| 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 = (file->type == SendMediaType::Photo
|| file->type == SendMediaType::Secure)
partsCount = (type() == SendMediaType::Photo
|| type() == SendMediaType::Secure)
? file->fileparts.size()
: file->thumbparts.size();
if (file->type == SendMediaType::File
|| file->type == SendMediaType::ThemeFile
|| file->type == SendMediaType::Audio) {
if (type() == SendMediaType::File
|| type() == SendMediaType::ThemeFile
|| type() == SendMediaType::Audio) {
setDocSize(file->filesize);
} else {
docSize = docPartSize = docPartsCount = 0;
@@ -115,6 +134,22 @@ bool Uploader::File::setPartSize(uint32 partSize) {
return (docPartsCount <= kDocumentMaxPartsCountDefault);
}
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;
}
Uploader::Uploader(not_null<ApiWrap*> api)
: _api(api)
, _nextTimer([=] { sendNext(); })
@@ -247,9 +282,39 @@ Main::Session &Uploader::session() const {
return _api->session();
}
void Uploader::uploadMedia(
const FullMsgId &msgId,
const SendMediaReady &media) {
if (media.type == SendMediaType::Photo) {
session().data().processPhoto(media.photo, media.photoThumbs);
} else if (media.type == SendMediaType::File
|| media.type == SendMediaType::ThemeFile
|| media.type == SendMediaType::Audio) {
const auto document = media.photoThumbs.empty()
? session().data().processDocument(media.document)
: session().data().processDocument(
media.document,
Images::FromImageInMemory(
media.photoThumbs.front().second.image,
"JPG",
media.photoThumbs.front().second.bytes));
if (!media.data.isEmpty()) {
document->setDataAndCache(media.data);
if (media.type == SendMediaType::ThemeFile) {
document->checkWallPaperProperties();
}
}
if (!media.file.isEmpty()) {
document->setLocation(Core::FileLocation(media.file));
}
}
queue.emplace(msgId, File(media));
sendNext();
}
void Uploader::upload(
const FullMsgId &msgId,
const std::shared_ptr<FilePrepareResult> &file) {
const std::shared_ptr<FileLoadResult> &file) {
if (file->type == SendMediaType::Photo) {
const auto photo = session().data().processPhoto(
file->photo,
@@ -318,13 +383,13 @@ void Uploader::currentFailed() {
}
void Uploader::notifyFailed(FullMsgId id, const File &file) {
const auto type = file.file->type;
const auto type = file.type();
if (type == SendMediaType::Photo) {
_photoFailed.fire_copy(id);
} else if (type == SendMediaType::File
|| type == SendMediaType::ThemeFile
|| type == SendMediaType::Audio) {
const auto document = session().data().document(file.file->id);
const auto document = session().data().document(file.id());
if (document->uploading()) {
document->status = FileUploadFailed;
}
@@ -374,14 +439,18 @@ void Uploader::sendNext() {
}
}
auto &parts = (uploadingData.file->type == SendMediaType::Photo
|| uploadingData.file->type == SendMediaType::Secure)
? uploadingData.file->fileparts
: uploadingData.file->thumbparts;
const auto partsOfId = (uploadingData.file->type == SendMediaType::Photo
|| uploadingData.file->type == SendMediaType::Secure)
? uploadingData.file->id
: uploadingData.file->thumbId;
auto &parts = uploadingData.file
? ((uploadingData.type() == SendMediaType::Photo
|| uploadingData.type() == SendMediaType::Secure)
? uploadingData.file->fileparts
: uploadingData.file->thumbparts)
: uploadingData.media.parts;
const auto partsOfId = uploadingData.file
? ((uploadingData.type() == SendMediaType::Photo
|| uploadingData.type() == SendMediaType::Secure)
? uploadingData.file->id
: uploadingData.file->thumbId)
: uploadingData.media.thumbId;
if (parts.isEmpty()) {
if (uploadingData.docSentParts >= uploadingData.docPartsCount) {
if (requestsSent.empty() && docRequestsSent.empty()) {
@@ -393,17 +462,19 @@ void Uploader::sendNext() {
const auto attachedStickers = uploadingData.file
? uploadingData.file->attachedStickers
: std::vector<MTPInputDocument>();
if (uploadingData.file->type == SendMediaType::Photo) {
auto photoFilename = uploadingData.file->filename;
if (uploadingData.type() == SendMediaType::Photo) {
auto photoFilename = uploadingData.filename();
if (!photoFilename.endsWith(u".jpg"_q, 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 += u".jpg"_q;
}
const auto md5 = uploadingData.file->filemd5;
const auto md5 = uploadingData.file
? uploadingData.file->filemd5
: uploadingData.media.jpeg_md5;
const auto file = MTP_inputFile(
MTP_long(uploadingData.file->id),
MTP_long(uploadingData.id()),
MTP_int(uploadingData.partsCount),
MTP_string(photoFilename),
MTP_bytes(md5));
@@ -416,30 +487,34 @@ void Uploader::sendNext() {
.options = options,
.edit = edit,
});
} else if (uploadingData.file->type == SendMediaType::File
|| uploadingData.file->type == SendMediaType::ThemeFile
|| uploadingData.file->type == SendMediaType::Audio) {
} else if (uploadingData.type() == SendMediaType::File
|| uploadingData.type() == SendMediaType::ThemeFile
|| uploadingData.type() == SendMediaType::Audio) {
QByteArray docMd5(32, Qt::Uninitialized);
hashMd5Hex(uploadingData.md5Hash.result(), docMd5.data());
const auto file = (uploadingData.docSize > kUseBigFilesFrom)
? MTP_inputFileBig(
MTP_long(uploadingData.file->id),
MTP_long(uploadingData.id()),
MTP_int(uploadingData.docPartsCount),
MTP_string(uploadingData.file->filename))
MTP_string(uploadingData.filename()))
: MTP_inputFile(
MTP_long(uploadingData.file->id),
MTP_long(uploadingData.id()),
MTP_int(uploadingData.docPartsCount),
MTP_string(uploadingData.file->filename),
MTP_string(uploadingData.filename()),
MTP_bytes(docMd5));
const auto thumb = [&]() -> std::optional<MTPInputFile> {
if (!uploadingData.partsCount) {
return std::nullopt;
}
const auto thumbFilename = uploadingData.file->thumbname;
const auto thumbMd5 = uploadingData.file->thumbmd5;
const auto thumbFilename = uploadingData.file
? uploadingData.file->thumbname
: (u"thumb."_q + uploadingData.media.thumbExt);
const auto thumbMd5 = uploadingData.file
? uploadingData.file->thumbmd5
: uploadingData.media.jpeg_md5;
return MTP_inputFile(
MTP_long(uploadingData.file->thumbId),
MTP_long(uploadingData.thumbId()),
MTP_int(uploadingData.partsCount),
MTP_string(thumbFilename),
MTP_bytes(thumbMd5));
@@ -454,10 +529,10 @@ void Uploader::sendNext() {
.options = options,
.edit = edit,
});
} else if (uploadingData.file->type == SendMediaType::Secure) {
} else if (uploadingData.type() == SendMediaType::Secure) {
_secureReady.fire({
uploadingId,
uploadingData.file->id,
uploadingData.id(),
uploadingData.partsCount });
}
queue.erase(uploadingId);
@@ -467,11 +542,15 @@ void Uploader::sendNext() {
return;
}
auto &content = uploadingData.file->content;
auto &content = uploadingData.file
? uploadingData.file->content
: uploadingData.media.data;
QByteArray toSend;
if (content.isEmpty()) {
if (!uploadingData.docFile) {
const auto filepath = uploadingData.file->filepath;
const auto filepath = uploadingData.file
? uploadingData.file->filepath
: uploadingData.media.file;
uploadingData.docFile = std::make_unique<QFile>(filepath);
if (!uploadingData.docFile->open(QIODevice::ReadOnly)) {
currentFailed();
@@ -486,9 +565,9 @@ void Uploader::sendNext() {
const auto offset = uploadingData.docSentParts
* uploadingData.docPartSize;
toSend = content.mid(offset, uploadingData.docPartSize);
if ((uploadingData.file->type == SendMediaType::File
|| uploadingData.file->type == SendMediaType::ThemeFile
|| uploadingData.file->type == SendMediaType::Audio)
if ((uploadingData.type() == SendMediaType::File
|| uploadingData.type() == SendMediaType::ThemeFile
|| uploadingData.type() == SendMediaType::Audio)
&& uploadingData.docSentParts <= kUseBigFilesFrom) {
uploadingData.md5Hash.feed(toSend.constData(), toSend.size());
}
@@ -502,7 +581,7 @@ void Uploader::sendNext() {
mtpRequestId requestId;
if (uploadingData.docSize > kUseBigFilesFrom) {
requestId = _api->request(MTPupload_SaveBigFilePart(
MTP_long(uploadingData.file->id),
MTP_long(uploadingData.id()),
MTP_int(uploadingData.docSentParts),
MTP_int(uploadingData.docPartsCount),
MTP_bytes(toSend)
@@ -513,7 +592,7 @@ void Uploader::sendNext() {
}).toDC(MTP::uploadDcId(todc)).send();
} else {
requestId = _api->request(MTPupload_SaveFilePart(
MTP_long(uploadingData.file->id),
MTP_long(uploadingData.id()),
MTP_int(uploadingData.docSentParts),
MTP_bytes(toSend)
)).done([=](const MTPBool &result, mtpRequestId requestId) {
@@ -644,18 +723,18 @@ void Uploader::partLoaded(const MTPBool &result, mtpRequestId requestId) {
}
sentSize -= sentPartSize;
sentSizes[dc] -= sentPartSize;
if (file.file->type == SendMediaType::Photo) {
if (file.type() == SendMediaType::Photo) {
file.fileSentSize += sentPartSize;
const auto photo = session().data().photo(file.file->id);
const auto photo = session().data().photo(file.id());
if (photo->uploading() && file.file) {
photo->uploadingData->size = file.file->partssize;
photo->uploadingData->offset = file.fileSentSize;
}
_photoProgress.fire_copy(fullId);
} else if (file.file->type == SendMediaType::File
|| file.file->type == SendMediaType::ThemeFile
|| file.file->type == SendMediaType::Audio) {
const auto document = session().data().document(file.file->id);
} else if (file.type() == SendMediaType::File
|| file.type() == SendMediaType::ThemeFile
|| file.type() == SendMediaType::Audio) {
const auto document = session().data().document(file.id());
if (document->uploading()) {
const auto doneParts = file.docSentParts
- int(docRequestsSent.size());
@@ -664,7 +743,7 @@ void Uploader::partLoaded(const MTPBool &result, mtpRequestId requestId) {
doneParts * file.docPartSize);
}
_documentProgress.fire_copy(fullId);
} else if (file.file->type == SendMediaType::Secure) {
} else if (file.type() == SendMediaType::Secure) {
file.fileSentSize += sentPartSize;
_secureProgress.fire_copy({
fullId,

View File

@@ -12,7 +12,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mtproto/facade.h"
class ApiWrap;
struct FilePrepareResult;
struct FileLoadResult;
struct SendMediaReady;
namespace Api {
enum class SendProgressType;
@@ -57,9 +58,10 @@ public:
return uploadingId;
}
void uploadMedia(const FullMsgId &msgId, const SendMediaReady &image);
void upload(
const FullMsgId &msgId,
const std::shared_ptr<FilePrepareResult> &file);
const std::shared_ptr<FileLoadResult> &file);
void cancel(const FullMsgId &msgId);
void pause(const FullMsgId &msgId);

View File

@@ -222,6 +222,42 @@ int PhotoSideLimit() {
return PhotoSideLimit(SendLargePhotos.value());
}
SendMediaReady::SendMediaReady(
SendMediaType type,
const QString &file,
const QString &filename,
int64 filesize,
const QByteArray &data,
const uint64 &id,
const uint64 &thumbId,
const QString &thumbExt,
const PeerId &peer,
const MTPPhoto &photo,
const PreparedPhotoThumbs &photoThumbs,
const MTPDocument &document,
const QByteArray &jpeg)
: type(type)
, file(file)
, filename(filename)
, filesize(filesize)
, data(data)
, thumbExt(thumbExt)
, id(id)
, thumbId(thumbId)
, peer(peer)
, photo(photo)
, document(document)
, photoThumbs(photoThumbs) {
if (!jpeg.isEmpty()) {
int32 size = jpeg.size();
for (int32 i = 0, part = 0; i < size; i += kPhotoUploadPartSize, ++part) {
parts.insert(part, jpeg.mid(i, kPhotoUploadPartSize));
}
jpeg_md5.resize(32);
hashMd5Hex(jpeg.constData(), jpeg.size(), jpeg_md5.data());
}
}
TaskQueue::TaskQueue(crl::time stopTimeoutMs) {
if (stopTimeoutMs > 0) {
_stopTimer = new QTimer(this);
@@ -419,17 +455,22 @@ SendingAlbum::Item::Item(TaskId taskId)
: taskId(taskId) {
}
FilePrepareResult::FilePrepareResult(FilePrepareDescriptor &&descriptor)
: taskId(descriptor.taskId)
, id(descriptor.id)
, to(std::move(descriptor.to))
, album(std::move(descriptor.album))
, type(descriptor.type)
, caption(std::move(descriptor.caption))
, spoiler(descriptor.spoiler) {
FileLoadResult::FileLoadResult(
TaskId taskId,
uint64 id,
const FileLoadTo &to,
const TextWithTags &caption,
bool spoiler,
std::shared_ptr<SendingAlbum> album)
: taskId(taskId)
, id(id)
, to(to)
, album(std::move(album))
, caption(caption)
, spoiler(spoiler) {
}
void FilePrepareResult::setFileData(const QByteArray &filedata) {
void FileLoadResult::setFileData(const QByteArray &filedata) {
if (filedata.isEmpty()) {
partssize = 0;
} else {
@@ -442,7 +483,7 @@ void FilePrepareResult::setFileData(const QByteArray &filedata) {
}
}
void FilePrepareResult::setThumbData(const QByteArray &thumbdata) {
void FileLoadResult::setThumbData(const QByteArray &thumbdata) {
if (!thumbdata.isEmpty()) {
thumbbytes = thumbdata;
int32 size = thumbdata.size();
@@ -454,11 +495,6 @@ void FilePrepareResult::setThumbData(const QByteArray &thumbdata) {
}
}
std::shared_ptr<FilePrepareResult> MakePreparedFile(
FilePrepareDescriptor &&descriptor) {
return std::make_shared<FilePrepareResult>(std::move(descriptor));
}
FileLoadTask::FileLoadTask(
not_null<Main::Session*> session,
const QString &filepath,
@@ -674,14 +710,13 @@ bool FileLoadTask::FillImageInformation(
}
void FileLoadTask::process(Args &&args) {
_result = MakePreparedFile({
.taskId = id(),
.id = _id,
.to = _to,
.caption = _caption,
.spoiler = _spoiler,
.album = _album,
});
_result = std::make_shared<FileLoadResult>(
id(),
_id,
_to,
_caption,
_spoiler,
_album);
QString filename, filemime;
qint64 filesize = 0;
@@ -1027,7 +1062,7 @@ void FileLoadTask::finish() {
}
}
FilePrepareResult *FileLoadTask::peekResult() const {
FileLoadResult *FileLoadTask::peekResult() const {
return _result.get();
}

View File

@@ -36,8 +36,43 @@ enum class SendMediaType {
Secure,
};
using UploadFileParts = QMap<int, QByteArray>;
struct SendMediaReady {
SendMediaReady() = default; // temp
SendMediaReady(
SendMediaType type,
const QString &file,
const QString &filename,
int64 filesize,
const QByteArray &data,
const uint64 &id,
const uint64 &thumbId,
const QString &thumbExt,
const PeerId &peer,
const MTPPhoto &photo,
const PreparedPhotoThumbs &photoThumbs,
const MTPDocument &document,
const QByteArray &jpeg);
SendMediaType type;
QString file, filename;
int64 filesize = 0;
QByteArray data;
QString thumbExt;
uint64 id, thumbId; // id always file-id of media, thumbId is file-id of thumb ( == id for photos)
PeerId peer;
MTPPhoto photo;
MTPDocument document;
PreparedPhotoThumbs photoThumbs;
UploadFileParts parts;
QByteArray jpeg_md5;
QString caption;
};
using TaskId = void*; // no interface, just id
inline constexpr auto kEmptyTaskId = TaskId();
class Task {
public:
@@ -109,7 +144,7 @@ struct SendingAlbum {
struct Item {
explicit Item(TaskId taskId);
TaskId taskId = kEmptyTaskId;
TaskId taskId;
uint64 randomId = 0;
FullMsgId msgId;
std::optional<MTPInputSingleMedia> media;
@@ -147,21 +182,17 @@ struct FileLoadTo {
MsgId replaceMediaOf;
};
using UploadFileParts = QMap<int, QByteArray>;
struct FilePrepareDescriptor {
TaskId taskId = kEmptyTaskId;
base::required<uint64> id;
SendMediaType type = SendMediaType::File;
FileLoadTo to = { PeerId(), Api::SendOptions(), FullReplyTo(), MsgId() };
TextWithTags caption;
bool spoiler = false;
std::shared_ptr<SendingAlbum> album;
};
struct FilePrepareResult {
explicit FilePrepareResult(FilePrepareDescriptor &&descriptor);
struct FileLoadResult {
FileLoadResult(
TaskId taskId,
uint64 id,
const FileLoadTo &to,
const TextWithTags &caption,
bool spoiler,
std::shared_ptr<SendingAlbum> album);
TaskId taskId = kEmptyTaskId;
uint64 id = 0;
TaskId taskId;
uint64 id;
FileLoadTo to;
std::shared_ptr<SendingAlbum> album;
SendMediaType type = SendMediaType::File;
@@ -185,8 +216,8 @@ struct FilePrepareResult {
QImage goodThumbnail;
QByteArray goodThumbnailBytes;
MTPPhoto photo = MTP_photoEmpty(MTP_long(0));
MTPDocument document = MTP_documentEmpty(MTP_long(0));
MTPPhoto photo;
MTPDocument document;
PreparedPhotoThumbs photoThumbs;
TextWithTags caption;
@@ -199,9 +230,6 @@ struct FilePrepareResult {
};
[[nodiscard]] std::shared_ptr<FilePrepareResult> MakePreparedFile(
FilePrepareDescriptor &&descriptor);
class FileLoadTask final : public Task {
public:
static std::unique_ptr<Ui::PreparedFileInformation> ReadMediaInformation(
@@ -248,7 +276,7 @@ public:
}
void finish() override;
FilePrepareResult *peekResult() const;
FileLoadResult *peekResult() const;
private:
static bool CheckForSong(
@@ -284,6 +312,6 @@ private:
TextWithTags _caption;
bool _spoiler = false;
std::shared_ptr<FilePrepareResult> _result;
std::shared_ptr<FileLoadResult> _result;
};