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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2018-12-26 17:11:15 +01:00
|
|
|
#include "td/telegram/files/FileBitmask.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/net/DcId.h"
|
|
|
|
|
2018-12-08 23:00:27 +01:00
|
|
|
#include "td/utils/base64.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
#include "td/utils/Variant.h"
|
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
namespace td {
|
2019-01-30 17:08:50 +01:00
|
|
|
class FileReferenceView {
|
|
|
|
public:
|
|
|
|
static Slice invalid_file_reference() {
|
|
|
|
return "#";
|
|
|
|
}
|
|
|
|
static std::string create_one(td::Slice first) {
|
|
|
|
char second_length = char(255);
|
|
|
|
return PSTRING() << second_length << first;
|
|
|
|
}
|
|
|
|
static std::string create_two(td::Slice first, td::Slice second = {}) {
|
|
|
|
if (second.size() >= 255) {
|
|
|
|
LOG(ERROR) << "File reference is too big " << base64_encode(second);
|
|
|
|
second = invalid_file_reference();
|
|
|
|
}
|
|
|
|
char second_length = narrow_cast<unsigned char>(second.size());
|
|
|
|
return PSTRING() << second_length << first << second;
|
|
|
|
}
|
|
|
|
std::string create(td::Slice first, td::Slice second) const {
|
|
|
|
if (size() == 1) {
|
|
|
|
return create_one(first);
|
|
|
|
}
|
|
|
|
return create_two(first, second);
|
|
|
|
}
|
|
|
|
FileReferenceView(Slice data) {
|
|
|
|
if (data.empty()) {
|
|
|
|
size_ = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char second_size = data.ubegin()[0];
|
|
|
|
|
|
|
|
if (second_size == 255) {
|
|
|
|
first_ = data.substr(1);
|
|
|
|
second_ = data.substr(1);
|
|
|
|
size_ = 1;
|
|
|
|
} else {
|
|
|
|
if (second_size > data.size() - 1) {
|
|
|
|
size_ = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto first_size = data.size() - 1 - second_size;
|
|
|
|
first_ = data.substr(1, first_size);
|
|
|
|
second_ = data.substr(1 + first_size);
|
|
|
|
size_ = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t size() const {
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
Slice first() const {
|
|
|
|
return first_;
|
|
|
|
}
|
|
|
|
Slice second() const {
|
|
|
|
return second_;
|
|
|
|
}
|
|
|
|
bool has_first() const {
|
|
|
|
return first() != invalid_file_reference();
|
|
|
|
}
|
|
|
|
bool has_second() const {
|
|
|
|
return second() != invalid_file_reference();
|
|
|
|
}
|
|
|
|
std::pair<std::string, bool> delete_file_reference(Slice bad_file_reference) const {
|
|
|
|
if (bad_file_reference == FileReferenceView::invalid_file_reference()) {
|
|
|
|
return {"", false};
|
|
|
|
}
|
|
|
|
auto first = this->first();
|
|
|
|
auto second = this->second();
|
|
|
|
bool changed = false;
|
|
|
|
if (first == bad_file_reference) {
|
|
|
|
first = invalid_file_reference();
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (second == bad_file_reference) {
|
|
|
|
second = invalid_file_reference();
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (!changed) {
|
|
|
|
return {"", false};
|
|
|
|
}
|
|
|
|
return {create(first, second), true};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Slice first_;
|
|
|
|
Slice second_;
|
|
|
|
int size_;
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
struct EmptyRemoteFileLocation {
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const EmptyRemoteFileLocation &lhs, const EmptyRemoteFileLocation &rhs) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const EmptyRemoteFileLocation &lhs, const EmptyRemoteFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PartialRemoteFileLocation {
|
|
|
|
int64 file_id_;
|
|
|
|
int32 part_count_;
|
|
|
|
int32 part_size_;
|
|
|
|
int32 ready_part_count_;
|
|
|
|
int32 is_big_;
|
2018-11-11 14:35:51 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
store(file_id_, storer);
|
|
|
|
store(part_count_, storer);
|
|
|
|
store(part_size_, storer);
|
|
|
|
store(ready_part_count_, storer);
|
|
|
|
store(is_big_, storer);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
|
|
|
parse(file_id_, parser);
|
|
|
|
parse(part_count_, parser);
|
|
|
|
parse(part_size_, parser);
|
|
|
|
parse(ready_part_count_, parser);
|
|
|
|
parse(is_big_, parser);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const PartialRemoteFileLocation &lhs, const PartialRemoteFileLocation &rhs) {
|
|
|
|
return lhs.file_id_ == rhs.file_id_ && lhs.part_count_ == rhs.part_count_ && lhs.part_size_ == rhs.part_size_ &&
|
|
|
|
lhs.ready_part_count_ == rhs.ready_part_count_ && lhs.is_big_ == rhs.is_big_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const PartialRemoteFileLocation &lhs, const PartialRemoteFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2018-12-27 17:50:45 +01:00
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const PartialRemoteFileLocation &location) {
|
|
|
|
return sb << '[' << (location.is_big_ ? "Big" : "Small") << " partial remote location with " << location.part_count_
|
|
|
|
<< " parts of size " << location.part_size_ << " with " << location.ready_part_count_ << " ready parts]";
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct PhotoRemoteFileLocation {
|
|
|
|
int64 id_;
|
|
|
|
int64 access_hash_;
|
|
|
|
int64 volume_id_;
|
|
|
|
int64 secret_;
|
|
|
|
int32 local_id_;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
store(id_, storer);
|
|
|
|
store(access_hash_, storer);
|
|
|
|
store(volume_id_, storer);
|
|
|
|
store(secret_, storer);
|
|
|
|
store(local_id_, storer);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
|
|
|
parse(id_, parser);
|
|
|
|
parse(access_hash_, parser);
|
|
|
|
parse(volume_id_, parser);
|
|
|
|
parse(secret_, parser);
|
|
|
|
parse(local_id_, parser);
|
|
|
|
}
|
|
|
|
struct AsKey {
|
|
|
|
const PhotoRemoteFileLocation &key;
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
store(key.id_, storer);
|
|
|
|
store(key.volume_id_, storer);
|
|
|
|
store(key.local_id_, storer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
AsKey as_key() const {
|
|
|
|
return AsKey{*this};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const PhotoRemoteFileLocation &other) const {
|
|
|
|
return std::tie(id_, volume_id_, local_id_) < std::tie(other.id_, other.volume_id_, other.local_id_);
|
|
|
|
}
|
|
|
|
bool operator==(const PhotoRemoteFileLocation &other) const {
|
|
|
|
return std::tie(id_, volume_id_, local_id_) == std::tie(other.id_, other.volume_id_, other.local_id_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &string_builder, const PhotoRemoteFileLocation &location) {
|
|
|
|
return string_builder << "[id = " << location.id_ << ", access_hash = " << location.access_hash_
|
2018-04-11 16:21:24 +02:00
|
|
|
<< ", volume_id = " << location.volume_id_ << ", local_id = " << location.local_id_ << "]";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct WebRemoteFileLocation {
|
|
|
|
string url_;
|
|
|
|
int64 access_hash_;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
store(url_, storer);
|
|
|
|
store(access_hash_, storer);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
|
|
|
parse(url_, parser);
|
|
|
|
parse(access_hash_, parser);
|
|
|
|
}
|
|
|
|
struct AsKey {
|
|
|
|
const WebRemoteFileLocation &key;
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
store(key.url_, storer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
AsKey as_key() const {
|
|
|
|
return AsKey{*this};
|
|
|
|
}
|
|
|
|
bool operator<(const WebRemoteFileLocation &other) const {
|
|
|
|
return url_ < other.url_;
|
|
|
|
}
|
|
|
|
bool operator==(const WebRemoteFileLocation &other) const {
|
|
|
|
return url_ == other.url_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &string_builder, const WebRemoteFileLocation &location) {
|
|
|
|
return string_builder << "[url = " << location.url_ << ", access_hash = " << location.access_hash_ << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CommonRemoteFileLocation {
|
|
|
|
int64 id_;
|
|
|
|
int64 access_hash_;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
store(id_, storer);
|
|
|
|
store(access_hash_, storer);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
|
|
|
parse(id_, parser);
|
|
|
|
parse(access_hash_, parser);
|
|
|
|
}
|
|
|
|
struct AsKey {
|
|
|
|
const CommonRemoteFileLocation &key;
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
td::store(key.id_, storer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
AsKey as_key() const {
|
|
|
|
return AsKey{*this};
|
|
|
|
}
|
|
|
|
bool operator<(const CommonRemoteFileLocation &other) const {
|
|
|
|
return std::tie(id_) < std::tie(other.id_);
|
|
|
|
}
|
|
|
|
bool operator==(const CommonRemoteFileLocation &other) const {
|
|
|
|
return std::tie(id_) == std::tie(other.id_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &string_builder, const CommonRemoteFileLocation &location) {
|
|
|
|
return string_builder << "[id = " << location.id_ << ", access_hash = " << location.access_hash_ << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
class FullRemoteFileLocation {
|
|
|
|
public:
|
2018-01-20 15:57:52 +01:00
|
|
|
FileType file_type_{FileType::None};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr int32 WEB_LOCATION_FLAG = 1 << 24;
|
2018-12-05 10:32:31 +01:00
|
|
|
static constexpr int32 FILE_REFERENCE_FLAG = 1 << 25;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool web_location_flag_{false};
|
|
|
|
DcId dc_id_;
|
2018-12-05 10:32:31 +01:00
|
|
|
std::string file_reference_;
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class LocationType : int32 { Web, Photo, Common, None };
|
2018-12-31 20:04:05 +01:00
|
|
|
Variant<WebRemoteFileLocation, PhotoRemoteFileLocation, CommonRemoteFileLocation> variant_;
|
|
|
|
|
|
|
|
LocationType location_type() const {
|
|
|
|
if (is_web()) {
|
|
|
|
return LocationType::Web;
|
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
switch (file_type_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
case FileType::Photo:
|
|
|
|
case FileType::ProfilePhoto:
|
|
|
|
case FileType::Thumbnail:
|
|
|
|
case FileType::EncryptedThumbnail:
|
|
|
|
case FileType::Wallpaper:
|
|
|
|
return LocationType::Photo;
|
|
|
|
case FileType::Video:
|
|
|
|
case FileType::VoiceNote:
|
|
|
|
case FileType::Document:
|
|
|
|
case FileType::Sticker:
|
|
|
|
case FileType::Audio:
|
|
|
|
case FileType::Animation:
|
|
|
|
case FileType::Encrypted:
|
|
|
|
case FileType::VideoNote:
|
2018-03-27 15:11:15 +02:00
|
|
|
case FileType::SecureRaw:
|
|
|
|
case FileType::Secure:
|
2018-12-31 20:04:05 +01:00
|
|
|
return LocationType::Common;
|
|
|
|
case FileType::None:
|
|
|
|
case FileType::Size:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
case FileType::Temp:
|
|
|
|
return LocationType::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebRemoteFileLocation &web() {
|
|
|
|
return variant_.get<WebRemoteFileLocation>();
|
|
|
|
}
|
|
|
|
PhotoRemoteFileLocation &photo() {
|
|
|
|
return variant_.get<PhotoRemoteFileLocation>();
|
|
|
|
}
|
|
|
|
CommonRemoteFileLocation &common() {
|
|
|
|
return variant_.get<CommonRemoteFileLocation>();
|
|
|
|
}
|
|
|
|
const WebRemoteFileLocation &web() const {
|
|
|
|
return variant_.get<WebRemoteFileLocation>();
|
|
|
|
}
|
|
|
|
const PhotoRemoteFileLocation &photo() const {
|
|
|
|
return variant_.get<PhotoRemoteFileLocation>();
|
|
|
|
}
|
|
|
|
const CommonRemoteFileLocation &common() const {
|
|
|
|
return variant_.get<CommonRemoteFileLocation>();
|
|
|
|
}
|
|
|
|
|
|
|
|
friend StringBuilder &operator<<(StringBuilder &string_builder,
|
|
|
|
const FullRemoteFileLocation &full_remote_file_location);
|
|
|
|
|
2019-01-20 16:26:51 +01:00
|
|
|
int32 key_type() const {
|
|
|
|
auto type = static_cast<int32>(file_type_);
|
|
|
|
if (is_web()) {
|
|
|
|
type |= WEB_LOCATION_FLAG;
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 full_type() const {
|
2018-01-20 15:57:52 +01:00
|
|
|
auto type = static_cast<int32>(file_type_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_web()) {
|
|
|
|
type |= WEB_LOCATION_FLAG;
|
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
if (!file_reference_.empty()) {
|
|
|
|
type |= FILE_REFERENCE_FLAG;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using ::td::store;
|
|
|
|
store(full_type(), storer);
|
|
|
|
store(dc_id_.get_value(), storer);
|
2018-12-05 10:32:31 +01:00
|
|
|
if (!file_reference_.empty()) {
|
|
|
|
store(file_reference_, storer);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
variant_.visit([&](auto &&value) {
|
|
|
|
using td::store;
|
|
|
|
store(value, storer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using ::td::parse;
|
|
|
|
int32 raw_type;
|
|
|
|
parse(raw_type, parser);
|
|
|
|
web_location_flag_ = (raw_type & WEB_LOCATION_FLAG) != 0;
|
|
|
|
raw_type &= ~WEB_LOCATION_FLAG;
|
2018-12-05 10:32:31 +01:00
|
|
|
bool has_file_reference = (raw_type & FILE_REFERENCE_FLAG) != 0;
|
|
|
|
raw_type &= ~FILE_REFERENCE_FLAG;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (raw_type < 0 || raw_type >= static_cast<int32>(FileType::Size)) {
|
|
|
|
return parser.set_error("Invalid FileType in FullRemoteFileLocation");
|
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
file_type_ = static_cast<FileType>(raw_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 dc_id_value;
|
|
|
|
parse(dc_id_value, parser);
|
|
|
|
dc_id_ = DcId::from_value(dc_id_value);
|
|
|
|
|
2018-12-05 10:32:31 +01:00
|
|
|
if (has_file_reference) {
|
|
|
|
parse(file_reference_, parser);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (location_type()) {
|
|
|
|
case LocationType::Web: {
|
|
|
|
variant_ = WebRemoteFileLocation();
|
|
|
|
return web().parse(parser);
|
|
|
|
}
|
|
|
|
case LocationType::Photo: {
|
|
|
|
variant_ = PhotoRemoteFileLocation();
|
|
|
|
return photo().parse(parser);
|
|
|
|
}
|
|
|
|
case LocationType::Common: {
|
|
|
|
variant_ = CommonRemoteFileLocation();
|
|
|
|
return common().parse(parser);
|
|
|
|
}
|
|
|
|
case LocationType::None: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parser.set_error("Invalid FileType in FullRemoteFileLocation");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AsKey {
|
|
|
|
const FullRemoteFileLocation &key;
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
2019-01-20 16:26:51 +01:00
|
|
|
store(key.key_type(), storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
key.variant_.visit([&](auto &&value) {
|
|
|
|
using td::store;
|
|
|
|
store(value.as_key(), storer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
AsKey as_key() const {
|
|
|
|
return AsKey{*this};
|
|
|
|
}
|
|
|
|
|
|
|
|
DcId get_dc_id() const {
|
2018-06-25 23:10:53 +02:00
|
|
|
CHECK(!is_web());
|
2018-12-31 20:04:05 +01:00
|
|
|
return dc_id_;
|
|
|
|
}
|
|
|
|
int64 get_access_hash() const {
|
|
|
|
switch (location_type()) {
|
|
|
|
case LocationType::Photo:
|
|
|
|
return photo().access_hash_;
|
|
|
|
case LocationType::Common:
|
|
|
|
return common().access_hash_;
|
|
|
|
case LocationType::Web:
|
|
|
|
return web().access_hash_;
|
|
|
|
case LocationType::None:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int64 get_id() const {
|
|
|
|
switch (location_type()) {
|
|
|
|
case LocationType::Photo:
|
|
|
|
return photo().id_;
|
|
|
|
case LocationType::Common:
|
|
|
|
return common().id_;
|
|
|
|
case LocationType::Web:
|
|
|
|
case LocationType::None:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
bool delete_file_reference(Slice bad_file_reference) {
|
2019-01-30 17:08:50 +01:00
|
|
|
auto res = FileReferenceView(file_reference_).delete_file_reference(bad_file_reference);
|
|
|
|
if (res.second) {
|
|
|
|
file_reference_ = res.first;
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
2019-01-30 17:08:50 +01:00
|
|
|
return res.second;
|
|
|
|
}
|
|
|
|
bool has_upload_file_reference() const {
|
|
|
|
return FileReferenceView(file_reference_).has_first();
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
2019-01-30 17:08:50 +01:00
|
|
|
bool has_download_file_reference() const {
|
|
|
|
return FileReferenceView(file_reference_).has_second();
|
2018-12-08 23:00:27 +01:00
|
|
|
}
|
2019-01-30 17:08:50 +01:00
|
|
|
bool has_any_file_reference() const {
|
|
|
|
return has_upload_file_reference() || has_download_file_reference();
|
|
|
|
}
|
|
|
|
Slice get_raw_file_reference() const {
|
2018-12-05 10:32:31 +01:00
|
|
|
return file_reference_;
|
|
|
|
}
|
2019-01-30 17:08:50 +01:00
|
|
|
Slice get_upload_file_reference() const {
|
|
|
|
return FileReferenceView(file_reference_).first();
|
|
|
|
}
|
|
|
|
Slice get_download_file_reference() const {
|
|
|
|
return FileReferenceView(file_reference_).second();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
string get_url() const {
|
|
|
|
if (is_web()) {
|
|
|
|
return web().url_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_web() const {
|
|
|
|
return web_location_flag_;
|
|
|
|
}
|
|
|
|
bool is_photo() const {
|
|
|
|
return location_type() == LocationType::Photo;
|
|
|
|
}
|
|
|
|
bool is_common() const {
|
|
|
|
return location_type() == LocationType::Common;
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
bool is_encrypted_secret() const {
|
2018-01-20 15:57:52 +01:00
|
|
|
return file_type_ == FileType::Encrypted;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
bool is_encrypted_secure() const {
|
|
|
|
return file_type_ == FileType::Secure;
|
|
|
|
}
|
|
|
|
bool is_encrypted_any() const {
|
|
|
|
return is_encrypted_secret() || is_encrypted_secure();
|
|
|
|
}
|
|
|
|
bool is_secure() const {
|
|
|
|
return file_type_ == FileType::SecureRaw || file_type_ == FileType::Secure;
|
|
|
|
}
|
|
|
|
bool is_document() const {
|
|
|
|
return is_common() && !is_secure() && !is_encrypted_secret();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-11-06 15:24:32 +01:00
|
|
|
#define as_input_web_file_location() as_input_web_file_location_impl(__FILE__, __LINE__)
|
|
|
|
tl_object_ptr<telegram_api::inputWebFileLocation> as_input_web_file_location_impl(const char *file, int line) const {
|
|
|
|
CHECK(is_web()) << file << ' ' << line;
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<telegram_api::inputWebFileLocation>(web().url_, web().access_hash_);
|
|
|
|
}
|
2018-11-06 15:24:32 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::InputFileLocation> as_input_file_location() const {
|
|
|
|
switch (location_type()) {
|
|
|
|
case LocationType::Photo:
|
2019-01-30 17:08:50 +01:00
|
|
|
return make_tl_object<telegram_api::inputFileLocation>(
|
|
|
|
photo().volume_id_, photo().local_id_, photo().secret_,
|
|
|
|
BufferSlice(FileReferenceView(file_reference_).second()));
|
2018-12-31 20:04:05 +01:00
|
|
|
case LocationType::Common:
|
2018-03-27 15:11:15 +02:00
|
|
|
if (is_encrypted_secret()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<telegram_api::inputEncryptedFileLocation>(common().id_, common().access_hash_);
|
2018-03-27 15:11:15 +02:00
|
|
|
} else if (is_secure()) {
|
|
|
|
return make_tl_object<telegram_api::inputSecureFileLocation>(common().id_, common().access_hash_);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-12-05 10:32:31 +01:00
|
|
|
return make_tl_object<telegram_api::inputDocumentFileLocation>(common().id_, common().access_hash_,
|
|
|
|
BufferSlice(file_reference_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
case LocationType::Web:
|
|
|
|
case LocationType::None:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 15:24:32 +01:00
|
|
|
#define as_input_document() as_input_document_impl(__FILE__, __LINE__)
|
2019-01-23 20:20:48 +01:00
|
|
|
tl_object_ptr<telegram_api::inputDocument> as_input_document_impl(const char *file, int line) const {
|
2018-11-06 15:24:32 +01:00
|
|
|
CHECK(is_common()) << file << ' ' << line;
|
|
|
|
CHECK(is_document()) << file << ' ' << line;
|
2018-12-05 10:32:31 +01:00
|
|
|
return make_tl_object<telegram_api::inputDocument>(common().id_, common().access_hash_,
|
|
|
|
BufferSlice(file_reference_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-11-06 15:24:32 +01:00
|
|
|
#define as_input_photo() as_input_photo_impl(__FILE__, __LINE__)
|
2019-01-23 20:20:48 +01:00
|
|
|
tl_object_ptr<telegram_api::inputPhoto> as_input_photo_impl(const char *file, int line) const {
|
2018-11-06 15:24:32 +01:00
|
|
|
CHECK(is_photo()) << file << ' ' << line;
|
2019-01-30 17:08:50 +01:00
|
|
|
return make_tl_object<telegram_api::inputPhoto>(photo().id_, photo().access_hash_,
|
|
|
|
BufferSlice(FileReferenceView(file_reference_).first()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 20:20:48 +01:00
|
|
|
tl_object_ptr<telegram_api::inputEncryptedFile> as_input_encrypted_file() const {
|
2018-11-06 15:24:32 +01:00
|
|
|
CHECK(is_encrypted_secret());
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<telegram_api::inputEncryptedFile>(common().id_, common().access_hash_);
|
|
|
|
}
|
2018-11-06 15:24:32 +01:00
|
|
|
|
|
|
|
#define as_input_secure_file() as_input_secure_file_impl(__FILE__, __LINE__)
|
2019-01-23 20:20:48 +01:00
|
|
|
tl_object_ptr<telegram_api::inputSecureFile> as_input_secure_file_impl(const char *file, int line) const {
|
2018-11-06 15:24:32 +01:00
|
|
|
CHECK(is_secure()) << file << ' ' << line;
|
2018-03-27 15:11:15 +02:00
|
|
|
return make_tl_object<telegram_api::inputSecureFile>(common().id_, common().access_hash_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// TODO: this constructor is just for immediate unserialize
|
|
|
|
FullRemoteFileLocation() = default;
|
|
|
|
FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, int32 local_id, int64 volume_id, int64 secret,
|
2019-01-30 17:08:50 +01:00
|
|
|
DcId dc_id, std::string upload_file_reference, std::string download_file_reference)
|
2018-01-20 15:57:52 +01:00
|
|
|
: file_type_(file_type)
|
2018-12-31 20:04:05 +01:00
|
|
|
, dc_id_(dc_id)
|
2019-01-30 17:08:50 +01:00
|
|
|
, file_reference_(FileReferenceView::create_two(upload_file_reference, download_file_reference))
|
2018-12-31 20:04:05 +01:00
|
|
|
, variant_(PhotoRemoteFileLocation{id, access_hash, volume_id, secret, local_id}) {
|
|
|
|
CHECK(is_photo());
|
2019-01-30 17:08:50 +01:00
|
|
|
FileReferenceView view(file_reference_);
|
|
|
|
if (!view.has_first() || !view.has_second()) {
|
2019-01-25 15:44:47 +01:00
|
|
|
LOG(ERROR) << "Tried to register file with invalid file reference";
|
|
|
|
file_reference_.clear();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
FullRemoteFileLocation(FileType file_type, int64 id, int64 access_hash, DcId dc_id, std::string file_reference)
|
|
|
|
: file_type_(file_type)
|
|
|
|
, dc_id_(dc_id)
|
|
|
|
, file_reference_(std::move(file_reference))
|
|
|
|
, variant_(CommonRemoteFileLocation{id, access_hash}) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(is_common());
|
2019-01-30 17:08:50 +01:00
|
|
|
FileReferenceView view(file_reference_);
|
|
|
|
if (!view.has_first() || !view.has_second()) {
|
2019-01-25 15:44:47 +01:00
|
|
|
LOG(ERROR) << "Tried to register file with invalid file reference";
|
|
|
|
file_reference_.clear();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-06-25 23:10:53 +02:00
|
|
|
FullRemoteFileLocation(FileType file_type, string url, int64 access_hash)
|
2018-01-20 15:57:52 +01:00
|
|
|
: file_type_(file_type)
|
2018-12-31 20:04:05 +01:00
|
|
|
, web_location_flag_{true}
|
2018-06-25 23:10:53 +02:00
|
|
|
, dc_id_()
|
2018-12-31 20:04:05 +01:00
|
|
|
, variant_(WebRemoteFileLocation{std::move(url), access_hash}) {
|
|
|
|
CHECK(is_web());
|
|
|
|
CHECK(!web().url_.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const FullRemoteFileLocation &other) const {
|
2019-01-20 16:26:51 +01:00
|
|
|
if (key_type() != other.key_type()) {
|
|
|
|
return key_type() < other.key_type();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (dc_id_ != other.dc_id_) {
|
|
|
|
return dc_id_ < other.dc_id_;
|
|
|
|
}
|
|
|
|
switch (location_type()) {
|
|
|
|
case LocationType::Photo:
|
|
|
|
return photo() < other.photo();
|
|
|
|
case LocationType::Common:
|
|
|
|
return common() < other.common();
|
|
|
|
case LocationType::Web:
|
|
|
|
return web() < other.web();
|
|
|
|
case LocationType::None:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool operator==(const FullRemoteFileLocation &other) const {
|
2019-01-20 16:26:51 +01:00
|
|
|
if (key_type() != other.key_type()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (dc_id_ != other.dc_id_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (location_type()) {
|
|
|
|
case LocationType::Photo:
|
|
|
|
return photo() == other.photo();
|
|
|
|
case LocationType::Common:
|
|
|
|
return common() == other.common();
|
|
|
|
case LocationType::Web:
|
|
|
|
return web() == other.web();
|
|
|
|
case LocationType::None:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int32 KEY_MAGIC = 0x64374632;
|
2019-01-30 17:08:50 +01:00
|
|
|
}; // namespace td
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &string_builder,
|
|
|
|
const FullRemoteFileLocation &full_remote_file_location) {
|
2018-11-15 22:25:08 +01:00
|
|
|
string_builder << "[" << full_remote_file_location.file_type_;
|
2018-06-25 23:10:53 +02:00
|
|
|
if (!full_remote_file_location.is_web()) {
|
|
|
|
string_builder << ", " << full_remote_file_location.get_dc_id();
|
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
if (!full_remote_file_location.file_reference_.empty()) {
|
2018-12-08 23:00:27 +01:00
|
|
|
string_builder << ", " << tag("file_reference", base64_encode(full_remote_file_location.file_reference_));
|
2018-12-05 10:32:31 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-06-25 23:10:53 +02:00
|
|
|
string_builder << ", location = ";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (full_remote_file_location.is_web()) {
|
|
|
|
string_builder << full_remote_file_location.web();
|
|
|
|
} else if (full_remote_file_location.is_photo()) {
|
|
|
|
string_builder << full_remote_file_location.photo();
|
|
|
|
} else if (full_remote_file_location.is_common()) {
|
|
|
|
string_builder << full_remote_file_location.common();
|
|
|
|
}
|
|
|
|
|
|
|
|
return string_builder << "]";
|
|
|
|
}
|
|
|
|
|
2018-01-20 15:06:22 +01:00
|
|
|
class RemoteFileLocation {
|
|
|
|
public:
|
|
|
|
enum class Type : int32 { Empty, Partial, Full };
|
2018-01-20 15:57:52 +01:00
|
|
|
|
|
|
|
Type type() const {
|
2018-01-20 16:16:51 +01:00
|
|
|
return static_cast<Type>(variant_.get_offset());
|
2018-01-20 15:57:52 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
2018-01-20 16:16:51 +01:00
|
|
|
storer.store_int(variant_.get_offset());
|
2018-12-31 20:04:05 +01:00
|
|
|
bool ok{false};
|
|
|
|
variant_.visit([&](auto &&value) {
|
|
|
|
using td::store;
|
|
|
|
store(value, storer);
|
|
|
|
ok = true;
|
|
|
|
});
|
|
|
|
CHECK(ok);
|
|
|
|
}
|
|
|
|
PartialRemoteFileLocation &partial() {
|
|
|
|
return variant_.get<1>();
|
|
|
|
}
|
|
|
|
FullRemoteFileLocation &full() {
|
|
|
|
return variant_.get<2>();
|
|
|
|
}
|
|
|
|
const PartialRemoteFileLocation &partial() const {
|
|
|
|
return variant_.get<1>();
|
|
|
|
}
|
|
|
|
const FullRemoteFileLocation &full() const {
|
|
|
|
return variant_.get<2>();
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
2018-01-20 16:16:51 +01:00
|
|
|
auto type = static_cast<Type>(parser.fetch_int());
|
|
|
|
switch (type) {
|
2018-12-27 02:36:20 +01:00
|
|
|
case Type::Empty:
|
2018-12-31 20:04:05 +01:00
|
|
|
variant_ = EmptyRemoteFileLocation();
|
2018-01-20 15:06:22 +01:00
|
|
|
return;
|
2018-12-27 02:36:20 +01:00
|
|
|
case Type::Partial:
|
2018-12-31 20:04:05 +01:00
|
|
|
variant_ = PartialRemoteFileLocation();
|
|
|
|
return partial().parse(parser);
|
2018-12-27 02:36:20 +01:00
|
|
|
case Type::Full:
|
2018-12-31 20:04:05 +01:00
|
|
|
variant_ = FullRemoteFileLocation();
|
|
|
|
return full().parse(parser);
|
|
|
|
}
|
|
|
|
parser.set_error("Invalid type in RemoteFileLocation");
|
|
|
|
}
|
|
|
|
|
2018-01-20 16:16:51 +01:00
|
|
|
RemoteFileLocation() : variant_{EmptyRemoteFileLocation{}} {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-20 16:16:51 +01:00
|
|
|
explicit RemoteFileLocation(const FullRemoteFileLocation &full) : variant_(full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-20 16:16:51 +01:00
|
|
|
explicit RemoteFileLocation(const PartialRemoteFileLocation &partial) : variant_(partial) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
RemoteFileLocation(FileType file_type, int64 id, int64 access_hash, int32 local_id, int64 volume_id, int64 secret,
|
2019-01-30 17:08:50 +01:00
|
|
|
DcId dc_id, std::string upload_file_reference, std::string download_file_reference)
|
2018-12-05 10:32:31 +01:00
|
|
|
: variant_(FullRemoteFileLocation{file_type, id, access_hash, local_id, volume_id, secret, dc_id,
|
2019-01-30 17:08:50 +01:00
|
|
|
std::move(upload_file_reference), std::move(download_file_reference)}) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
RemoteFileLocation(FileType file_type, int64 id, int64 access_hash, DcId dc_id, std::string file_reference)
|
|
|
|
: variant_(FullRemoteFileLocation{file_type, id, access_hash, dc_id, std::move(file_reference)}) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-20 15:17:58 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Variant<EmptyRemoteFileLocation, PartialRemoteFileLocation, FullRemoteFileLocation> variant_;
|
|
|
|
|
|
|
|
friend bool operator==(const RemoteFileLocation &lhs, const RemoteFileLocation &rhs);
|
2018-12-27 02:36:20 +01:00
|
|
|
|
|
|
|
bool is_empty() const {
|
|
|
|
switch (type()) {
|
|
|
|
case Type::Empty:
|
|
|
|
return true;
|
|
|
|
case Type::Partial:
|
|
|
|
return partial().ready_part_count_ == 0;
|
|
|
|
case Type::Full:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const RemoteFileLocation &lhs, const RemoteFileLocation &rhs) {
|
2018-12-27 02:36:20 +01:00
|
|
|
if (lhs.is_empty() && rhs.is_empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return lhs.variant_ == rhs.variant_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const RemoteFileLocation &lhs, const RemoteFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EmptyLocalFileLocation {
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const EmptyLocalFileLocation &lhs, const EmptyLocalFileLocation &rhs) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const EmptyLocalFileLocation &lhs, const EmptyLocalFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PartialLocalFileLocation {
|
2018-01-20 15:57:52 +01:00
|
|
|
FileType file_type_;
|
2018-01-20 15:06:22 +01:00
|
|
|
int32 part_size_;
|
2018-12-26 17:11:15 +01:00
|
|
|
string path_;
|
2018-12-31 20:04:05 +01:00
|
|
|
string iv_;
|
2018-11-11 12:38:04 +01:00
|
|
|
string ready_bitmask_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
2018-01-20 15:57:52 +01:00
|
|
|
store(file_type_, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
store(path_, storer);
|
|
|
|
store(part_size_, storer);
|
2018-11-11 12:38:04 +01:00
|
|
|
int32 deprecated_ready_part_count = -1;
|
|
|
|
store(deprecated_ready_part_count, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
store(iv_, storer);
|
2018-11-11 12:38:04 +01:00
|
|
|
store(ready_bitmask_, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
2018-01-20 15:57:52 +01:00
|
|
|
parse(file_type_, parser);
|
|
|
|
if (file_type_ < FileType::Thumbnail || file_type_ >= FileType::Size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return parser.set_error("Invalid type in PartialLocalFileLocation");
|
|
|
|
}
|
|
|
|
parse(path_, parser);
|
|
|
|
parse(part_size_, parser);
|
2018-11-11 12:38:04 +01:00
|
|
|
int32 deprecated_ready_part_count;
|
|
|
|
parse(deprecated_ready_part_count, parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
parse(iv_, parser);
|
2018-11-11 12:38:04 +01:00
|
|
|
if (deprecated_ready_part_count == -1) {
|
|
|
|
parse(ready_bitmask_, parser);
|
|
|
|
} else {
|
2018-12-26 17:11:15 +01:00
|
|
|
CHECK(0 <= deprecated_ready_part_count);
|
|
|
|
CHECK(deprecated_ready_part_count <= (1 << 22));
|
2018-11-11 12:38:04 +01:00
|
|
|
ready_bitmask_ = Bitmask(Bitmask::Ones{}, deprecated_ready_part_count).encode();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const PartialLocalFileLocation &lhs, const PartialLocalFileLocation &rhs) {
|
2018-01-20 15:57:52 +01:00
|
|
|
return lhs.file_type_ == rhs.file_type_ && lhs.path_ == rhs.path_ && lhs.part_size_ == rhs.part_size_ &&
|
2018-11-11 12:38:04 +01:00
|
|
|
lhs.iv_ == rhs.iv_ && lhs.ready_bitmask_ == rhs.ready_bitmask_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const PartialLocalFileLocation &lhs, const PartialLocalFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2018-12-27 18:46:28 +01:00
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const PartialLocalFileLocation &location) {
|
|
|
|
return sb << "[partial local location of " << location.file_type_ << " with part size " << location.part_size_
|
|
|
|
<< " and ready parts " << Bitmask(Bitmask::Decode{}, location.ready_bitmask_) << "] at \"" << location.path_
|
|
|
|
<< '"';
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct FullLocalFileLocation {
|
2018-01-20 15:57:52 +01:00
|
|
|
FileType file_type_;
|
2018-12-31 20:04:05 +01:00
|
|
|
string path_;
|
|
|
|
uint64 mtime_nsec_;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
2018-01-20 15:57:52 +01:00
|
|
|
store(file_type_, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
store(mtime_nsec_, storer);
|
|
|
|
store(path_, storer);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
2018-01-20 15:57:52 +01:00
|
|
|
parse(file_type_, parser);
|
|
|
|
if (file_type_ < FileType::Thumbnail || file_type_ >= FileType::Size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return parser.set_error("Invalid type in FullLocalFileLocation");
|
|
|
|
}
|
|
|
|
parse(mtime_nsec_, parser);
|
|
|
|
parse(path_, parser);
|
|
|
|
}
|
|
|
|
const FullLocalFileLocation &as_key() const {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove this constructor
|
2018-01-20 15:57:52 +01:00
|
|
|
FullLocalFileLocation() : file_type_(FileType::Photo) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
FullLocalFileLocation(FileType file_type, string path, uint64 mtime_nsec)
|
2018-01-20 15:57:52 +01:00
|
|
|
: file_type_(file_type), path_(std::move(path)), mtime_nsec_(mtime_nsec) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const int32 KEY_MAGIC = 0x84373817;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator<(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) {
|
2018-01-20 15:57:52 +01:00
|
|
|
return std::tie(lhs.file_type_, lhs.mtime_nsec_, lhs.path_) < std::tie(rhs.file_type_, rhs.mtime_nsec_, rhs.path_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) {
|
2018-01-20 15:57:52 +01:00
|
|
|
return std::tie(lhs.file_type_, lhs.mtime_nsec_, lhs.path_) == std::tie(rhs.file_type_, rhs.mtime_nsec_, rhs.path_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const FullLocalFileLocation &lhs, const FullLocalFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const FullLocalFileLocation &location) {
|
2019-01-01 18:35:08 +01:00
|
|
|
return sb << "[full local location of " << location.file_type_ << "] at \"" << location.path_ << '"';
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 19:17:08 +01:00
|
|
|
struct PartialLocalFileLocationPtr {
|
|
|
|
unique_ptr<PartialLocalFileLocation> location_; // must never be equal to nullptr
|
|
|
|
|
|
|
|
PartialLocalFileLocationPtr() : location_(make_unique<PartialLocalFileLocation>()) {
|
|
|
|
}
|
|
|
|
explicit PartialLocalFileLocationPtr(PartialLocalFileLocation location)
|
|
|
|
: location_(make_unique<PartialLocalFileLocation>(location)) {
|
|
|
|
}
|
|
|
|
PartialLocalFileLocationPtr(const PartialLocalFileLocationPtr &other)
|
|
|
|
: location_(make_unique<PartialLocalFileLocation>(*other.location_)) {
|
|
|
|
}
|
|
|
|
PartialLocalFileLocationPtr &operator=(const PartialLocalFileLocationPtr &other) {
|
|
|
|
*location_ = *other.location_;
|
2019-01-02 19:52:44 +01:00
|
|
|
return *this;
|
2019-01-02 19:17:08 +01:00
|
|
|
}
|
|
|
|
PartialLocalFileLocationPtr(PartialLocalFileLocationPtr &&other)
|
|
|
|
: location_(make_unique<PartialLocalFileLocation>(std::move(*other.location_))) {
|
|
|
|
}
|
|
|
|
PartialLocalFileLocationPtr &operator=(PartialLocalFileLocationPtr &&other) {
|
|
|
|
*location_ = std::move(*other.location_);
|
2019-01-02 19:52:44 +01:00
|
|
|
return *this;
|
2019-01-02 19:17:08 +01:00
|
|
|
}
|
|
|
|
~PartialLocalFileLocationPtr() = default;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
td::store(*location_, storer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const PartialLocalFileLocationPtr &lhs, const PartialLocalFileLocationPtr &rhs) {
|
|
|
|
return *lhs.location_ == *rhs.location_;
|
|
|
|
}
|
|
|
|
|
2018-01-20 15:06:22 +01:00
|
|
|
class LocalFileLocation {
|
|
|
|
public:
|
2018-12-31 20:04:05 +01:00
|
|
|
enum class Type : int32 { Empty, Partial, Full };
|
2018-01-20 15:57:52 +01:00
|
|
|
|
|
|
|
Type type() const {
|
2018-01-20 16:16:51 +01:00
|
|
|
return static_cast<Type>(variant_.get_offset());
|
2018-01-20 15:57:52 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
PartialLocalFileLocation &partial() {
|
2019-01-02 19:17:08 +01:00
|
|
|
return *variant_.get<1>().location_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
FullLocalFileLocation &full() {
|
|
|
|
return variant_.get<2>();
|
|
|
|
}
|
|
|
|
const PartialLocalFileLocation &partial() const {
|
2019-01-02 19:17:08 +01:00
|
|
|
return *variant_.get<1>().location_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
const FullLocalFileLocation &full() const {
|
|
|
|
return variant_.get<2>();
|
|
|
|
}
|
|
|
|
|
2018-02-19 23:28:06 +01:00
|
|
|
CSlice file_name() const {
|
|
|
|
switch (type()) {
|
|
|
|
case Type::Partial:
|
|
|
|
return partial().path_;
|
|
|
|
case Type::Full:
|
|
|
|
return full().path_;
|
|
|
|
case Type::Empty:
|
|
|
|
default:
|
|
|
|
return CSlice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
2018-01-20 16:16:51 +01:00
|
|
|
store(variant_.get_offset(), storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
variant_.visit([&](auto &&value) {
|
|
|
|
using td::store;
|
|
|
|
store(value, storer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
2018-01-20 16:16:51 +01:00
|
|
|
auto type = static_cast<Type>(parser.fetch_int());
|
|
|
|
switch (type) {
|
2018-12-31 20:04:05 +01:00
|
|
|
case Type::Empty:
|
|
|
|
variant_ = EmptyLocalFileLocation();
|
2018-01-20 15:06:22 +01:00
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
case Type::Partial:
|
2019-01-02 19:17:08 +01:00
|
|
|
variant_ = PartialLocalFileLocationPtr();
|
2018-12-31 20:04:05 +01:00
|
|
|
return parse(partial(), parser);
|
|
|
|
case Type::Full:
|
|
|
|
variant_ = FullLocalFileLocation();
|
|
|
|
return parse(full(), parser);
|
|
|
|
}
|
|
|
|
return parser.set_error("Invalid type in LocalFileLocation");
|
|
|
|
}
|
|
|
|
|
2018-01-20 16:16:51 +01:00
|
|
|
LocalFileLocation() : variant_{EmptyLocalFileLocation()} {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-05 10:32:31 +01:00
|
|
|
explicit LocalFileLocation(const PartialLocalFileLocation &partial) : variant_(PartialLocalFileLocationPtr(partial)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-20 16:16:51 +01:00
|
|
|
explicit LocalFileLocation(const FullLocalFileLocation &full) : variant_(full) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
LocalFileLocation(FileType file_type, string path, uint64 mtime_nsec)
|
2018-01-20 16:16:51 +01:00
|
|
|
: variant_(FullLocalFileLocation{file_type, std::move(path), mtime_nsec}) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-20 15:17:58 +01:00
|
|
|
|
|
|
|
private:
|
2019-01-02 19:17:08 +01:00
|
|
|
Variant<EmptyLocalFileLocation, PartialLocalFileLocationPtr, FullLocalFileLocation> variant_;
|
2018-01-20 15:17:58 +01:00
|
|
|
|
|
|
|
friend bool operator==(const LocalFileLocation &lhs, const LocalFileLocation &rhs);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const LocalFileLocation &lhs, const LocalFileLocation &rhs) {
|
|
|
|
return lhs.variant_ == rhs.variant_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const LocalFileLocation &lhs, const LocalFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2019-01-01 18:02:13 +01:00
|
|
|
inline StringBuilder &operator<<(StringBuilder &sb, const LocalFileLocation &location) {
|
|
|
|
switch (location.type()) {
|
|
|
|
case LocalFileLocation::Type::Empty:
|
|
|
|
return sb << "[empty local location]";
|
|
|
|
case LocalFileLocation::Type::Partial:
|
|
|
|
return sb << location.partial();
|
|
|
|
case LocalFileLocation::Type::Full:
|
|
|
|
return sb << location.full();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct FullGenerateFileLocation {
|
2018-01-20 15:57:52 +01:00
|
|
|
FileType file_type_{FileType::None};
|
2018-12-31 20:04:05 +01:00
|
|
|
string original_path_;
|
|
|
|
string conversion_;
|
|
|
|
static const int32 KEY_MAGIC = 0x8b60a1c8;
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
2018-01-20 15:57:52 +01:00
|
|
|
store(file_type_, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
store(original_path_, storer);
|
|
|
|
store(conversion_, storer);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
2018-01-20 15:57:52 +01:00
|
|
|
parse(file_type_, parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
parse(original_path_, parser);
|
|
|
|
parse(conversion_, parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
const FullGenerateFileLocation &as_key() const {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
FullGenerateFileLocation() = default;
|
2018-01-20 15:57:52 +01:00
|
|
|
FullGenerateFileLocation(FileType file_type, string original_path, string conversion)
|
|
|
|
: file_type_(file_type), original_path_(std::move(original_path)), conversion_(std::move(conversion)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator<(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) {
|
2018-01-20 15:57:52 +01:00
|
|
|
return std::tie(lhs.file_type_, lhs.original_path_, lhs.conversion_) <
|
|
|
|
std::tie(rhs.file_type_, rhs.original_path_, rhs.conversion_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) {
|
2018-01-20 15:57:52 +01:00
|
|
|
return std::tie(lhs.file_type_, lhs.original_path_, lhs.conversion_) ==
|
|
|
|
std::tie(rhs.file_type_, rhs.original_path_, rhs.conversion_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const FullGenerateFileLocation &lhs, const FullGenerateFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &string_builder,
|
|
|
|
const FullGenerateFileLocation &full_generated_file_location) {
|
2018-11-15 22:25:08 +01:00
|
|
|
return string_builder << '[' << tag("file_type", full_generated_file_location.file_type_)
|
2018-12-31 20:04:05 +01:00
|
|
|
<< tag("original_path", full_generated_file_location.original_path_)
|
2018-11-15 22:25:08 +01:00
|
|
|
<< tag("conversion", full_generated_file_location.conversion_) << ']';
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 15:06:22 +01:00
|
|
|
class GenerateFileLocation {
|
|
|
|
public:
|
2018-12-31 20:04:05 +01:00
|
|
|
enum class Type : int32 { Empty, Full };
|
2018-01-20 15:57:52 +01:00
|
|
|
|
|
|
|
Type type() const {
|
|
|
|
return type_;
|
|
|
|
}
|
2018-01-20 15:06:22 +01:00
|
|
|
|
|
|
|
FullGenerateFileLocation &full() {
|
|
|
|
CHECK(type_ == Type::Full);
|
|
|
|
return full_;
|
|
|
|
}
|
|
|
|
const FullGenerateFileLocation &full() const {
|
|
|
|
CHECK(type_ == Type::Full);
|
|
|
|
return full_;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
2018-01-20 15:06:22 +01:00
|
|
|
td::store(type_, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (type_) {
|
|
|
|
case Type::Empty:
|
2018-01-20 15:06:22 +01:00
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
case Type::Full:
|
2018-01-20 15:06:22 +01:00
|
|
|
return td::store(full_, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-20 15:06:22 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
2018-01-20 15:06:22 +01:00
|
|
|
td::parse(type_, parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (type_) {
|
|
|
|
case Type::Empty:
|
2018-01-20 15:06:22 +01:00
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
case Type::Full:
|
2018-01-20 15:06:22 +01:00
|
|
|
return td::parse(full_, parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-20 15:06:22 +01:00
|
|
|
return parser.set_error("Invalid type in GenerateFileLocation");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GenerateFileLocation() : type_(Type::Empty) {
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit GenerateFileLocation(const FullGenerateFileLocation &full) : type_(Type::Full), full_(full) {
|
|
|
|
}
|
2018-01-20 15:06:22 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
GenerateFileLocation(FileType file_type, string original_path, string conversion)
|
2018-01-20 15:06:22 +01:00
|
|
|
: type_(Type::Full), full_{file_type, std::move(original_path), std::move(conversion)} {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-20 15:06:22 +01:00
|
|
|
|
|
|
|
private:
|
2018-01-20 15:57:52 +01:00
|
|
|
Type type_;
|
2018-01-20 15:06:22 +01:00
|
|
|
FullGenerateFileLocation full_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const GenerateFileLocation &lhs, const GenerateFileLocation &rhs) {
|
2018-01-20 15:57:52 +01:00
|
|
|
if (lhs.type() != rhs.type()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-20 15:57:52 +01:00
|
|
|
switch (lhs.type()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
case GenerateFileLocation::Type::Empty:
|
2018-01-20 15:06:22 +01:00
|
|
|
return true;
|
2018-12-31 20:04:05 +01:00
|
|
|
case GenerateFileLocation::Type::Full:
|
2018-01-20 15:06:22 +01:00
|
|
|
return lhs.full() == rhs.full();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const GenerateFileLocation &lhs, const GenerateFileLocation &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|