Remove charset of binary files
This commit is contained in:
parent
3a36aec87e
commit
961065ef3a
|
@ -56,17 +56,16 @@ class InAppLocalhostServer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var contentType = ['text', 'html'];
|
var contentType = ContentType('text', 'html', charset: 'utf-8');
|
||||||
if (!request.requestedUri.path.endsWith('/') &&
|
if (!request.requestedUri.path.endsWith('/') &&
|
||||||
request.requestedUri.pathSegments.isNotEmpty) {
|
request.requestedUri.pathSegments.isNotEmpty) {
|
||||||
var mimeType = MimeTypeResolver.lookup(request.requestedUri.path);
|
var mimeType = MimeTypeResolver.lookup(request.requestedUri.path);
|
||||||
if (mimeType != null) {
|
if (mimeType != null) {
|
||||||
contentType = mimeType.split('/');
|
contentType = _getContentTypeFromMimeType(mimeType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
request.response.headers.contentType =
|
request.response.headers.contentType = contentType;
|
||||||
ContentType(contentType[0], contentType[1], charset: 'utf-8');
|
|
||||||
request.response.add(body);
|
request.response.add(body);
|
||||||
request.response.close();
|
request.response.close();
|
||||||
});
|
});
|
||||||
|
@ -93,4 +92,20 @@ class InAppLocalhostServer {
|
||||||
bool isRunning() {
|
bool isRunning() {
|
||||||
return this._server != null;
|
return this._server != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ContentType _getContentTypeFromMimeType(String mimeType) {
|
||||||
|
final contentType = mimeType.split('/');
|
||||||
|
String? charset;
|
||||||
|
|
||||||
|
if (_isTextFile(mimeType)) {
|
||||||
|
charset = 'utf-8';
|
||||||
|
}
|
||||||
|
|
||||||
|
return ContentType(contentType[0], contentType[1], charset: charset);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _isTextFile(String mimeType) {
|
||||||
|
final textFile = RegExp(r'^text\/|^application\/(javascript|json)');
|
||||||
|
return textFile.hasMatch(mimeType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue