Cannot preview image with file name containing image extension (jpg,..) and mime type is application/octet-stream

Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
dab246
2025-04-17 00:00:29 +07:00
committed by Dat H. Pham
parent 3337a608be
commit 037fcc03a1
5 changed files with 89 additions and 3 deletions
@@ -4,7 +4,13 @@ class SupportedPreviewFileTypes {
'image/jpeg',
'image/gif',
'image/webp',
'image/png',];
'image/png',
'image/svg+xml',
'image/x-icon',
'image/tiff',
'image/heif',
'image/avif'
];
static const textMimeTypes = [
'text/plain',
@@ -1,6 +1,7 @@
import 'package:core/data/constants/constant.dart';
import 'package:core/domain/preview/document_uti.dart';
import 'package:core/domain/preview/supported_preview_file_types.dart';
import 'package:core/presentation/extensions/string_extension.dart';
import 'package:core/presentation/resources/image_paths.dart';
import 'package:http_parser/http_parser.dart';
@@ -38,7 +39,7 @@ extension MediaTypeExtension on MediaType {
return imagePaths.icFilePptx;
} else if (isZipFile()) {
return imagePaths.icFileZip;
} else if (isImageFile()) {
} else if (isImageSupportedPreview(fileName: fileName)) {
return imagePaths.icFilePng;
} else {
return imagePaths.icFileEPup;
@@ -56,4 +57,11 @@ extension MediaTypeExtension on MediaType {
mimeType == Constant.textHtmlMimeType ||
(mimeType == Constant.octetStreamMimeType &&
fileName?.endsWith(Constant.htmlExtension) == true);
bool isImageSupportedPreview({required String? fileName}) =>
isImageFile() ||
(mimeType == Constant.octetStreamMimeType &&
fileName != null &&
SupportedPreviewFileTypes.imageMimeTypes
.contains(fileName.imageMimeType));
}
@@ -40,4 +40,42 @@ extension StringExtension on String {
}
String get firstCharacterToUpperCase => isNotEmpty ? this[0].toUpperCase() : '';
String get fileExtension {
int dotIndex = lastIndexOf('.');
if (dotIndex == -1 || dotIndex == length - 1) {
return '';
}
return substring(dotIndex + 1);
}
String get imageMimeType {
switch (fileExtension.toLowerCase()) {
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'png':
return 'image/png';
case 'gif':
return 'image/gif';
case 'webp':
return 'image/webp';
case 'svg':
return 'image/svg+xml';
case 'bmp':
return 'image/bmp';
case 'ico':
return 'image/x-icon';
case 'tif':
case 'tiff':
return 'image/tiff';
case 'heic':
case 'heif':
return 'image/heif';
case 'avif':
return 'image/avif';
default:
return 'application/octet-stream'; // Unknown type
}
}
}
@@ -58,4 +58,38 @@ void main() {
expect(name.firstLetterToUpperCase, equals(''));
});
});
group('fileExtension', () {
test('should return empty string when no dot in path', () {
expect('filename'.fileExtension, equals(''));
});
test('should return empty string when dot is last character', () {
expect('filename.'.fileExtension, equals(''));
});
test('should return extension for simple filename', () {
expect('filename.txt'.fileExtension, equals('txt'));
});
test('should return extension for filename with multiple dots', () {
expect('file.name.with.dots.pdf'.fileExtension, equals('pdf'));
});
test('should return extension for path with directories', () {
expect('/path/to/file/image.png'.fileExtension, equals('png'));
});
test('should handle empty string', () {
expect(''.fileExtension, equals(''));
});
test('should handle whitespace after dot', () {
expect('filename. txt'.fileExtension, equals(' txt'));
});
test('should handle complex extensions', () {
expect('archive.tar.gz'.fileExtension, equals('gz'));
});
});
}
@@ -41,7 +41,7 @@ extension AttachmentExtension on Attachment {
bool get isHTMLFile => type?.isHTMLFile(fileName: name) ?? false;
bool get isImage => type?.isImageFile() ?? false;
bool get isImage => type?.isImageSupportedPreview(fileName: name) ?? false;
bool get isText => (type?.isTextFile() ?? false)
|| name?.endsWith('.txt') == true