diff --git a/lib/state_notifier/note_create_page/note_create_state_notifier.dart b/lib/state_notifier/note_create_page/note_create_state_notifier.dart index 32da9539d..3f193c9ea 100644 --- a/lib/state_notifier/note_create_page/note_create_state_notifier.dart +++ b/lib/state_notifier/note_create_page/note_create_state_notifier.dart @@ -6,7 +6,9 @@ import "package:flutter/material.dart"; import "package:flutter_gen/gen_l10n/app_localizations.dart"; import "package:flutter_image_compress/flutter_image_compress.dart"; import "package:freezed_annotation/freezed_annotation.dart"; +import "package:image/image.dart"; import "package:mfm_parser/mfm_parser.dart"; +import "package:mime/mime.dart"; import "package:miria/extensions/note_visibility_extension.dart"; import "package:miria/log.dart"; import "package:miria/model/image_file.dart"; @@ -158,17 +160,16 @@ class NoteCreateNotifier extends _$NoteCreateNotifier { files: await Future.wait( initialMediaFiles.map((media) async { final file = _fileSystem.file(media); - final contents = await file.readAsBytes(); final fileName = file.basename; final extension = fileName.split(".").last.toLowerCase(); if (["jpg", "png", "gif", "webp"].contains(extension)) { return ImageFile( - data: contents, + data: await loadImage(file), fileName: fileName, ); } else { return UnknownFile( - data: contents, + data: await file.readAsBytes(), fileName: fileName, ); } @@ -607,7 +608,7 @@ class NoteCreateNotifier extends _$NoteCreateNotifier { final files = await Future.wait( fsFiles.map( (file) async => ImageFile( - data: await file.readAsBytes(), + data: await loadImage(file), fileName: file.basename, ), ), @@ -617,6 +618,30 @@ class NoteCreateNotifier extends _$NoteCreateNotifier { } } + Future loadImage(File file) async { + final imageBytes = await file.readAsBytes(); + final mime = lookupMimeType(file.path, headerBytes: imageBytes); + if (mime == "image/jpeg") { + final origExif = decodeJpgExif(imageBytes); + final exif = ExifData(); + + if (origExif != null) { + final orientation = origExif.imageIfd["Orientation"]; + if (orientation != null) { + exif.imageIfd["Orientation"] = orientation; + } + } + + final jpg = injectJpgExif(imageBytes, exif); + if (jpg == null) { + return Uint8List(0); + } + + return jpg; + } + return imageBytes; + } + /// メディアの内容を変更する void setFileContent(MisskeyPostFile file, Uint8List? content) { if (content == null) return; diff --git a/pubspec.lock b/pubspec.lock index 3af88c6bb..9eb5d5842 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1033,7 +1033,7 @@ packages: source: hosted version: "1.0.5" mime: - dependency: transitive + dependency: "direct main" description: name: mime sha256: "801fd0b26f14a4a58ccb09d5892c3fbdeff209594300a542492cf13fba9d247a" diff --git a/pubspec.yaml b/pubspec.yaml index c1ae7d786..451895194 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -77,6 +77,7 @@ dependencies: flutter_hooks: ^0.20.5 punycode: ^1.0.0 image: ^4.3.0 + mime: ^1.0.6 dependency_overrides: image_editor: