mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:28:10 +00:00
LibCore+file: Factorize code to detect mime-type from bytes of a file
This is always nice to factorize code, but even better when it contains magic numbers.
This commit is contained in:
parent
309a15e2aa
commit
50b5528746
3 changed files with 18 additions and 4 deletions
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/MimeData.h>
|
#include <LibCore/MimeData.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
@ -184,4 +185,19 @@ Optional<DeprecatedString> guess_mime_type_based_on_sniffed_bytes(ReadonlyBytes
|
||||||
#undef __ENUMERATE_MIME_TYPE_HEADER
|
#undef __ENUMERATE_MIME_TYPE_HEADER
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<DeprecatedString> guess_mime_type_based_on_sniffed_bytes(Core::File& file)
|
||||||
|
{
|
||||||
|
// Read accounts for longest possible offset + signature we currently match against (extra/iso-9660)
|
||||||
|
auto maybe_buffer = ByteBuffer::create_uninitialized(0x9006);
|
||||||
|
if (maybe_buffer.is_error())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto maybe_bytes = file.read_some(maybe_buffer.value());
|
||||||
|
if (maybe_bytes.is_error())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return Core::guess_mime_type_based_on_sniffed_bytes(maybe_bytes.value());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,5 +51,6 @@ private:
|
||||||
StringView guess_mime_type_based_on_filename(StringView);
|
StringView guess_mime_type_based_on_filename(StringView);
|
||||||
|
|
||||||
Optional<DeprecatedString> guess_mime_type_based_on_sniffed_bytes(ReadonlyBytes);
|
Optional<DeprecatedString> guess_mime_type_based_on_sniffed_bytes(ReadonlyBytes);
|
||||||
|
Optional<DeprecatedString> guess_mime_type_based_on_sniffed_bytes(Core::File&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -210,8 +210,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
bool all_ok = true;
|
bool all_ok = true;
|
||||||
// Read accounts for longest possible offset + signature we currently match against.
|
|
||||||
auto buffer = TRY(ByteBuffer::create_uninitialized(0x9006));
|
|
||||||
|
|
||||||
for (auto const& path : paths) {
|
for (auto const& path : paths) {
|
||||||
auto file_or_error = Core::File::open(path, Core::File::OpenMode::Read);
|
auto file_or_error = Core::File::open(path, Core::File::OpenMode::Read);
|
||||||
|
@ -230,9 +228,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
} else if (!file_size_in_bytes) {
|
} else if (!file_size_in_bytes) {
|
||||||
outln("{}: empty", path);
|
outln("{}: empty", path);
|
||||||
} else {
|
} else {
|
||||||
auto bytes = TRY(file->read_some(buffer));
|
|
||||||
auto file_name_guess = Core::guess_mime_type_based_on_filename(path);
|
auto file_name_guess = Core::guess_mime_type_based_on_filename(path);
|
||||||
auto mime_type = Core::guess_mime_type_based_on_sniffed_bytes(bytes).value_or(file_name_guess);
|
auto mime_type = Core::guess_mime_type_based_on_sniffed_bytes(*file).value_or(file_name_guess);
|
||||||
auto human_readable_description = get_description_from_mime_type(mime_type, path).value_or(mime_type);
|
auto human_readable_description = get_description_from_mime_type(mime_type, path).value_or(mime_type);
|
||||||
outln("{}: {}", path, flag_mime_only ? mime_type : human_readable_description);
|
outln("{}: {}", path, flag_mime_only ? mime_type : human_readable_description);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue