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/localimageloader.h"
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_document.h"
|
2017-02-28 14:05:30 +00:00
|
|
|
#include "core/file_utilities.h"
|
|
|
|
#include "media/media_audio.h"
|
2016-11-28 15:45:07 +00:00
|
|
|
#include "boxes/send_files_box.h"
|
2016-06-24 10:37:29 +00:00
|
|
|
#include "media/media_clip_reader.h"
|
2015-10-27 02:39:02 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2017-08-15 15:59:40 +00:00
|
|
|
#include "storage/file_download.h"
|
2017-12-19 16:57:42 +00:00
|
|
|
#include "storage/storage_media_prepare.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
using Storage::ValidateThumbDimensions;
|
2017-03-09 09:47:45 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
TaskQueue::TaskQueue(TimeMs stopTimeoutMs) {
|
2015-09-29 18:44:31 +00:00
|
|
|
if (stopTimeoutMs > 0) {
|
|
|
|
_stopTimer = new QTimer(this);
|
|
|
|
connect(_stopTimer, SIGNAL(timeout()), this, SLOT(stop()));
|
|
|
|
_stopTimer->setSingleShot(true);
|
2017-12-19 16:57:42 +00:00
|
|
|
_stopTimer->setInterval(int(stopTimeoutMs));
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
TaskId TaskQueue::addTask(std::unique_ptr<Task> &&task) {
|
|
|
|
const auto result = task->id();
|
2015-09-29 18:44:31 +00:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&_tasksToProcessMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
_tasksToProcess.push_back(std::move(task));
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
2015-10-28 02:41:13 +00:00
|
|
|
|
|
|
|
wakeThread();
|
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
return result;
|
2015-10-28 02:41:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 10:38:14 +00:00
|
|
|
void TaskQueue::addTasks(std::vector<std::unique_ptr<Task>> &&tasks) {
|
2015-10-28 02:41:13 +00:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&_tasksToProcessMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
for (auto &task : tasks) {
|
|
|
|
_tasksToProcess.push_back(std::move(task));
|
|
|
|
}
|
2015-10-28 02:41:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wakeThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskQueue::wakeThread() {
|
2015-09-29 18:44:31 +00:00
|
|
|
if (!_thread) {
|
|
|
|
_thread = new QThread();
|
|
|
|
|
|
|
|
_worker = new TaskQueueWorker(this);
|
|
|
|
_worker->moveToThread(_thread);
|
|
|
|
|
|
|
|
connect(this, SIGNAL(taskAdded()), _worker, SLOT(onTaskAdded()));
|
|
|
|
connect(_worker, SIGNAL(taskProcessed()), this, SLOT(onTaskProcessed()));
|
|
|
|
|
|
|
|
_thread->start();
|
|
|
|
}
|
|
|
|
if (_stopTimer) _stopTimer->stop();
|
|
|
|
emit taskAdded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskQueue::cancelTask(TaskId id) {
|
2017-12-18 10:38:14 +00:00
|
|
|
const auto removeFrom = [&](std::deque<std::unique_ptr<Task>> &queue) {
|
|
|
|
const auto proj = [](const std::unique_ptr<Task> &task) {
|
|
|
|
return task->id();
|
|
|
|
};
|
|
|
|
auto i = ranges::find(queue, id, proj);
|
|
|
|
if (i != queue.end()) {
|
|
|
|
queue.erase(i);
|
|
|
|
}
|
|
|
|
};
|
2015-11-23 16:05:30 +00:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&_tasksToProcessMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
removeFrom(_tasksToProcess);
|
|
|
|
if (_taskInProcessId == id) {
|
|
|
|
_taskInProcessId = TaskId();
|
2015-11-23 16:05:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
QMutexLocker lock(&_tasksToFinishMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
removeFrom(_tasksToFinish);
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TaskQueue::onTaskProcessed() {
|
|
|
|
do {
|
2017-12-18 10:38:14 +00:00
|
|
|
auto task = std::unique_ptr<Task>();
|
2015-09-29 18:44:31 +00:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&_tasksToFinishMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
if (_tasksToFinish.empty()) break;
|
|
|
|
task = std::move(_tasksToFinish.front());
|
2015-09-29 18:44:31 +00:00
|
|
|
_tasksToFinish.pop_front();
|
|
|
|
}
|
|
|
|
task->finish();
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
if (_stopTimer) {
|
|
|
|
QMutexLocker lock(&_tasksToProcessMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
if (_tasksToProcess.empty() && !_taskInProcessId) {
|
2015-09-29 18:44:31 +00:00
|
|
|
_stopTimer->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskQueue::stop() {
|
|
|
|
if (_thread) {
|
|
|
|
_thread->requestInterruption();
|
|
|
|
_thread->quit();
|
2016-03-01 02:36:23 +00:00
|
|
|
DEBUG_LOG(("Waiting for taskThread to finish"));
|
2015-09-29 18:44:31 +00:00
|
|
|
_thread->wait();
|
|
|
|
delete _worker;
|
|
|
|
delete _thread;
|
|
|
|
_worker = 0;
|
|
|
|
_thread = 0;
|
|
|
|
}
|
|
|
|
_tasksToProcess.clear();
|
|
|
|
_tasksToFinish.clear();
|
2017-12-18 10:38:14 +00:00
|
|
|
_taskInProcessId = TaskId();
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TaskQueue::~TaskQueue() {
|
|
|
|
stop();
|
|
|
|
delete _stopTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TaskQueueWorker::onTaskAdded() {
|
|
|
|
if (_inTaskAdded) return;
|
|
|
|
_inTaskAdded = true;
|
2016-01-05 04:28:06 +00:00
|
|
|
|
2015-09-29 18:44:31 +00:00
|
|
|
bool someTasksLeft = false;
|
|
|
|
do {
|
2017-12-18 10:38:14 +00:00
|
|
|
auto task = std::unique_ptr<Task>();
|
2015-09-29 18:44:31 +00:00
|
|
|
{
|
|
|
|
QMutexLocker lock(&_queue->_tasksToProcessMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
if (!_queue->_tasksToProcess.empty()) {
|
|
|
|
task = std::move(_queue->_tasksToProcess.front());
|
|
|
|
_queue->_tasksToProcess.pop_front();
|
|
|
|
_queue->_taskInProcessId = task->id();
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task) {
|
|
|
|
task->process();
|
|
|
|
bool emitTaskProcessed = false;
|
|
|
|
{
|
|
|
|
QMutexLocker lockToProcess(&_queue->_tasksToProcessMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
if (_queue->_taskInProcessId == task->id()) {
|
|
|
|
_queue->_taskInProcessId = TaskId();
|
|
|
|
someTasksLeft = !_queue->_tasksToProcess.empty();
|
2015-09-29 18:44:31 +00:00
|
|
|
|
|
|
|
QMutexLocker lockToFinish(&_queue->_tasksToFinishMutex);
|
2017-12-18 10:38:14 +00:00
|
|
|
emitTaskProcessed = _queue->_tasksToFinish.empty();
|
|
|
|
_queue->_tasksToFinish.push_back(std::move(task));
|
2015-09-29 18:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (emitTaskProcessed) {
|
|
|
|
emit taskProcessed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
} while (someTasksLeft && !thread()->isInterruptionRequested());
|
|
|
|
|
|
|
|
_inTaskAdded = false;
|
|
|
|
}
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
SendingAlbum::SendingAlbum() : groupId(rand_value<uint64>()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLoadResult::FileLoadResult(
|
|
|
|
TaskId taskId,
|
|
|
|
uint64 id,
|
|
|
|
const FileLoadTo &to,
|
|
|
|
const QString &caption,
|
|
|
|
std::shared_ptr<SendingAlbum> album)
|
|
|
|
: taskId(taskId)
|
|
|
|
, id(id)
|
|
|
|
, to(to)
|
2017-12-26 15:05:17 +00:00
|
|
|
, album(std::move(album))
|
|
|
|
, caption(caption) {
|
2017-12-19 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileLoadTask::FileLoadTask(
|
|
|
|
const QString &filepath,
|
2017-12-21 16:15:37 +00:00
|
|
|
const QByteArray &content,
|
2017-12-19 16:57:42 +00:00
|
|
|
std::unique_ptr<FileMediaInformation> information,
|
|
|
|
SendMediaType type,
|
|
|
|
const FileLoadTo &to,
|
|
|
|
const QString &caption,
|
|
|
|
std::shared_ptr<SendingAlbum> album)
|
|
|
|
: _id(rand_value<uint64>())
|
2015-10-27 02:39:02 +00:00
|
|
|
, _to(to)
|
2017-12-19 16:57:42 +00:00
|
|
|
, _album(std::move(album))
|
2015-10-27 02:39:02 +00:00
|
|
|
, _filepath(filepath)
|
|
|
|
, _content(content)
|
2017-12-21 16:15:37 +00:00
|
|
|
, _information(std::move(information))
|
2015-10-28 02:41:13 +00:00
|
|
|
, _type(type)
|
2016-11-28 15:45:07 +00:00
|
|
|
, _caption(caption) {
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
FileLoadTask::FileLoadTask(
|
|
|
|
const QByteArray &voice,
|
|
|
|
int32 duration,
|
|
|
|
const VoiceWaveform &waveform,
|
|
|
|
const FileLoadTo &to,
|
|
|
|
const QString &caption)
|
|
|
|
: _id(rand_value<uint64>())
|
2015-10-27 02:39:02 +00:00
|
|
|
, _to(to)
|
2016-02-12 16:35:06 +00:00
|
|
|
, _content(voice)
|
2015-10-27 02:39:02 +00:00
|
|
|
, _duration(duration)
|
2016-02-12 16:35:06 +00:00
|
|
|
, _waveform(waveform)
|
2016-11-28 15:45:07 +00:00
|
|
|
, _type(SendMediaType::Audio)
|
|
|
|
, _caption(caption) {
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
std::unique_ptr<FileMediaInformation> FileLoadTask::ReadMediaInformation(
|
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
|
|
|
const QString &filemime) {
|
|
|
|
auto result = std::make_unique<FileMediaInformation>();
|
2017-03-10 14:14:10 +00:00
|
|
|
result->filemime = filemime;
|
|
|
|
|
|
|
|
if (CheckForSong(filepath, content, result)) {
|
|
|
|
return result;
|
|
|
|
} else if (CheckForVideo(filepath, content, result)) {
|
|
|
|
return result;
|
|
|
|
} else if (CheckForImage(filepath, content, result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Mimes, typename Extensions>
|
2017-12-19 16:57:42 +00:00
|
|
|
bool FileLoadTask::CheckMimeOrExtensions(
|
|
|
|
const QString &filepath,
|
|
|
|
const QString &filemime,
|
|
|
|
Mimes &mimes,
|
|
|
|
Extensions &extensions) {
|
2017-03-10 14:14:10 +00:00
|
|
|
if (std::find(std::begin(mimes), std::end(mimes), filemime) != std::end(mimes)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (std::find_if(std::begin(extensions), std::end(extensions), [&filepath](auto &extension) {
|
|
|
|
return filepath.endsWith(extension, Qt::CaseInsensitive);
|
|
|
|
}) != std::end(extensions)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
bool FileLoadTask::CheckForSong(
|
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
|
|
|
std::unique_ptr<FileMediaInformation> &result) {
|
2017-03-10 14:14:10 +00:00
|
|
|
static const auto mimes = {
|
|
|
|
qstr("audio/mp3"),
|
|
|
|
qstr("audio/m4a"),
|
|
|
|
qstr("audio/aac"),
|
|
|
|
qstr("audio/ogg"),
|
|
|
|
qstr("audio/flac"),
|
|
|
|
};
|
|
|
|
static const auto extensions = {
|
|
|
|
qstr(".mp3"),
|
|
|
|
qstr(".m4a"),
|
|
|
|
qstr(".aac"),
|
|
|
|
qstr(".ogg"),
|
|
|
|
qstr(".flac"),
|
|
|
|
};
|
|
|
|
if (!CheckMimeOrExtensions(filepath, result->filemime, mimes, extensions)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-12 12:43:39 +00:00
|
|
|
|
2017-03-10 14:14:10 +00:00
|
|
|
auto media = Media::Player::PrepareForSending(filepath, content);
|
2017-03-27 12:50:40 +00:00
|
|
|
if (media.duration < 0) {
|
2017-03-10 14:14:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!ValidateThumbDimensions(media.cover.width(), media.cover.height())) {
|
|
|
|
media.cover = QImage();
|
|
|
|
}
|
|
|
|
result->media = std::move(media);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
bool FileLoadTask::CheckForVideo(
|
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
|
|
|
std::unique_ptr<FileMediaInformation> &result) {
|
2017-03-10 14:14:10 +00:00
|
|
|
static const auto mimes = {
|
|
|
|
qstr("video/mp4"),
|
|
|
|
qstr("video/quicktime"),
|
|
|
|
};
|
|
|
|
static const auto extensions = {
|
|
|
|
qstr(".mp4"),
|
|
|
|
qstr(".mov"),
|
|
|
|
};
|
|
|
|
if (!CheckMimeOrExtensions(filepath, result->filemime, mimes, extensions)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto media = Media::Clip::PrepareForSending(filepath, content);
|
2017-03-27 12:50:40 +00:00
|
|
|
if (media.duration < 0) {
|
2017-03-10 14:14:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto coverWidth = media.thumbnail.width();
|
|
|
|
auto coverHeight = media.thumbnail.height();
|
|
|
|
if (!ValidateThumbDimensions(coverWidth, coverHeight)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filepath.endsWith(qstr(".mp4"), Qt::CaseInsensitive)) {
|
|
|
|
result->filemime = qstr("video/mp4");
|
|
|
|
}
|
|
|
|
result->media = std::move(media);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
bool FileLoadTask::CheckForImage(
|
|
|
|
const QString &filepath,
|
|
|
|
const QByteArray &content,
|
|
|
|
std::unique_ptr<FileMediaInformation> &result) {
|
2017-03-10 14:14:10 +00:00
|
|
|
auto animated = false;
|
|
|
|
auto image = ([&filepath, &content, &animated] {
|
|
|
|
if (!content.isEmpty()) {
|
|
|
|
return App::readImage(content, nullptr, false, &animated);
|
|
|
|
} else if (!filepath.isEmpty()) {
|
|
|
|
return App::readImage(filepath, nullptr, false, &animated);
|
|
|
|
}
|
|
|
|
return QImage();
|
|
|
|
})();
|
2017-12-21 16:15:37 +00:00
|
|
|
return FillImageInformation(std::move(image), animated, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileLoadTask::FillImageInformation(
|
|
|
|
QImage &&image,
|
|
|
|
bool animated,
|
|
|
|
std::unique_ptr<FileMediaInformation> &result) {
|
|
|
|
Expects(result != nullptr);
|
2017-03-10 14:14:10 +00:00
|
|
|
|
|
|
|
if (image.isNull()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
auto media = FileMediaInformation::Image();
|
2017-03-10 14:14:10 +00:00
|
|
|
media.data = std::move(image);
|
|
|
|
media.animated = animated;
|
|
|
|
result->media = media;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-27 02:39:02 +00:00
|
|
|
void FileLoadTask::process() {
|
2017-03-10 15:45:22 +00:00
|
|
|
const auto stickerMime = qsl("image/webp");
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2017-12-19 16:57:42 +00:00
|
|
|
_result = std::make_shared<FileLoadResult>(
|
|
|
|
id(),
|
|
|
|
_id,
|
|
|
|
_to,
|
|
|
|
_caption,
|
|
|
|
_album);
|
2015-10-27 02:39:02 +00:00
|
|
|
|
|
|
|
QString filename, filemime;
|
|
|
|
qint64 filesize = 0;
|
|
|
|
QByteArray filedata;
|
|
|
|
|
|
|
|
uint64 thumbId = 0;
|
2017-03-10 15:45:22 +00:00
|
|
|
auto thumbname = qsl("thumb.jpg");
|
2015-10-27 02:39:02 +00:00
|
|
|
QByteArray thumbdata;
|
|
|
|
|
2017-03-10 14:14:10 +00:00
|
|
|
auto isAnimation = false;
|
|
|
|
auto isSong = false;
|
|
|
|
auto isVideo = false;
|
|
|
|
auto isVoice = (_type == SendMediaType::Audio);
|
|
|
|
|
2017-12-21 16:15:37 +00:00
|
|
|
auto fullimage = QImage();
|
2017-01-13 11:58:41 +00:00
|
|
|
auto info = _filepath.isEmpty() ? QFileInfo() : QFileInfo(_filepath);
|
|
|
|
if (info.exists()) {
|
2015-11-02 22:36:26 +00:00
|
|
|
if (info.isDir()) {
|
|
|
|
_result->filesize = -1;
|
|
|
|
return;
|
|
|
|
}
|
2017-03-10 14:14:10 +00:00
|
|
|
|
|
|
|
// Voice sending is supported only from memory for now.
|
|
|
|
// Because for voice we force mime type and don't read MediaInformation.
|
|
|
|
// For a real file we always read mime type and read MediaInformation.
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!isVoice);
|
2017-03-10 14:14:10 +00:00
|
|
|
|
2015-10-27 02:39:02 +00:00
|
|
|
filesize = info.size();
|
|
|
|
filename = info.fileName();
|
2017-03-10 14:14:10 +00:00
|
|
|
if (!_information) {
|
|
|
|
_information = readMediaInformation(mimeTypeForFile(info).name());
|
|
|
|
}
|
|
|
|
filemime = _information->filemime;
|
2017-12-19 16:57:42 +00:00
|
|
|
if (auto image = base::get_if<FileMediaInformation::Image>(
|
|
|
|
&_information->media)) {
|
2017-03-10 14:14:10 +00:00
|
|
|
fullimage = base::take(image->data);
|
|
|
|
if (auto opaque = (filemime != stickerMime)) {
|
|
|
|
fullimage = Images::prepareOpaque(std::move(fullimage));
|
|
|
|
}
|
|
|
|
isAnimation = image->animated;
|
|
|
|
}
|
2015-10-27 02:39:02 +00:00
|
|
|
} else if (!_content.isEmpty()) {
|
|
|
|
filesize = _content.size();
|
2017-03-10 14:14:10 +00:00
|
|
|
if (isVoice) {
|
2015-10-27 02:39:02 +00:00
|
|
|
filename = filedialogDefaultName(qsl("audio"), qsl(".ogg"), QString(), true);
|
|
|
|
filemime = "audio/ogg";
|
|
|
|
} else {
|
2017-12-21 16:15:37 +00:00
|
|
|
if (_information) {
|
|
|
|
if (auto image = base::get_if<FileMediaInformation::Image>(
|
|
|
|
&_information->media)) {
|
|
|
|
fullimage = base::take(image->data);
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 15:45:07 +00:00
|
|
|
auto mimeType = mimeTypeForData(_content);
|
2016-02-12 16:35:06 +00:00
|
|
|
filemime = mimeType.name();
|
2016-11-28 15:45:07 +00:00
|
|
|
if (filemime != stickerMime) {
|
2017-02-21 13:45:56 +00:00
|
|
|
fullimage = Images::prepareOpaque(std::move(fullimage));
|
2016-02-12 16:35:06 +00:00
|
|
|
}
|
|
|
|
if (filemime == "image/jpeg") {
|
2016-11-28 15:45:07 +00:00
|
|
|
filename = filedialogDefaultName(qsl("photo"), qsl(".jpg"), QString(), true);
|
|
|
|
} else if (filemime == "image/png") {
|
|
|
|
filename = filedialogDefaultName(qsl("image"), qsl(".png"), QString(), true);
|
2016-02-12 16:35:06 +00:00
|
|
|
} else {
|
|
|
|
QString ext;
|
|
|
|
QStringList patterns = mimeType.globPatterns();
|
|
|
|
if (!patterns.isEmpty()) {
|
|
|
|
ext = patterns.front().replace('*', QString());
|
|
|
|
}
|
|
|
|
filename = filedialogDefaultName(qsl("file"), ext, QString(), true);
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 09:44:31 +00:00
|
|
|
} else {
|
|
|
|
if (_information) {
|
2017-12-19 16:57:42 +00:00
|
|
|
if (auto image = base::get_if<FileMediaInformation::Image>(
|
|
|
|
&_information->media)) {
|
2017-03-18 09:44:31 +00:00
|
|
|
fullimage = base::take(image->data);
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 09:44:31 +00:00
|
|
|
if (!fullimage.isNull() && fullimage.width() > 0) {
|
|
|
|
if (_type == SendMediaType::Photo) {
|
|
|
|
if (ValidateThumbDimensions(fullimage.width(), fullimage.height())) {
|
|
|
|
filesize = -1; // Fill later.
|
|
|
|
filemime = mimeTypeForName("image/jpeg").name();
|
|
|
|
filename = filedialogDefaultName(qsl("image"), qsl(".jpg"), QString(), true);
|
|
|
|
} else {
|
|
|
|
_type = SendMediaType::File;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_type == SendMediaType::File) {
|
|
|
|
filemime = mimeTypeForName("image/png").name();
|
|
|
|
filename = filedialogDefaultName(qsl("image"), qsl(".png"), QString(), true);
|
|
|
|
{
|
|
|
|
QBuffer buffer(&_content);
|
|
|
|
fullimage.save(&buffer, "PNG");
|
|
|
|
}
|
|
|
|
filesize = _content.size();
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
2017-03-18 09:44:31 +00:00
|
|
|
fullimage = Images::prepareOpaque(std::move(fullimage));
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_result->filesize = (int32)qMin(filesize, qint64(INT_MAX));
|
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
if (!filesize || filesize > App::kFileSizeLimit) {
|
2015-10-27 02:39:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PreparedPhotoThumbs photoThumbs;
|
|
|
|
QVector<MTPPhotoSize> photoSizes;
|
|
|
|
QPixmap thumb;
|
|
|
|
|
|
|
|
QVector<MTPDocumentAttribute> attributes(1, MTP_documentAttributeFilename(MTP_string(filename)));
|
|
|
|
|
2017-08-17 09:45:23 +00:00
|
|
|
auto thumbSize = MTP_photoSizeEmpty(MTP_string(""));
|
|
|
|
auto photo = MTP_photoEmpty(MTP_long(0));
|
|
|
|
auto document = MTP_documentEmpty(MTP_long(0));
|
2015-10-27 02:39:02 +00:00
|
|
|
|
2017-03-10 14:14:10 +00:00
|
|
|
if (!isVoice) {
|
|
|
|
if (!_information) {
|
|
|
|
_information = readMediaInformation(filemime);
|
|
|
|
filemime = _information->filemime;
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
if (auto song = base::get_if<FileMediaInformation::Song>(
|
|
|
|
&_information->media)) {
|
2017-03-10 14:14:10 +00:00
|
|
|
isSong = true;
|
|
|
|
auto flags = MTPDdocumentAttributeAudio::Flag::f_title | MTPDdocumentAttributeAudio::Flag::f_performer;
|
|
|
|
attributes.push_back(MTP_documentAttributeAudio(MTP_flags(flags), MTP_int(song->duration), MTP_string(song->title), MTP_string(song->performer), MTPstring()));
|
|
|
|
if (!song->cover.isNull()) { // cover to thumb
|
|
|
|
auto coverWidth = song->cover.width();
|
|
|
|
auto coverHeight = song->cover.height();
|
|
|
|
auto full = (coverWidth > 90 || coverHeight > 90) ? App::pixmapFromImageInPlace(song->cover.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : App::pixmapFromImageInPlace(std::move(song->cover));
|
|
|
|
{
|
|
|
|
auto thumbFormat = QByteArray("JPG");
|
|
|
|
auto thumbQuality = 87;
|
|
|
|
|
|
|
|
QBuffer buffer(&thumbdata);
|
|
|
|
full.save(&buffer, thumbFormat, thumbQuality);
|
2015-12-28 15:34:45 +00:00
|
|
|
}
|
2017-03-10 14:14:10 +00:00
|
|
|
|
|
|
|
thumb = full;
|
|
|
|
thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0));
|
|
|
|
|
|
|
|
thumbId = rand_value<uint64>();
|
2015-12-28 15:34:45 +00:00
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
} else if (auto video = base::get_if<FileMediaInformation::Video>(
|
|
|
|
&_information->media)) {
|
2017-03-10 14:14:10 +00:00
|
|
|
isVideo = true;
|
|
|
|
auto coverWidth = video->thumbnail.width();
|
|
|
|
auto coverHeight = video->thumbnail.height();
|
2017-12-25 11:13:27 +00:00
|
|
|
if (video->isGifv && !_album) {
|
2017-03-10 14:14:10 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeAnimated());
|
|
|
|
}
|
2017-03-23 16:11:35 +00:00
|
|
|
auto flags = MTPDdocumentAttributeVideo::Flags(0);
|
|
|
|
attributes.push_back(MTP_documentAttributeVideo(MTP_flags(flags), MTP_int(video->duration), MTP_int(coverWidth), MTP_int(coverHeight)));
|
2017-03-10 14:14:10 +00:00
|
|
|
|
|
|
|
auto cover = (coverWidth > 90 || coverHeight > 90)
|
|
|
|
? video->thumbnail.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation)
|
|
|
|
: std::move(video->thumbnail);
|
|
|
|
{
|
|
|
|
auto thumbFormat = QByteArray("JPG");
|
|
|
|
auto thumbQuality = 87;
|
|
|
|
|
|
|
|
QBuffer buffer(&thumbdata);
|
|
|
|
cover.save(&buffer, thumbFormat, thumbQuality);
|
|
|
|
}
|
|
|
|
|
|
|
|
thumb = App::pixmapFromImageInPlace(std::move(cover));
|
|
|
|
thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(thumb.width()), MTP_int(thumb.height()), MTP_int(0));
|
|
|
|
|
|
|
|
thumbId = rand_value<uint64>();
|
2015-12-28 15:34:45 +00:00
|
|
|
}
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 14:14:10 +00:00
|
|
|
if (!fullimage.isNull() && fullimage.width() > 0 && !isSong && !isVideo && !isVoice) {
|
2016-11-15 11:56:49 +00:00
|
|
|
auto w = fullimage.width(), h = fullimage.height();
|
2015-10-27 02:39:02 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeImageSize(MTP_int(w), MTP_int(h)));
|
|
|
|
|
2017-03-09 09:47:45 +00:00
|
|
|
if (ValidateThumbDimensions(w, h)) {
|
2017-03-10 14:14:10 +00:00
|
|
|
if (isAnimation) {
|
2015-10-27 02:39:02 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeAnimated());
|
2016-11-28 15:45:07 +00:00
|
|
|
} else if (_type != SendMediaType::File) {
|
2016-11-15 11:56:49 +00:00
|
|
|
auto thumb = (w > 100 || h > 100) ? App::pixmapFromImageInPlace(fullimage.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : QPixmap::fromImage(fullimage);
|
2015-10-27 02:39:02 +00:00
|
|
|
photoThumbs.insert('s', thumb);
|
|
|
|
photoSizes.push_back(MTP_photoSize(MTP_string("s"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(thumb.width()), MTP_int(thumb.height()), MTP_int(0)));
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
auto medium = (w > 320 || h > 320) ? App::pixmapFromImageInPlace(fullimage.scaled(320, 320, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : QPixmap::fromImage(fullimage);
|
2015-10-27 02:39:02 +00:00
|
|
|
photoThumbs.insert('m', medium);
|
|
|
|
photoSizes.push_back(MTP_photoSize(MTP_string("m"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(medium.width()), MTP_int(medium.height()), MTP_int(0)));
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
auto full = (w > 1280 || h > 1280) ? App::pixmapFromImageInPlace(fullimage.scaled(1280, 1280, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : QPixmap::fromImage(fullimage);
|
2015-10-27 02:39:02 +00:00
|
|
|
photoThumbs.insert('y', full);
|
|
|
|
photoSizes.push_back(MTP_photoSize(MTP_string("y"), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0)));
|
|
|
|
|
|
|
|
{
|
|
|
|
QBuffer buffer(&filedata);
|
2016-12-02 19:16:35 +00:00
|
|
|
full.save(&buffer, "JPG", 87);
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 15:42:01 +00:00
|
|
|
photo = MTP_photo(MTP_flags(0), MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_vector<MTPPhotoSize>(photoSizes));
|
2016-12-13 17:07:56 +00:00
|
|
|
|
|
|
|
if (filesize < 0) {
|
|
|
|
filesize = _result->filesize = filedata.size();
|
|
|
|
}
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray thumbFormat = "JPG";
|
2017-08-15 15:59:40 +00:00
|
|
|
auto thumbQuality = 87;
|
|
|
|
if (!isAnimation
|
|
|
|
&& filemime == stickerMime
|
|
|
|
&& w > 0
|
|
|
|
&& h > 0
|
|
|
|
&& w <= StickerMaxSize
|
|
|
|
&& h <= StickerMaxSize
|
|
|
|
&& filesize < Storage::kMaxStickerInMemory) {
|
2017-03-25 15:42:01 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(0), MTP_string(""), MTP_inputStickerSetEmpty(), MTPMaskCoords()));
|
2015-10-27 02:39:02 +00:00
|
|
|
thumbFormat = "webp";
|
|
|
|
thumbname = qsl("thumb.webp");
|
|
|
|
}
|
|
|
|
|
2016-07-13 17:34:57 +00:00
|
|
|
QPixmap full = (w > 90 || h > 90) ? App::pixmapFromImageInPlace(fullimage.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation)) : QPixmap::fromImage(fullimage, Qt::ColorOnly);
|
2015-10-27 02:39:02 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
QBuffer buffer(&thumbdata);
|
|
|
|
full.save(&buffer, thumbFormat, thumbQuality);
|
|
|
|
}
|
|
|
|
|
|
|
|
thumb = full;
|
|
|
|
thumbSize = MTP_photoSize(MTP_string(""), MTP_fileLocationUnavailable(MTP_long(0), MTP_int(0), MTP_long(0)), MTP_int(full.width()), MTP_int(full.height()), MTP_int(0));
|
|
|
|
|
2016-03-24 10:12:18 +00:00
|
|
|
thumbId = rand_value<uint64>();
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 09:45:23 +00:00
|
|
|
if (_type == SendMediaType::Photo && photo.type() == mtpc_photoEmpty) {
|
|
|
|
_type = SendMediaType::File;
|
|
|
|
}
|
|
|
|
|
2017-03-10 14:14:10 +00:00
|
|
|
if (isVoice) {
|
2017-03-25 15:42:01 +00:00
|
|
|
auto flags = MTPDdocumentAttributeAudio::Flag::f_voice | MTPDdocumentAttributeAudio::Flag::f_waveform;
|
|
|
|
attributes[0] = MTP_documentAttributeAudio(MTP_flags(flags), MTP_int(_duration), MTPstring(), MTPstring(), MTP_bytes(documentWaveformEncode5bit(_waveform)));
|
2016-02-12 16:35:06 +00:00
|
|
|
attributes.resize(1);
|
2016-07-08 13:56:53 +00:00
|
|
|
document = MTP_document(MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_string(filemime), MTP_int(filesize), thumbSize, MTP_int(MTP::maindc()), MTP_int(0), MTP_vector<MTPDocumentAttribute>(attributes));
|
2016-12-13 17:07:56 +00:00
|
|
|
} else if (_type != SendMediaType::Photo) {
|
2016-07-08 13:56:53 +00:00
|
|
|
document = MTP_document(MTP_long(_id), MTP_long(0), MTP_int(unixtime()), MTP_string(filemime), MTP_int(filesize), thumbSize, MTP_int(MTP::maindc()), MTP_int(0), MTP_vector<MTPDocumentAttribute>(attributes));
|
2016-12-13 17:07:56 +00:00
|
|
|
_type = SendMediaType::File;
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
2016-01-05 04:28:06 +00:00
|
|
|
|
2015-10-27 02:39:02 +00:00
|
|
|
_result->type = _type;
|
|
|
|
_result->filepath = _filepath;
|
|
|
|
_result->content = _content;
|
|
|
|
|
|
|
|
_result->filename = filename;
|
2015-12-19 14:37:28 +00:00
|
|
|
_result->filemime = filemime;
|
2015-10-27 02:39:02 +00:00
|
|
|
_result->setFileData(filedata);
|
|
|
|
|
|
|
|
_result->thumbId = thumbId;
|
|
|
|
_result->thumbname = thumbname;
|
|
|
|
_result->setThumbData(thumbdata);
|
|
|
|
_result->thumb = thumb;
|
|
|
|
|
|
|
|
_result->photo = photo;
|
|
|
|
_result->document = document;
|
|
|
|
_result->photoThumbs = photoThumbs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileLoadTask::finish() {
|
2017-09-28 20:11:10 +00:00
|
|
|
if (!_result || !_result->filesize || _result->filesize < 0) {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lng_send_image_empty(lt_name, _filepath)),
|
|
|
|
LayerOption::KeepOther);
|
2017-12-19 16:57:42 +00:00
|
|
|
removeFromAlbum();
|
2016-11-28 15:45:07 +00:00
|
|
|
} else if (_result->filesize > App::kFileSizeLimit) {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(
|
|
|
|
lng_send_image_too_large(lt_name, _filepath)),
|
|
|
|
LayerOption::KeepOther);
|
2017-12-19 16:57:42 +00:00
|
|
|
removeFromAlbum();
|
2016-11-28 15:45:07 +00:00
|
|
|
} else if (App::main()) {
|
|
|
|
App::main()->onSendFileConfirm(_result);
|
2015-10-27 02:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-19 16:57:42 +00:00
|
|
|
|
|
|
|
void FileLoadTask::removeFromAlbum() {
|
|
|
|
if (!_album) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto proj = [](const SendingAlbum::Item &item) {
|
|
|
|
return item.taskId;
|
|
|
|
};
|
|
|
|
const auto it = ranges::find(_album->items, id(), proj);
|
|
|
|
Assert(it != _album->items.end());
|
|
|
|
|
|
|
|
_album->items.erase(it);
|
|
|
|
}
|