Skip to content

Commit d7c7671

Browse files
committed
more mimetypes
1 parent f295f1e commit d7c7671

36 files changed

Lines changed: 302 additions & 47 deletions

src/odr/file.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <odr/internal/abstract/file.hpp>
88
#include <odr/internal/common/file.hpp>
9+
#include <odr/internal/magic.hpp>
910
#include <odr/internal/open_strategy.hpp>
1011
#include <odr/internal/util/file_util.hpp>
1112
#include <odr/internal/util/stream_util.hpp>
@@ -27,6 +28,12 @@ FileMeta::FileMeta(const FileType type, const bool password_encrypted,
2728
: type{type}, password_encrypted{password_encrypted},
2829
document_meta{document_meta} {}
2930

31+
FileMeta::FileMeta(const FileType type, const std::string_view mimetype,
32+
const bool password_encrypted,
33+
const std::optional<DocumentMeta> document_meta)
34+
: type{type}, mimetype{mimetype}, password_encrypted{password_encrypted},
35+
document_meta{document_meta} {}
36+
3037
File::File() = default;
3138

3239
File::File(std::shared_ptr<internal::abstract::File> impl)
@@ -66,6 +73,12 @@ std::vector<FileType> DecodedFile::list_file_types(const std::string &path,
6673
std::make_shared<internal::DiskFile>(path), logger);
6774
}
6875

76+
std::string_view DecodedFile::mimetype(const std::string &path,
77+
Logger &logger) {
78+
(void)logger;
79+
return internal::magic::mimetype(path);
80+
}
81+
6982
std::vector<DecoderEngine>
7083
DecodedFile::list_decoder_engines(const FileType as) {
7184
return internal::open_strategy::list_decoder_engines(as);

src/odr/file.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <memory>
66
#include <optional>
77
#include <string>
8+
#include <string_view>
89
#include <vector>
910

1011
namespace odr::internal::abstract {
@@ -140,10 +141,14 @@ struct DocumentMeta final {
140141
/// @brief Meta information about a file.
141142
struct FileMeta final {
142143
FileMeta();
144+
[[deprecated]]
143145
FileMeta(FileType type, bool password_encrypted,
144146
std::optional<DocumentMeta> document_meta);
147+
FileMeta(FileType type, std::string_view mimetype, bool password_encrypted,
148+
std::optional<DocumentMeta> document_meta);
145149

146150
FileType type{FileType::unknown};
151+
std::string_view mimetype;
147152
bool password_encrypted{false};
148153
std::optional<DocumentMeta> document_meta;
149154
};
@@ -175,9 +180,12 @@ class File final {
175180
/// @brief Represents a decoded file.
176181
class DecodedFile {
177182
public:
178-
static std::vector<FileType> list_file_types(const std::string &path,
179-
Logger &logger = Logger::null());
180-
static std::vector<DecoderEngine> list_decoder_engines(FileType as);
183+
[[nodiscard]] static std::vector<FileType>
184+
list_file_types(const std::string &path, Logger &logger = Logger::null());
185+
[[nodiscard]] static std::vector<DecoderEngine>
186+
list_decoder_engines(FileType as);
187+
[[nodiscard]] static std::string_view
188+
mimetype(const std::string &path, Logger &logger = Logger::null());
181189

182190
explicit DecodedFile(std::shared_ptr<internal::abstract::DecodedFile> impl);
183191
explicit DecodedFile(const File &file, Logger &logger = Logger::null());

src/odr/internal/abstract/file.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DecodedFile {
3838
[[nodiscard]] virtual FileCategory file_category() const noexcept = 0;
3939
[[nodiscard]] virtual FileMeta file_meta() const noexcept = 0;
4040
[[nodiscard]] virtual DecoderEngine decoder_engine() const noexcept = 0;
41+
[[nodiscard]] virtual std::string_view mimetype() const noexcept = 0;
4142

4243
[[nodiscard]] virtual bool password_encrypted() const noexcept {
4344
return false;

src/odr/internal/cfb/cfb_file.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ FileType CfbFile::file_type() const noexcept {
1717
}
1818

1919
FileMeta CfbFile::file_meta() const noexcept {
20-
FileMeta meta;
21-
meta.type = file_type();
22-
return meta;
20+
return {FileType::compound_file_binary_format, "application/x-cfb", false,
21+
std::nullopt};
2322
}
2423

2524
DecoderEngine CfbFile::decoder_engine() const noexcept {
2625
return DecoderEngine::odr;
2726
}
2827

28+
std::string_view CfbFile::mimetype() const noexcept {
29+
return "application/x-cfb";
30+
}
31+
2932
bool CfbFile::is_decodable() const noexcept { return true; }
3033

3134
std::shared_ptr<abstract::Archive> CfbFile::archive() const {

src/odr/internal/cfb/cfb_file.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class CfbFile final : public abstract::ArchiveFile {
2626
[[nodiscard]] FileType file_type() const noexcept override;
2727
[[nodiscard]] FileMeta file_meta() const noexcept override;
2828
[[nodiscard]] DecoderEngine decoder_engine() const noexcept override;
29+
[[nodiscard]] std::string_view mimetype() const noexcept override;
2930

3031
[[nodiscard]] bool is_decodable() const noexcept override;
3132

src/odr/internal/common/image_file.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ DecoderEngine ImageFile::decoder_engine() const noexcept {
2020
return DecoderEngine::odr;
2121
}
2222

23+
std::string_view ImageFile::mimetype() const noexcept { return ""; }
24+
2325
bool ImageFile::is_decodable() const noexcept { return false; }
2426

2527
std::shared_ptr<abstract::Image> ImageFile::image() const {

src/odr/internal/common/image_file.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ImageFile final : public abstract::ImageFile {
1313
[[nodiscard]] FileType file_type() const noexcept override;
1414
[[nodiscard]] FileMeta file_meta() const noexcept override;
1515
[[nodiscard]] DecoderEngine decoder_engine() const noexcept override;
16+
[[nodiscard]] std::string_view mimetype() const noexcept override;
1617

1718
[[nodiscard]] bool is_decodable() const noexcept override;
1819

src/odr/internal/csv/csv_file.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ FileType CsvFile::file_type() const noexcept {
2020
return FileType::comma_separated_values;
2121
}
2222

23-
FileMeta CsvFile::file_meta() const noexcept {
24-
return {FileType::comma_separated_values, false, {}};
25-
}
26-
2723
DecoderEngine CsvFile::decoder_engine() const noexcept {
2824
return DecoderEngine::odr;
2925
}
3026

27+
std::string_view CsvFile::mimetype() const noexcept { return "text/csv"; }
28+
29+
FileMeta CsvFile::file_meta() const noexcept {
30+
return {FileType::comma_separated_values, "text/csv", false, std::nullopt};
31+
}
32+
3133
bool CsvFile::is_decodable() const noexcept { return false; }
3234

3335
} // namespace odr::internal::csv

src/odr/internal/csv/csv_file.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class CsvFile final : public abstract::TextFile {
1717
[[nodiscard]] FileType file_type() const noexcept override;
1818
[[nodiscard]] FileMeta file_meta() const noexcept override;
1919
[[nodiscard]] DecoderEngine decoder_engine() const noexcept override;
20+
[[nodiscard]] std::string_view mimetype() const noexcept override;
2021

2122
[[nodiscard]] bool is_decodable() const noexcept override;
2223

src/odr/internal/json/json_file.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ FileType JsonFile::file_type() const noexcept {
2121
}
2222

2323
FileMeta JsonFile::file_meta() const noexcept {
24-
return {FileType::javascript_object_notation, false, {}};
24+
return {FileType::javascript_object_notation, "application/json", false,
25+
std::nullopt};
2526
}
2627

2728
DecoderEngine JsonFile::decoder_engine() const noexcept {
2829
return DecoderEngine::odr;
2930
}
3031

32+
std::string_view JsonFile::mimetype() const noexcept {
33+
return "application/json";
34+
}
35+
3136
bool JsonFile::is_decodable() const noexcept { return false; }
3237

3338
} // namespace odr::internal::json

0 commit comments

Comments
 (0)