ultralight-ux / Ultralight

Lightweight, high-performance HTML renderer for game and app developers.
https://ultralig.ht
4.68k stars 196 forks source link

Use gio's mime type functions #419

Open SupinePandora43 opened 2 years ago

SupinePandora43 commented 2 years ago

Right now FileSystem exposes GetFileMimeType(const String& path) requiring a library users to implement it by themselves. It can cause platform differences between windows (registry), macos (kUTTagClassMIMEType) and linux (if-else).

Since ultralight bundles gio, is it possible to use it instead?

Like in: https://stackoverflow.com/a/21552150/9765252

ULString fileName = ...;
ULBuffer fileData = ...;

uint8_t* contentType = g_content_type_guess(fileName.data(), fileData.data(), fileData.size(), NULL);
ASSERT(contentType != NULL);

uint8_t* mime_type = g_content_type_get_mime_type(contentType);

g_free(contentType);

ULString mime;
mime.data = mime_type;
mime.length = getNullTerminatedLength(mime_type);

// gio's result will be freed by default ULString's `free(data)` destructor.

But it returns application/x-ext-js instead of application/javascript for mediaControlsLocalizedStrings.js and similarly for .dat and .pem files. css is fine though.

SupinePandora43 commented 2 years ago

I take my words back - it returns garbage (application/x-ext-EXTENSION) instead of valid values.