mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 02:57:42 +00:00
LibCore+LibWeb: Move guess-mimetype-based-on-filename logic to LibCore
This could be useful in more places.
This commit is contained in:
parent
e3437414f0
commit
78518d230c
3 changed files with 30 additions and 27 deletions
|
@ -70,4 +70,30 @@ void MimeData::set_text(const String& text)
|
||||||
set_data("text/plain", text.to_byte_buffer());
|
set_data("text/plain", text.to_byte_buffer());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String guess_mime_type_based_on_filename(const URL& url)
|
||||||
|
{
|
||||||
|
String lowercase_url = url.path().to_lowercase();
|
||||||
|
if (lowercase_url.ends_with(".pbm"))
|
||||||
|
return "image/x‑portable‑bitmap";
|
||||||
|
if (url.path().ends_with(".pgm"))
|
||||||
|
return "image/x‑portable‑graymap";
|
||||||
|
if (url.path().ends_with(".png"))
|
||||||
|
return "image/png";
|
||||||
|
if (lowercase_url.ends_with(".ppm"))
|
||||||
|
return "image/x‑portable‑pixmap";
|
||||||
|
if (lowercase_url.ends_with(".gif"))
|
||||||
|
return "image/gif";
|
||||||
|
if (lowercase_url.ends_with(".bmp"))
|
||||||
|
return "image/bmp";
|
||||||
|
if (lowercase_url.ends_with(".jpg") || lowercase_url.ends_with(".jpeg"))
|
||||||
|
return "image/jpeg";
|
||||||
|
if (lowercase_url.ends_with(".md"))
|
||||||
|
return "text/markdown";
|
||||||
|
if (lowercase_url.ends_with(".html") || lowercase_url.ends_with(".htm"))
|
||||||
|
return "text/html";
|
||||||
|
if (lowercase_url.ends_with("/"))
|
||||||
|
return "text/html";
|
||||||
|
return "text/plain";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,4 +61,6 @@ private:
|
||||||
HashMap<String, ByteBuffer> m_data;
|
HashMap<String, ByteBuffer> m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
String guess_mime_type_based_on_filename(const URL&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
|
#include <LibCore/MimeData.h>
|
||||||
#include <LibWeb/HTML/HTMLImageElement.h>
|
#include <LibWeb/HTML/HTMLImageElement.h>
|
||||||
#include <LibWeb/Loader/Resource.h>
|
#include <LibWeb/Loader/Resource.h>
|
||||||
|
|
||||||
|
@ -83,32 +84,6 @@ String mime_type_from_content_type(const String& content_type)
|
||||||
return content_type;
|
return content_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String guess_mime_type_based_on_filename(const URL& url)
|
|
||||||
{
|
|
||||||
String lowercase_url = url.path().to_lowercase();
|
|
||||||
if (lowercase_url.ends_with(".pbm"))
|
|
||||||
return "image/x‑portable‑bitmap";
|
|
||||||
if (url.path().ends_with(".pgm"))
|
|
||||||
return "image/x‑portable‑graymap";
|
|
||||||
if (url.path().ends_with(".png"))
|
|
||||||
return "image/png";
|
|
||||||
if (lowercase_url.ends_with(".ppm"))
|
|
||||||
return "image/x‑portable‑pixmap";
|
|
||||||
if (lowercase_url.ends_with(".gif"))
|
|
||||||
return "image/gif";
|
|
||||||
if (lowercase_url.ends_with(".bmp"))
|
|
||||||
return "image/bmp";
|
|
||||||
if (lowercase_url.ends_with(".jpg") || lowercase_url.ends_with(".jpeg"))
|
|
||||||
return "image/jpeg";
|
|
||||||
if (lowercase_url.ends_with(".md"))
|
|
||||||
return "text/markdown";
|
|
||||||
if (lowercase_url.ends_with(".html") || lowercase_url.ends_with(".htm"))
|
|
||||||
return "text/html";
|
|
||||||
if (lowercase_url.ends_with("/"))
|
|
||||||
return "text/html";
|
|
||||||
return "text/plain";
|
|
||||||
}
|
|
||||||
|
|
||||||
void Resource::did_load(Badge<ResourceLoader>, const ByteBuffer& data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers)
|
void Resource::did_load(Badge<ResourceLoader>, const ByteBuffer& data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers)
|
||||||
{
|
{
|
||||||
ASSERT(!m_loaded);
|
ASSERT(!m_loaded);
|
||||||
|
@ -134,7 +109,7 @@ void Resource::did_load(Badge<ResourceLoader>, const ByteBuffer& data, const Has
|
||||||
dbg() << "No Content-Type header to go on! Guessing based on filename...";
|
dbg() << "No Content-Type header to go on! Guessing based on filename...";
|
||||||
#endif
|
#endif
|
||||||
m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
|
m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
|
||||||
m_mime_type = guess_mime_type_based_on_filename(url());
|
m_mime_type = Core::guess_mime_type_based_on_filename(url());
|
||||||
}
|
}
|
||||||
|
|
||||||
for_each_client([](auto& client) {
|
for_each_client([](auto& client) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue