Move guess_file_type_by_path to FileType.cpp.

This commit is contained in:
levlam 2023-09-11 16:34:00 +03:00
parent 468f92d0e5
commit 622762f6ef
3 changed files with 37 additions and 28 deletions

View File

@ -3504,34 +3504,6 @@ FileType FileManager::guess_file_type(const tl_object_ptr<td_api::InputFile> &fi
return FileType::Temp;
}
auto guess_file_type_by_path = [](const string &file_path) {
PathView path_view(file_path);
auto file_name = path_view.file_name();
auto extension = path_view.extension();
if (extension == "jpg" || extension == "jpeg") {
return FileType::Photo;
}
if (extension == "ogg" || extension == "oga" || extension == "opus") {
return FileType::VoiceNote;
}
if (extension == "3gp" || extension == "mov") {
return FileType::Video;
}
if (extension == "mp3" || extension == "mpeg3" || extension == "m4a") {
return FileType::Audio;
}
if (extension == "webp" || extension == "tgs" || extension == "webm") {
return FileType::Sticker;
}
if (extension == "gif") {
return FileType::Animation;
}
if (extension == "mp4" || extension == "mpeg4") {
return to_lower(file_name).find("-gif-") != string::npos ? FileType::Animation : FileType::Video;
}
return FileType::Document;
};
switch (file->get_id()) {
case td_api::inputFileLocal::ID:
return guess_file_type_by_path(static_cast<const td_api::inputFileLocal *>(file.get())->path_);

View File

@ -6,6 +6,9 @@
//
#include "td/telegram/files/FileType.h"
#include "td/utils/misc.h"
#include "td/utils/PathView.h"
namespace td {
FileType get_file_type(const td_api::FileType &file_type) {
@ -315,4 +318,36 @@ bool can_reuse_remote_file(FileType file_type) {
}
}
FileType guess_file_type_by_path(Slice file_path, FileType default_file_type) {
if (default_file_type != FileType::None) {
return default_file_type;
}
PathView path_view(file_path);
auto file_name = path_view.file_name();
auto extension = path_view.extension();
if (extension == "jpg" || extension == "jpeg") {
return FileType::Photo;
}
if (extension == "ogg" || extension == "oga" || extension == "opus") {
return FileType::VoiceNote;
}
if (extension == "3gp" || extension == "mov") {
return FileType::Video;
}
if (extension == "mp3" || extension == "mpeg3" || extension == "m4a") {
return FileType::Audio;
}
if (extension == "webp" || extension == "tgs" || extension == "webm") {
return FileType::Sticker;
}
if (extension == "gif") {
return FileType::Animation;
}
if (extension == "mp4" || extension == "mpeg4") {
return to_lower(file_name).find("-gif-") != string::npos ? FileType::Animation : FileType::Video;
}
return FileType::Document;
}
} // namespace td

View File

@ -67,4 +67,6 @@ bool is_file_big(FileType file_type, int64 expected_size);
bool can_reuse_remote_file(FileType file_type);
FileType guess_file_type_by_path(Slice file_path, FileType default_file_type = FileType::None);
} // namespace td