Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimisation of directory type checking in file list #1381

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 34 additions & 42 deletions src/FileList/FileListHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,28 @@ void FileListHandler::GetFileList(CARTA::FileListResponse& file_list_response, c
auto& file_info = *file_list_response.add_files();
FillRegionFileInfo(file_info, full_path, file_type, false);
}
} else if (cc_file.isDirectory(true) && cc_file.isExecutable() &&
(list_all_files || CasacoreImageType(full_path) == casacore::ImageOpener::UNKNOWN)) {
// Add directory: not image if checking type, or not checking type
casacore::String dir_name(cc_file.path().baseName());
auto directory_info = file_list_response.add_subdirectories();
directory_info->set_name(dir_name);
directory_info->set_date(cc_file.modifyTime());
directory_info->set_item_count(GetNumItems(cc_file.path().absoluteName()));
} else if (cc_file.isDirectory(true) && cc_file.isExecutable()) {
bool add_directory(false);

if (list_all_files) {
// No filtering; show all directories
add_directory = true;
} else {
// Determine if image or directory
auto file_type = GuessImageDirectoryType(full_path, filter_mode == CARTA::Content);
// This is a normal directory
if (file_type == CARTA::UNKNOWN) {
add_directory = true;
}
}

if (add_directory) {
casacore::String dir_name(cc_file.path().baseName());
auto directory_info = file_list_response.add_subdirectories();
directory_info->set_name(dir_name);
directory_info->set_date(cc_file.modifyTime());
directory_info->set_item_count(GetNumItems(cc_file.path().absoluteName()));
}
}
} else {
// Image list
Expand All @@ -173,40 +187,18 @@ void FileListHandler::GetFileList(CARTA::FileListResponse& file_list_response, c

if (cc_file.isDirectory(true) && cc_file.isExecutable()) {
// Determine if image or directory for image list
auto image_type = CasacoreImageType(full_path);

switch (image_type) {
case casacore::ImageOpener::AIPSPP:
case casacore::ImageOpener::IMAGECONCAT:
case casacore::ImageOpener::IMAGEEXPR:
case casacore::ImageOpener::COMPLISTIMAGE: {
file_type = CARTA::FileType::CASA;
add_image_file = true;
break;
}
case casacore::ImageOpener::GIPSY:
case casacore::ImageOpener::CAIPS:
case casacore::ImageOpener::NEWSTAR: {
std::string image_type_msg = fmt::format("{}: image type not supported", name);
result_msg = {image_type_msg, {"file_list"}, CARTA::ErrorSeverity::DEBUG};
break;
}
case casacore::ImageOpener::MIRIAD: {
file_type = CARTA::FileType::MIRIAD;
add_image_file = true;
break;
}
case casacore::ImageOpener::UNKNOWN: {
// UNKNOWN directories are directories
casacore::String dir_name(cc_file.path().baseName());
auto directory_info = file_list_response.add_subdirectories();
directory_info->set_name(dir_name);
directory_info->set_date(cc_file.modifyTime());
directory_info->set_item_count(GetNumItems(cc_file.path().absoluteName()));
break;
}
default:
break;
file_type = GuessImageDirectoryType(full_path, filter_mode == CARTA::Content);

if (file_type == CARTA::UNKNOWN) {
// This is a normal directory
casacore::String dir_name(cc_file.path().baseName());
auto directory_info = file_list_response.add_subdirectories();
directory_info->set_name(dir_name);
directory_info->set_date(cc_file.modifyTime());
directory_info->set_item_count(GetNumItems(cc_file.path().absoluteName()));
} else {
// This is a known directory image type
add_image_file = true;
}
} else if (cc_file.isRegular(true)) {
file_type = GuessImageType(full_path, filter_mode == CARTA::Content);
Expand Down
33 changes: 33 additions & 0 deletions src/Util/File.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <spdlog/fmt/fmt.h>
#include <fstream>

#include "Casacore.h"
#include "String.h"

uint32_t GetMagicNumber(const std::string& filename) {
Expand Down Expand Up @@ -101,6 +102,38 @@ CARTA::FileType GuessImageType(const std::string& path_string, bool check_conten
return CARTA::UNKNOWN;
}

CARTA::FileType GuessImageDirectoryType(const std::string& path_string, bool check_content) {
if (check_content) {
// Guess file type using casacore::ImageOpener
auto image_type = CasacoreImageType(path_string);

switch (image_type) {
case casacore::ImageOpener::AIPSPP:
case casacore::ImageOpener::IMAGECONCAT:
case casacore::ImageOpener::IMAGEEXPR:
case casacore::ImageOpener::COMPLISTIMAGE:
return CARTA::CASA;
case casacore::ImageOpener::MIRIAD:
return CARTA::MIRIAD;
default:
return CARTA::UNKNOWN;
}
} else {
// Guess file type by checking for files inside directory
fs::path dir(path_string);

if (fs::exists(dir / "table.info") || fs::exists(dir / "imageconcat.json") || fs::exists(dir / "imageexpr.json")) {
return CARTA::CASA;
}

if (fs::exists(dir / "image") && fs::exists(dir / "header")) {
return CARTA::MIRIAD;
}
}

return CARTA::UNKNOWN;
}

CARTA::FileType GuessRegionType(const std::string& path_string, bool check_content) {
if (check_content) {
// Check beginning of file for CRTF or REG header
Expand Down
1 change: 1 addition & 0 deletions src/Util/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define TEMP_FILE_ID -100

CARTA::FileType GuessImageType(const std::string& path_string, bool check_content);
CARTA::FileType GuessImageDirectoryType(const std::string& path_string, bool check_content);
CARTA::FileType GuessRegionType(const std::string& path_string, bool check_content);
CARTA::CatalogFileType GuessTableType(const std::string& path_string, bool check_content);

Expand Down
Loading