2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/files/FileGenerateManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/files/FileId.h"
|
|
|
|
#include "td/telegram/files/FileLoaderUtils.h"
|
|
|
|
#include "td/telegram/files/FileManager.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2018-08-13 22:18:27 +02:00
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2021-10-27 16:32:09 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-12-11 21:18:58 +01:00
|
|
|
#include "td/utils/format.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2018-10-30 15:25:31 +01:00
|
|
|
#include "td/utils/Parser.h"
|
2019-04-26 03:05:08 +02:00
|
|
|
#include "td/utils/port/FileFd.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/path.h"
|
2018-12-11 21:18:58 +01:00
|
|
|
#include "td/utils/port/Stat.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 22:18:27 +02:00
|
|
|
#include <cmath>
|
2018-09-27 15:37:15 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class FileGenerateActor : public Actor {
|
|
|
|
public:
|
2022-05-11 16:17:20 +02:00
|
|
|
virtual void file_generate_write_part(int64 offset, string data, Promise<> promise) {
|
2019-04-26 02:18:00 +02:00
|
|
|
LOG(ERROR) << "Receive unexpected file_generate_write_part";
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
virtual void file_generate_progress(int64 expected_size, int64 local_prefix_size, Promise<> promise) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void file_generate_finish(Status status, Promise<> promise) = 0;
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileDownloadGenerateActor final : public FileGenerateActor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2018-09-27 03:19:03 +02:00
|
|
|
FileDownloadGenerateActor(FileType file_type, FileId file_id, unique_ptr<FileGenerateCallback> callback,
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorShared<> parent)
|
|
|
|
: file_type_(file_type), file_id_(file_id), callback_(std::move(callback)), parent_(std::move(parent)) {
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
void file_generate_progress(int64 expected_size, int64 local_prefix_size, Promise<> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void file_generate_finish(Status status, Promise<> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FileType file_type_;
|
|
|
|
FileId file_id_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<FileGenerateCallback> callback_;
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorShared<> parent_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-08-13 22:18:27 +02:00
|
|
|
LOG(INFO) << "Generate by downloading " << file_id_;
|
2021-07-04 04:58:54 +02:00
|
|
|
class Callback final : public FileManager::DownloadCallback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit Callback(ActorId<FileDownloadGenerateActor> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: upload during download
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_download_ok(FileId file_id) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(parent_, &FileDownloadGenerateActor::on_download_ok);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_download_error(FileId file_id, Status error) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(parent_, &FileDownloadGenerateActor::on_download_error, std::move(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<FileDownloadGenerateActor> parent_;
|
|
|
|
};
|
|
|
|
|
2018-11-11 12:38:04 +01:00
|
|
|
send_closure(G()->file_manager(), &FileManager::download, file_id_, std::make_shared<Callback>(actor_id(this)), 1,
|
2022-02-28 13:15:00 +01:00
|
|
|
FileManager::KEEP_DOWNLOAD_OFFSET, FileManager::KEEP_DOWNLOAD_LIMIT);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2022-02-28 13:15:00 +01:00
|
|
|
send_closure(G()->file_manager(), &FileManager::download, file_id_, nullptr, 0, FileManager::KEEP_DOWNLOAD_OFFSET,
|
|
|
|
FileManager::KEEP_DOWNLOAD_LIMIT);
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_download_ok() {
|
2018-09-27 18:51:45 +02:00
|
|
|
send_lambda(G()->file_manager(),
|
|
|
|
[file_type = file_type_, file_id = file_id_, callback = std::move(callback_)]() mutable {
|
|
|
|
auto file_view = G()->td().get_actor_unsafe()->file_manager_->get_file_view(file_id);
|
2021-09-16 15:58:30 +02:00
|
|
|
CHECK(!file_view.empty());
|
2018-09-27 18:51:45 +02:00
|
|
|
if (file_view.has_local_location()) {
|
|
|
|
auto location = file_view.local_location();
|
|
|
|
location.file_type_ = file_type;
|
2021-10-19 17:11:16 +02:00
|
|
|
callback->on_ok(std::move(location));
|
2018-09-27 18:51:45 +02:00
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Expected to have local location";
|
|
|
|
callback->on_error(Status::Error(500, "Unknown"));
|
|
|
|
}
|
|
|
|
});
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
void on_download_error(Status error) {
|
|
|
|
callback_->on_error(std::move(error));
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-28 14:59:57 +02:00
|
|
|
class WebFileDownloadGenerateActor final : public FileGenerateActor {
|
2018-08-13 22:18:27 +02:00
|
|
|
public:
|
2022-07-28 14:59:57 +02:00
|
|
|
WebFileDownloadGenerateActor(string conversion, unique_ptr<FileGenerateCallback> callback, ActorShared<> parent)
|
2018-08-13 22:18:27 +02:00
|
|
|
: conversion_(std::move(conversion)), callback_(std::move(callback)), parent_(std::move(parent)) {
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
void file_generate_progress(int64 expected_size, int64 local_prefix_size, Promise<> promise) final {
|
2018-08-13 22:18:27 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void file_generate_finish(Status status, Promise<> promise) final {
|
2018-08-13 22:18:27 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
string conversion_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<FileGenerateCallback> callback_;
|
2018-08-13 22:18:27 +02:00
|
|
|
ActorShared<> parent_;
|
|
|
|
string file_name_;
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class Callback final : public NetQueryCallback {
|
2022-07-28 14:59:57 +02:00
|
|
|
ActorId<WebFileDownloadGenerateActor> parent_;
|
2018-08-13 22:18:27 +02:00
|
|
|
|
|
|
|
public:
|
2022-07-28 14:59:57 +02:00
|
|
|
explicit Callback(ActorId<WebFileDownloadGenerateActor> parent) : parent_(parent) {
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final {
|
2022-07-28 14:59:57 +02:00
|
|
|
send_closure(parent_, &WebFileDownloadGenerateActor::on_result, std::move(query));
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup_shared() final {
|
2022-07-28 14:59:57 +02:00
|
|
|
send_closure(parent_, &WebFileDownloadGenerateActor::hangup_shared);
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
ActorOwn<NetQueryCallback> net_callback_;
|
|
|
|
|
2022-07-28 15:47:19 +02:00
|
|
|
Result<tl_object_ptr<telegram_api::InputWebFileLocation>> parse_conversion() {
|
2018-08-13 22:18:27 +02:00
|
|
|
auto parts = full_split(Slice(conversion_), '#');
|
2022-07-28 15:47:19 +02:00
|
|
|
if (parts.size() <= 2 || !parts[0].empty() || !parts.back().empty()) {
|
|
|
|
return Status::Error("Wrong conversion");
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:43:53 +02:00
|
|
|
if (parts.size() == 6 && parts[1] == "audio_t") {
|
2022-07-28 15:47:19 +02:00
|
|
|
// music thumbnail
|
|
|
|
if (parts[2].empty() && parts[3].empty()) {
|
|
|
|
return Status::Error("Title or performer must be non-empty");
|
|
|
|
}
|
2022-08-05 21:43:53 +02:00
|
|
|
if (parts[4] != "0" && parts[4] != "1") {
|
|
|
|
return Status::Error("Invalid conversion");
|
|
|
|
}
|
2022-07-28 15:47:19 +02:00
|
|
|
|
2022-08-05 21:43:53 +02:00
|
|
|
bool is_small = parts[4][0] == '1';
|
|
|
|
file_name_ = PSTRING() << "Album cover " << (is_small ? "thumbnail " : "") << "for " << parts[3] << " - "
|
|
|
|
<< parts[2] << ".jpg";
|
2022-07-28 15:47:19 +02:00
|
|
|
|
2022-08-05 21:43:53 +02:00
|
|
|
int32 flags = telegram_api::inputWebFileAudioAlbumThumbLocation::TITLE_MASK;
|
|
|
|
if (is_small) {
|
|
|
|
flags |= telegram_api::inputWebFileAudioAlbumThumbLocation::SMALL_MASK;
|
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::inputWebFileAudioAlbumThumbLocation>(flags, false /*ignored*/, nullptr,
|
|
|
|
parts[2].str(), parts[3].str());
|
2022-07-28 15:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parts.size() != 9 || parts[1] != "map") {
|
2018-08-13 22:18:27 +02:00
|
|
|
return Status::Error("Wrong conversion");
|
|
|
|
}
|
|
|
|
|
|
|
|
TRY_RESULT(zoom, to_integer_safe<int32>(parts[2]));
|
|
|
|
TRY_RESULT(x, to_integer_safe<int32>(parts[3]));
|
|
|
|
TRY_RESULT(y, to_integer_safe<int32>(parts[4]));
|
|
|
|
TRY_RESULT(width, to_integer_safe<int32>(parts[5]));
|
|
|
|
TRY_RESULT(height, to_integer_safe<int32>(parts[6]));
|
|
|
|
TRY_RESULT(scale, to_integer_safe<int32>(parts[7]));
|
|
|
|
|
|
|
|
if (zoom < 13 || zoom > 20) {
|
|
|
|
return Status::Error("Wrong zoom");
|
|
|
|
}
|
|
|
|
auto size = 256 * (1 << zoom);
|
|
|
|
if (x < 0 || x >= size) {
|
|
|
|
return Status::Error("Wrong x");
|
|
|
|
}
|
|
|
|
if (y < 0 || y >= size) {
|
|
|
|
return Status::Error("Wrong y");
|
|
|
|
}
|
|
|
|
if (width < 16 || height < 16 || width > 1024 || height > 1024) {
|
|
|
|
return Status::Error("Wrong dimensions");
|
|
|
|
}
|
|
|
|
if (scale < 1 || scale > 3) {
|
|
|
|
return Status::Error("Wrong scale");
|
|
|
|
}
|
|
|
|
|
2018-08-14 01:11:49 +02:00
|
|
|
file_name_ = PSTRING() << "map_" << zoom << "_" << x << "_" << y << ".png";
|
2018-08-13 22:18:27 +02:00
|
|
|
|
|
|
|
const double PI = 3.14159265358979323846;
|
|
|
|
double longitude = (x + 0.1) * 360.0 / size - 180;
|
|
|
|
double latitude = 90 - 360 * std::atan(std::exp(((y + 0.1) / size - 0.5) * 2 * PI)) / PI;
|
|
|
|
|
2018-08-14 01:11:49 +02:00
|
|
|
int64 access_hash = G()->get_location_access_hash(latitude, longitude);
|
2018-08-13 22:18:27 +02:00
|
|
|
return make_tl_object<telegram_api::inputWebFileGeoPointLocation>(
|
2020-10-17 21:47:47 +02:00
|
|
|
make_tl_object<telegram_api::inputGeoPoint>(0, latitude, longitude, 0), access_hash, width, height, zoom,
|
|
|
|
scale);
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-08-13 22:18:27 +02:00
|
|
|
auto r_input_web_file = parse_conversion();
|
|
|
|
if (r_input_web_file.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't parse " << conversion_ << ": " << r_input_web_file.error();
|
|
|
|
return on_error(r_input_web_file.move_as_error());
|
|
|
|
}
|
|
|
|
|
2022-07-28 14:59:57 +02:00
|
|
|
net_callback_ = create_actor<Callback>("WebFileDownloadGenerateCallback", actor_id(this));
|
2018-08-13 22:18:27 +02:00
|
|
|
|
|
|
|
LOG(INFO) << "Download " << conversion_;
|
2020-03-15 23:01:14 +01:00
|
|
|
auto query =
|
2022-02-01 17:34:14 +01:00
|
|
|
G()->net_query_creator().create(telegram_api::upload_getWebFile(r_input_web_file.move_as_ok(), 0, 1 << 20), {},
|
|
|
|
G()->get_webfile_dc_id(), NetQuery::Type::DownloadSmall);
|
2018-08-13 22:18:27 +02:00
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(query), {net_callback_.get(), 0});
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(NetQueryPtr query) {
|
|
|
|
auto r_result = process_result(std::move(query));
|
|
|
|
if (r_result.is_error()) {
|
|
|
|
return on_error(r_result.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
callback_->on_ok(r_result.ok());
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<FullLocalFileLocation> process_result(NetQueryPtr query) {
|
|
|
|
TRY_RESULT(web_file, fetch_result<telegram_api::upload_getWebFile>(std::move(query)));
|
|
|
|
|
|
|
|
if (static_cast<size_t>(web_file->size_) != web_file->bytes_.size()) {
|
2022-07-28 14:59:57 +02:00
|
|
|
LOG(ERROR) << "Failed to download web file of size " << web_file->size_;
|
2018-08-13 22:18:27 +02:00
|
|
|
return Status::Error("File is too big");
|
|
|
|
}
|
|
|
|
|
|
|
|
return save_file_bytes(FileType::Thumbnail, std::move(web_file->bytes_), file_name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status error) {
|
|
|
|
callback_->on_error(std::move(error));
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup_shared() final {
|
2022-09-20 01:02:28 +02:00
|
|
|
on_error(Status::Error(-1, "Canceled"));
|
2018-08-13 22:18:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileExternalGenerateActor final : public FileGenerateActor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
FileExternalGenerateActor(uint64 query_id, const FullGenerateFileLocation &generate_location,
|
|
|
|
const LocalFileLocation &local_location, string name,
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<FileGenerateCallback> callback, ActorShared<> parent)
|
2018-12-31 20:04:05 +01:00
|
|
|
: query_id_(query_id)
|
|
|
|
, generate_location_(generate_location)
|
|
|
|
, local_(local_location)
|
|
|
|
, name_(std::move(name))
|
|
|
|
, callback_(std::move(callback))
|
|
|
|
, parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:17:20 +02:00
|
|
|
void file_generate_write_part(int64 offset, string data, Promise<> promise) final {
|
2019-04-26 02:18:00 +02:00
|
|
|
check_status(do_file_generate_write_part(offset, data), std::move(promise));
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:17:20 +02:00
|
|
|
void file_generate_progress(int64 expected_size, int64 local_prefix_size, Promise<> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
check_status(do_file_generate_progress(expected_size, local_prefix_size), std::move(promise));
|
|
|
|
}
|
2019-04-26 02:18:00 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void file_generate_finish(Status status, Promise<> promise) final {
|
2018-08-29 12:44:21 +02:00
|
|
|
if (status.is_error()) {
|
|
|
|
check_status(std::move(status));
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
check_status(do_file_generate_finish(), std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint64 query_id_;
|
|
|
|
FullGenerateFileLocation generate_location_;
|
|
|
|
LocalFileLocation local_;
|
|
|
|
string name_;
|
|
|
|
string path_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<FileGenerateCallback> callback_;
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorShared<> parent_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-01-20 15:57:52 +01:00
|
|
|
if (local_.type() == LocalFileLocation::Type::Full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_->on_ok(local_.full());
|
|
|
|
callback_.reset();
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
|
2018-01-20 15:57:52 +01:00
|
|
|
if (local_.type() == LocalFileLocation::Type::Partial) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const auto &partial = local_.partial();
|
|
|
|
path_ = partial.path_;
|
2018-01-05 15:45:17 +01:00
|
|
|
LOG(INFO) << "Unlink partially generated file at " << path_;
|
2018-12-31 20:04:05 +01:00
|
|
|
unlink(path_).ignore();
|
|
|
|
} else {
|
2018-01-20 15:57:52 +01:00
|
|
|
auto r_file_path = open_temp_file(generate_location_.file_type_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_file_path.is_error()) {
|
|
|
|
return check_status(r_file_path.move_as_error());
|
|
|
|
}
|
|
|
|
auto file_path = r_file_path.move_as_ok();
|
|
|
|
file_path.first.close();
|
|
|
|
path_ = file_path.second;
|
|
|
|
}
|
|
|
|
send_closure(
|
|
|
|
G()->td(), &Td::send_update,
|
|
|
|
make_tl_object<td_api::updateFileGenerationStart>(
|
|
|
|
static_cast<int64>(query_id_), generate_location_.original_path_, path_, generate_location_.conversion_));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2022-09-20 01:02:28 +02:00
|
|
|
check_status(Status::Error(-1, "Canceled"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-11 16:17:20 +02:00
|
|
|
Status do_file_generate_write_part(int64 offset, const string &data) {
|
2019-04-26 02:18:00 +02:00
|
|
|
if (offset < 0) {
|
|
|
|
return Status::Error("Wrong offset specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto size = data.size();
|
|
|
|
TRY_RESULT(fd, FileFd::open(path_, FileFd::Create | FileFd::Write));
|
|
|
|
TRY_RESULT(written, fd.pwrite(data, offset));
|
|
|
|
if (written != size) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to write file: written " << written << " bytes instead of " << size);
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:17:20 +02:00
|
|
|
Status do_file_generate_progress(int64 expected_size, int64 local_prefix_size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (local_prefix_size < 0) {
|
2022-05-11 16:17:20 +02:00
|
|
|
return Status::Error(400, "Invalid local prefix size");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-26 17:11:15 +01:00
|
|
|
callback_->on_partial_generate(PartialLocalFileLocation{generate_location_.file_type_, local_prefix_size, path_, "",
|
|
|
|
Bitmask(Bitmask::Ones{}, 1).encode()},
|
2018-11-11 12:38:04 +01:00
|
|
|
expected_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:44:21 +02:00
|
|
|
Status do_file_generate_finish() {
|
2022-06-14 20:09:07 +02:00
|
|
|
TRY_RESULT(perm_path, create_from_temp(generate_location_.file_type_, path_, name_));
|
2018-01-20 15:57:52 +01:00
|
|
|
callback_->on_ok(FullLocalFileLocation(generate_location_.file_type_, std::move(perm_path), 0));
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_.reset();
|
|
|
|
stop();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_status(Status status, Promise<> promise = Promise<>()) {
|
|
|
|
if (promise) {
|
2022-09-20 01:02:28 +02:00
|
|
|
if (status.is_ok() || status.code() == -1) {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
} else {
|
|
|
|
promise.set_error(Status::Error(400, status.message()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.is_error()) {
|
2018-01-05 15:45:17 +01:00
|
|
|
LOG(INFO) << "Unlink partially generated file at " << path_ << " because of " << status;
|
2018-12-31 20:04:05 +01:00
|
|
|
unlink(path_).ignore();
|
|
|
|
callback_->on_error(std::move(status));
|
|
|
|
callback_.reset();
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(G()->td(), &Td::send_update,
|
|
|
|
make_tl_object<td_api::updateFileGenerationStop>(static_cast<int64>(query_id_)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FileGenerateManager::Query::~Query() = default;
|
2021-10-18 13:36:15 +02:00
|
|
|
FileGenerateManager::Query::Query(Query &&other) noexcept = default;
|
|
|
|
FileGenerateManager::Query &FileGenerateManager::Query::operator=(Query &&other) noexcept = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-31 19:11:02 +01:00
|
|
|
static Status check_mtime(std::string &conversion, CSlice original_path) {
|
2018-10-30 15:25:31 +01:00
|
|
|
if (original_path.empty()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
ConstParser parser(conversion);
|
2020-07-21 13:24:55 +02:00
|
|
|
if (!parser.try_skip("#mtime#")) {
|
2018-10-31 19:11:02 +01:00
|
|
|
return Status::OK();
|
2018-10-30 15:25:31 +01:00
|
|
|
}
|
|
|
|
auto mtime_str = parser.read_till('#');
|
|
|
|
parser.skip('#');
|
2018-10-31 19:11:02 +01:00
|
|
|
while (mtime_str.size() >= 2 && mtime_str[0] == '0') {
|
|
|
|
mtime_str.remove_prefix(1);
|
|
|
|
}
|
2018-10-30 15:25:31 +01:00
|
|
|
auto r_mtime = to_integer_safe<uint64>(mtime_str);
|
|
|
|
if (parser.status().is_error() || r_mtime.is_error()) {
|
2018-10-31 19:11:02 +01:00
|
|
|
return Status::OK();
|
2018-10-30 15:25:31 +01:00
|
|
|
}
|
|
|
|
auto expected_mtime = r_mtime.move_as_ok();
|
|
|
|
conversion = parser.read_all().str();
|
|
|
|
auto r_stat = stat(original_path);
|
|
|
|
uint64 actual_mtime = r_stat.is_ok() ? r_stat.ok().mtime_nsec_ : 0;
|
2019-03-15 19:52:54 +01:00
|
|
|
if (FileManager::are_modification_times_equal(expected_mtime, actual_mtime)) {
|
2019-01-17 23:17:20 +01:00
|
|
|
LOG(DEBUG) << "File \"" << original_path << "\" modification time " << actual_mtime << " matches";
|
2018-10-31 19:11:02 +01:00
|
|
|
return Status::OK();
|
2018-10-30 15:25:31 +01:00
|
|
|
}
|
2018-11-06 17:02:19 +01:00
|
|
|
return Status::Error(PSLICE() << "FILE_GENERATE_LOCATION_INVALID: File \"" << original_path
|
2018-10-31 19:11:02 +01:00
|
|
|
<< "\" was modified: " << tag("expected modification time", expected_mtime)
|
|
|
|
<< tag("actual modification time", actual_mtime));
|
2018-10-30 15:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileGenerateManager::generate_file(uint64 query_id, FullGenerateFileLocation generate_location,
|
2018-12-31 20:04:05 +01:00
|
|
|
const LocalFileLocation &local_location, string name,
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<FileGenerateCallback> callback) {
|
2018-10-31 19:11:02 +01:00
|
|
|
LOG(INFO) << "Begin to generate file with " << generate_location;
|
2018-10-30 15:25:31 +01:00
|
|
|
auto mtime_status = check_mtime(generate_location.conversion_, generate_location.original_path_);
|
|
|
|
if (mtime_status.is_error()) {
|
|
|
|
return callback->on_error(std::move(mtime_status));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(query_id != 0);
|
2020-07-13 22:20:16 +02:00
|
|
|
auto it_flag = query_id_to_query_.emplace(query_id, Query{});
|
2021-03-15 01:32:14 +01:00
|
|
|
LOG_CHECK(it_flag.second) << "Query identifier must be unique";
|
2018-12-31 20:04:05 +01:00
|
|
|
auto parent = actor_shared(this, query_id);
|
|
|
|
|
|
|
|
Slice file_id_query = "#file_id#";
|
|
|
|
Slice conversion = generate_location.conversion_;
|
|
|
|
|
|
|
|
auto &query = it_flag.first->second;
|
2018-08-10 23:08:13 +02:00
|
|
|
if (begins_with(conversion, file_id_query)) {
|
2018-03-06 19:31:20 +01:00
|
|
|
auto file_id = FileId(to_integer<int32>(conversion.substr(file_id_query.size())), 0);
|
2018-01-20 15:57:52 +01:00
|
|
|
query.worker_ = create_actor<FileDownloadGenerateActor>("FileDownloadGenerateActor", generate_location.file_type_,
|
2018-12-31 20:04:05 +01:00
|
|
|
file_id, std::move(callback), std::move(parent));
|
2022-07-28 14:59:57 +02:00
|
|
|
} else if (FileManager::is_remotely_generated_file(conversion) && generate_location.original_path_.empty()) {
|
|
|
|
query.worker_ = create_actor<WebFileDownloadGenerateActor>("WebFileDownloadGenerateActor",
|
|
|
|
std::move(generate_location.conversion_),
|
|
|
|
std::move(callback), std::move(parent));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
query.worker_ = create_actor<FileExternalGenerateActor>("FileExternalGenerationActor", query_id, generate_location,
|
|
|
|
local_location, std::move(name), std::move(callback),
|
|
|
|
std::move(parent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileGenerateManager::cancel(uint64 query_id) {
|
|
|
|
auto it = query_id_to_query_.find(query_id);
|
|
|
|
if (it == query_id_to_query_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
it->second.worker_.reset();
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:17:20 +02:00
|
|
|
void FileGenerateManager::external_file_generate_write_part(uint64 query_id, int64 offset, string data,
|
2019-04-26 02:18:00 +02:00
|
|
|
Promise<> promise) {
|
|
|
|
auto it = query_id_to_query_.find(query_id);
|
|
|
|
if (it == query_id_to_query_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unknown generation_id"));
|
|
|
|
}
|
|
|
|
send_closure(it->second.worker_, &FileGenerateActor::file_generate_write_part, offset, std::move(data),
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:17:20 +02:00
|
|
|
void FileGenerateManager::external_file_generate_progress(uint64 query_id, int64 expected_size, int64 local_prefix_size,
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<> promise) {
|
|
|
|
auto it = query_id_to_query_.find(query_id);
|
|
|
|
if (it == query_id_to_query_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unknown generation_id"));
|
|
|
|
}
|
|
|
|
send_closure(it->second.worker_, &FileGenerateActor::file_generate_progress, expected_size, local_prefix_size,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileGenerateManager::external_file_generate_finish(uint64 query_id, Status status, Promise<> promise) {
|
|
|
|
auto it = query_id_to_query_.find(query_id);
|
|
|
|
if (it == query_id_to_query_.end()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Unknown generation_id"));
|
|
|
|
}
|
|
|
|
send_closure(it->second.worker_, &FileGenerateActor::file_generate_finish, std::move(status), std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileGenerateManager::do_cancel(uint64 query_id) {
|
|
|
|
query_id_to_query_.erase(query_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileGenerateManager::hangup_shared() {
|
|
|
|
do_cancel(get_link_token());
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileGenerateManager::hangup() {
|
|
|
|
close_flag_ = true;
|
|
|
|
for (auto &it : query_id_to_query_) {
|
|
|
|
it.second.worker_.reset();
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileGenerateManager::loop() {
|
|
|
|
if (close_flag_ && query_id_to_query_.empty()) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|