2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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"
|
|
|
|
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#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));
|
|
|
|
if (fd.get_size() != size_) {
|
|
|
|
return Status::Error("size mismatch");
|
|
|
|
}
|
|
|
|
fd_ = BufferedFd<FileFd>(std::move(fd));
|
|
|
|
sha256_init(&sha256_state_);
|
|
|
|
|
|
|
|
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) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// messages.getDocumentByHash#338e2464 sha256:bytes size:int mime_type:string = Document;
|
|
|
|
auto hash = BufferSlice(32);
|
|
|
|
sha256_final(&sha256_state_, hash.as_slice());
|
|
|
|
auto mime_type = MimeType::from_extension(PathView(local_.path_).extension(), "image/gif");
|
|
|
|
auto query =
|
|
|
|
telegram_api::messages_getDocumentByHash(std::move(hash), static_cast<int32>(size_), std::move(mime_type));
|
|
|
|
LOG(INFO) << "Send getDocumentByHash request: " << to_string(query);
|
|
|
|
auto ptr = G()->net_query_creator().create(create_storer(query));
|
|
|
|
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)) {
|
|
|
|
return Status::Error("unexpected end of file");
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
auto ready = fd_.input_buffer().prepare_read();
|
|
|
|
if (ready.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sha256_update(ready, &sha256_state_);
|
|
|
|
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) {
|
|
|
|
if (net_query->is_error()) {
|
|
|
|
return net_query->move_as_error();
|
|
|
|
}
|
|
|
|
TRY_RESULT(res, fetch_result<telegram_api::messages_getDocumentByHash>(net_query->ok()));
|
|
|
|
|
|
|
|
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);
|
|
|
|
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
|