isTextMimeType function
Checks if a file is considered a text file based on its MIME type.
path: The path to the file.headerBytes: Optional header bytes for more accurate MIME type detection.
Returns true if the file's MIME type is recognized as text, false otherwise.
doTextMimeInit(); // initializes
print(isTextMimeType('a.txt')); // true
print(isTextMimeType('a.pdf')); // false
print(isTextMimeType('a.yml')); // false
// add a custom text mimetype, yml|yaml is a config language.
doTextMimeAdd('yml', 'text/yaml');
print(isTextMimeType('a.yml')); // true
Implementation
bool isTextMimeType(String path, {List<int>? headerBytes}) {
final mimetype =
mimetypeResolver.lookup(path, headerBytes: headerBytes) ?? '';
if (mimetype.isEmpty) return false;
return mimetype.contains('text/') ||
mimetype.endsWith('xml') ||
mimetype.endsWith('json') ||
mimetype.endsWith('ml');
}