2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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/FileHashUploader.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-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2021-10-27 16:32:09 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/crypto.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/MimeType.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/PathView.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileHashUploader::start_up() {
|
|
|
|
auto status = init();
|
|
|
|
if (status.is_error()) {
|
|
|
|
callback_->on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status FileHashUploader::init() {
|
|
|
|
TRY_RESULT(fd, FileFd::open(local_.path_, FileFd::Read));
|
2019-06-19 16:31:25 +02:00
|
|
|
TRY_RESULT(file_size, fd.get_size());
|
|
|
|
if (file_size != size_) {
|
2019-08-26 03:53:22 +02:00
|
|
|
return Status::Error("Size mismatch");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
fd_ = BufferedFd<FileFd>(std::move(fd));
|
2019-07-23 02:20:11 +02:00
|
|
|
sha256_state_.init();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
resource_state_.set_unit_size(1024);
|
|
|
|
resource_state_.update_estimated_limit(size_);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void FileHashUploader::loop() {
|
|
|
|
if (stop_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto status = loop_impl();
|
|
|
|
if (status.is_error()) {
|
|
|
|
callback_->on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status FileHashUploader::loop_impl() {
|
2018-04-19 15:08:30 +02:00
|
|
|
if (state_ == State::CalcSha) {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(loop_sha());
|
|
|
|
}
|
2018-04-19 15:08:30 +02:00
|
|
|
if (state_ == State::NetRequest) {
|
2022-05-10 16:13:16 +02:00
|
|
|
// messages.getDocumentByHash#338e2464 sha256:bytes size:long mime_type:string = Document;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto hash = BufferSlice(32);
|
2023-02-02 15:40:47 +01:00
|
|
|
sha256_state_.extract(hash.as_mutable_slice(), true);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto mime_type = MimeType::from_extension(PathView(local_.path_).extension(), "image/gif");
|
2022-05-10 16:13:16 +02:00
|
|
|
auto query = telegram_api::messages_getDocumentByHash(std::move(hash), size_, std::move(mime_type));
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Send getDocumentByHash request: " << to_string(query);
|
2020-03-15 22:17:11 +01:00
|
|
|
auto ptr = G()->net_query_creator().create(query);
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(ptr), actor_shared(this));
|
2018-04-19 15:08:30 +02:00
|
|
|
state_ = State::WaitNetResult;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status FileHashUploader::loop_sha() {
|
|
|
|
auto limit = resource_state_.unused();
|
|
|
|
if (limit == 0) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
if (limit > size_left_) {
|
|
|
|
limit = size_left_;
|
|
|
|
}
|
|
|
|
resource_state_.start_use(limit);
|
|
|
|
|
2018-08-14 09:42:40 +02:00
|
|
|
fd_.get_poll_info().add_flags(PollFlags::Read());
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(read_size, fd_.flush_read(static_cast<size_t>(limit)));
|
|
|
|
if (read_size != static_cast<size_t>(limit)) {
|
2019-08-26 03:53:22 +02:00
|
|
|
return Status::Error("Unexpected end of file");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
auto ready = fd_.input_buffer().prepare_read();
|
|
|
|
if (ready.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
2019-07-23 02:20:11 +02:00
|
|
|
sha256_state_.feed(ready);
|
2018-12-31 20:04:05 +01:00
|
|
|
fd_.input_buffer().confirm_read(ready.size());
|
|
|
|
}
|
|
|
|
resource_state_.stop_use(limit);
|
|
|
|
|
|
|
|
size_left_ -= narrow_cast<int64>(read_size);
|
|
|
|
CHECK(size_left_ >= 0);
|
|
|
|
if (size_left_ == 0) {
|
2018-04-19 15:08:30 +02:00
|
|
|
state_ = State::NetRequest;
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileHashUploader::on_result(NetQueryPtr net_query) {
|
|
|
|
auto status = on_result_impl(std::move(net_query));
|
|
|
|
if (status.is_error()) {
|
|
|
|
callback_->on_error(std::move(status));
|
|
|
|
stop_flag_ = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status FileHashUploader::on_result_impl(NetQueryPtr net_query) {
|
2023-09-02 21:39:26 +02:00
|
|
|
TRY_RESULT(res, fetch_result<telegram_api::messages_getDocumentByHash>(std::move(net_query)));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
switch (res->get_id()) {
|
|
|
|
case telegram_api::documentEmpty::ID:
|
|
|
|
return Status::Error("Document is not found by hash");
|
|
|
|
case telegram_api::document::ID: {
|
|
|
|
auto document = move_tl_object_as<telegram_api::document>(res);
|
2019-04-09 00:37:20 +02:00
|
|
|
if (!DcId::is_valid(document->dc_id_)) {
|
|
|
|
return Status::Error("Found document has invalid DcId");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_->on_ok(FullRemoteFileLocation(FileType::Document, document->id_, document->access_hash_,
|
2018-12-05 10:32:31 +01:00
|
|
|
DcId::internal(document->dc_id_),
|
|
|
|
document->file_reference_.as_slice().str()));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
stop_flag_ = true;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Status::Error("UNREACHABLE");
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|