mirror of
https://github.com/transmission/transmission.git
synced 2026-02-14 23:19:34 +00:00
refactor: move to libtrbase: mime-types
This commit is contained in:
2
.gitattributes
vendored
2
.gitattributes
vendored
@@ -1,5 +1,5 @@
|
||||
# libtransmission
|
||||
libtransmission/mime-types.h linguist-generated=true
|
||||
lib/base/mime-types.h linguist-generated=true
|
||||
|
||||
# web
|
||||
web/package.json.buildonly linguist-generated=true
|
||||
|
||||
@@ -12,10 +12,8 @@
|
||||
#include "Session.h"
|
||||
#include "Utils.h"
|
||||
|
||||
#include <libtransmission/utils.h>
|
||||
|
||||
#include "lib/base/string-utils.h"
|
||||
#include <lib/base/file-utils.h>
|
||||
#include <lib/base/string-utils.h>
|
||||
|
||||
#include <giomm/icon.h>
|
||||
#include <glibmm/fileutils.h>
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <libtransmission/transmission.h>
|
||||
#include <libtransmission/utils.h>
|
||||
|
||||
#include "lib/base/file-utils.h"
|
||||
#include "lib/base/tr-macros.h"
|
||||
#include "lib/base/values.h"
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ target_sources(trbase
|
||||
i18n.h
|
||||
log.cc
|
||||
log.h
|
||||
mime-types.h
|
||||
quark.cc
|
||||
quark.h
|
||||
serializer.cc
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/stat.h> /* umask() */
|
||||
#endif
|
||||
@@ -18,6 +21,8 @@
|
||||
#include "lib/base/file.h"
|
||||
#include "lib/base/i18n.h"
|
||||
#include "lib/base/log.h"
|
||||
#include "lib/base/mime-types.h"
|
||||
#include "lib/base/string-utils.h"
|
||||
#include "lib/base/tr-strbuf.h"
|
||||
|
||||
using namespace std::literals;
|
||||
@@ -197,3 +202,40 @@ bool tr_file_move(std::string_view oldpath, std::string_view newpath, bool allow
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// --- mime-type
|
||||
|
||||
std::string_view tr_get_mime_type_for_filename(std::string_view filename)
|
||||
{
|
||||
auto constexpr Compare = [](mime_type_suffix const& entry, auto const& suffix)
|
||||
{
|
||||
return entry.suffix < suffix;
|
||||
};
|
||||
|
||||
if (auto const pos = filename.rfind('.'); pos != std::string_view::npos)
|
||||
{
|
||||
auto const suffix_lc = tr_strlower(filename.substr(pos + 1));
|
||||
auto const it = std::lower_bound(std::begin(MimeTypeSuffixes), std::end(MimeTypeSuffixes), suffix_lc, Compare);
|
||||
if (it != std::end(MimeTypeSuffixes) && suffix_lc == it->suffix)
|
||||
{
|
||||
std::string_view mime_type = it->mime_type;
|
||||
|
||||
// https://github.com/transmission/transmission/issues/5965#issuecomment-1704421231
|
||||
// An mp4 file's correct mime-type depends on the codecs used in the file,
|
||||
// which we have no way of inspecting and which might not be downloaded yet.
|
||||
// Let's use `video/mp4` since that's by far the most common use case for torrents.
|
||||
if (mime_type == "application/mp4")
|
||||
{
|
||||
mime_type = "video/mp4";
|
||||
}
|
||||
|
||||
return mime_type;
|
||||
}
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
||||
// application/octet-stream is the default value.
|
||||
// An unknown file type should use this type.
|
||||
auto constexpr Fallback = "application/octet-stream"sv;
|
||||
return Fallback;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ struct tr_error;
|
||||
|
||||
bool tr_file_read(std::string_view filename, std::vector<char>& contents, tr_error* error = nullptr);
|
||||
|
||||
[[nodiscard]] std::string_view tr_get_mime_type_for_filename(std::string_view filename);
|
||||
|
||||
/**
|
||||
* Tries to move a file by renaming, and [optionally] if that fails, by copying.
|
||||
*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// This file was generated with libtransmission/mime-types.js
|
||||
// This file was generated with lib/base/mime-types.js
|
||||
// DO NOT EDIT MANUALLY
|
||||
|
||||
// This file Copyright © Mnemosyne LLC.
|
||||
@@ -2,7 +2,7 @@
|
||||
import fs from 'node:fs';
|
||||
|
||||
const copyright =
|
||||
`// This file was generated with libtransmission/mime-types.js
|
||||
`// This file was generated with lib/base/mime-types.js
|
||||
// DO NOT EDIT MANUALLY
|
||||
|
||||
// This file Copyright © Mnemosyne LLC.
|
||||
@@ -62,7 +62,6 @@ target_sources(${TR_NAME}
|
||||
magnet-metainfo.h
|
||||
makemeta.cc
|
||||
makemeta.h
|
||||
mime-types.h
|
||||
net.cc
|
||||
net.h
|
||||
open-files.cc
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
|
||||
#include "libtransmission/torrent-files.h"
|
||||
#include "libtransmission/types.h"
|
||||
#include "libtransmission/utils.h"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
|
||||
@@ -44,7 +44,6 @@
|
||||
#include "lib/base/tr-strbuf.h"
|
||||
#include "lib/base/values.h"
|
||||
|
||||
#include "libtransmission/mime-types.h"
|
||||
#include "libtransmission/types.h"
|
||||
#include "libtransmission/utils.h"
|
||||
|
||||
@@ -435,40 +434,3 @@ void tr_lib_init()
|
||||
tr::serializer::install_libtransmission_converters();
|
||||
});
|
||||
}
|
||||
|
||||
// --- mime-type
|
||||
|
||||
std::string_view tr_get_mime_type_for_filename(std::string_view filename)
|
||||
{
|
||||
auto constexpr Compare = [](mime_type_suffix const& entry, auto const& suffix)
|
||||
{
|
||||
return entry.suffix < suffix;
|
||||
};
|
||||
|
||||
if (auto const pos = filename.rfind('.'); pos != std::string_view::npos)
|
||||
{
|
||||
auto const suffix_lc = tr_strlower(filename.substr(pos + 1));
|
||||
auto const it = std::lower_bound(std::begin(MimeTypeSuffixes), std::end(MimeTypeSuffixes), suffix_lc, Compare);
|
||||
if (it != std::end(MimeTypeSuffixes) && suffix_lc == it->suffix)
|
||||
{
|
||||
std::string_view mime_type = it->mime_type;
|
||||
|
||||
// https://github.com/transmission/transmission/issues/5965#issuecomment-1704421231
|
||||
// An mp4 file's correct mime-type depends on the codecs used in the file,
|
||||
// which we have no way of inspecting and which might not be downloaded yet.
|
||||
// Let's use `video/mp4` since that's by far the most common use case for torrents.
|
||||
if (mime_type == "application/mp4")
|
||||
{
|
||||
mime_type = "video/mp4";
|
||||
}
|
||||
|
||||
return mime_type;
|
||||
}
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
||||
// application/octet-stream is the default value.
|
||||
// An unknown file type should use this type.
|
||||
auto constexpr Fallback = "application/octet-stream"sv;
|
||||
return Fallback;
|
||||
}
|
||||
|
||||
@@ -25,8 +25,6 @@ std::optional<std::locale> tr_locale_set_global(std::locale const& locale) noexc
|
||||
|
||||
// ---
|
||||
|
||||
[[nodiscard]] std::string_view tr_get_mime_type_for_filename(std::string_view filename);
|
||||
|
||||
/** @brief return the current date in milliseconds */
|
||||
[[nodiscard]] uint64_t tr_time_msec();
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "lib/base/env.h"
|
||||
#include "lib/base/file-utils.h"
|
||||
|
||||
#include "libtransmission/transmission.h"
|
||||
#include "libtransmission/utils.h"
|
||||
|
||||
Reference in New Issue
Block a user