Better file_type_name.

GitOrigin-RevId: d3c0c0b88e9dec5348fa72c66d7403507e493e18
This commit is contained in:
levlam 2019-01-20 01:59:37 +03:00
parent e63d5ec20f
commit 73996c6efe
9 changed files with 62 additions and 20 deletions

View File

@ -158,31 +158,26 @@ Result<FullLocalFileLocation> save_file_bytes(FileType type, BufferSlice bytes,
return FullLocalFileLocation(type, std::move(perm_path), 0);
}
const char *file_type_name[file_type_size] = {"thumbnails", "profile_photos", "photos", "voice",
"videos", "documents", "secret", "temp",
"stickers", "music", "animations", "secret_thumbnails",
"wallpapers", "video_notes", "passport", "passport"};
string get_file_base_dir(const FileDirType &file_dir_type) {
static Slice get_file_base_dir(const FileDirType &file_dir_type) {
switch (file_dir_type) {
case FileDirType::Secure:
return G()->get_dir().str();
return G()->get_dir();
case FileDirType::Common:
return G()->get_files_dir().str();
return G()->get_files_dir();
default:
UNREACHABLE();
return "";
return Slice();
}
}
string get_files_base_dir(FileType file_type) {
Slice get_files_base_dir(FileType file_type) {
return get_file_base_dir(get_file_dir_type(file_type));
}
string get_files_temp_dir(FileType file_type) {
return get_files_base_dir(file_type) + "temp" + TD_DIR_SLASH;
return PSTRING() << get_files_base_dir(file_type) << "temp" << TD_DIR_SLASH;
}
string get_files_dir(FileType file_type) {
return get_files_base_dir(file_type) + file_type_name[static_cast<int32>(file_type)] + TD_DIR_SLASH;
return PSTRING() << get_files_base_dir(file_type) << get_file_type_name(file_type) << TD_DIR_SLASH;
}
} // namespace td

View File

@ -27,7 +27,7 @@ Result<string> search_file(CSlice dir, CSlice name, int64 expected_size) TD_WARN
Result<FullLocalFileLocation> save_file_bytes(FileType type, BufferSlice bytes, CSlice file_name);
string get_files_base_dir(FileType file_type);
Slice get_files_base_dir(FileType file_type);
string get_files_temp_dir(FileType file_type);

View File

@ -858,7 +858,8 @@ Result<FileId> FileManager::register_file(FileData &&data, FileLocationSource fi
if (file_location_source == FileLocationSource::FromDb) {
PathView path_view(data.local_.full().path_);
if (path_view.is_relative()) {
data.local_.full().path_ = get_files_base_dir(data.local_.full().file_type_) + data.local_.full().path_;
data.local_.full().path_ = PSTRING()
<< get_files_base_dir(data.local_.full().file_type_) << data.local_.full().path_;
}
}

View File

@ -201,7 +201,7 @@ StringBuilder &operator<<(StringBuilder &sb, const FileStats &file_stats) {
sb << "[FileStat " << tag("total", total_stat);
for (int32 i = 0; i < file_type_size; i++) {
sb << tag(Slice(file_type_name[i]), file_stats.stat_by_type[i]);
sb << tag(get_file_type_name(FileType(i)), file_stats.stat_by_type[i]);
}
sb << "]";
} else {
@ -224,7 +224,7 @@ StringBuilder &operator<<(StringBuilder &sb, const FileStats &file_stats) {
sb << "[FileStat " << tag("owner_dialog_id", by_type.first) << tag("total", dialog_stat);
for (int32 i = 0; i < file_type_size; i++) {
sb << tag(Slice(file_type_name[i]), by_type.second[i]);
sb << tag(get_file_type_name(FileType(i)), by_type.second[i]);
}
sb << "]";
}

View File

@ -71,7 +71,7 @@ Status scan_db(CallbackT &&callback) {
}
PathView path_view(info.path);
if (path_view.is_relative()) {
info.path = get_files_base_dir(info.file_type) + info.path;
info.path = PSTRING() << get_files_base_dir(info.file_type) << info.path;
}
// LOG(INFO) << "Found file in the database: " << data << " " << info.path;
info.owner_dialog_id = data.owner_dialog_id_;

View File

@ -9,6 +9,7 @@
#include "td/telegram/td_api.h"
#include "td/utils/logging.h"
#include "td/utils/Slice.h"
namespace td {
@ -117,10 +118,51 @@ inline tl_object_ptr<td_api::FileType> as_td_api(FileType file_type) {
}
constexpr int32 file_type_size = static_cast<int32>(FileType::Size);
extern const char *file_type_name[file_type_size];
inline CSlice get_file_type_name(FileType file_type) {
switch (file_type) {
case FileType::Thumbnail:
return CSlice("thumbnails");
case FileType::ProfilePhoto:
return CSlice("profile_photos");
case FileType::Photo:
return CSlice("photos");
case FileType::VoiceNote:
return CSlice("voice");
case FileType::Video:
return CSlice("videos");
case FileType::Document:
return CSlice("documents");
case FileType::Encrypted:
return CSlice("secret");
case FileType::Temp:
return CSlice("temp");
case FileType::Sticker:
return CSlice("stickers");
case FileType::Audio:
return CSlice("music");
case FileType::Animation:
return CSlice("animations");
case FileType::EncryptedThumbnail:
return CSlice("secret_thumbnails");
case FileType::Wallpaper:
return CSlice("wallpapers");
case FileType::VideoNote:
return CSlice("video_notes");
case FileType::SecureRaw:
return CSlice("passport");
case FileType::Secure:
return CSlice("passport");
case FileType::Size:
case FileType::None:
default:
UNREACHABLE();
return CSlice("none");
}
}
inline StringBuilder &operator<<(StringBuilder &string_builder, FileType file_type) {
return string_builder << file_type_name[static_cast<int32>(file_type)];
return string_builder << get_file_type_name(file_type);
}
enum class FileDirType : int8 { Secure, Common };

View File

@ -127,7 +127,8 @@ class NetStatsManager : public Actor {
f(media_net_stats_, 1, CSlice("media"), FileType::None);
for (int32 file_type_i = 0; file_type_i < file_type_size; file_type_i++) {
auto &stat = files_stats_[file_type_i];
f(stat, file_type_i + 2, CSlice(file_type_name[file_type_i]), FileType(file_type_i));
auto file_type = static_cast<FileType>(file_type_i);
f(stat, file_type_i + 2, get_file_type_name(file_type), file_type);
}
f(call_net_stats_, call_net_stats_id_, CSlice("calls"), FileType::None);
}

View File

@ -16,6 +16,7 @@
#include <limits>
namespace td {
class SliceStorer : public Storer {
Slice slice;

View File

@ -57,6 +57,7 @@
while (false)
namespace td {
template <class StorerT>
void store(bool x, StorerT &storer) {
storer.store_binary(static_cast<int32>(x));
@ -215,4 +216,5 @@ TD_WARN_UNUSED_RESULT Status unserialize(T &object, Slice data) {
parser.fetch_end();
return parser.get_status();
}
} // namespace td