Skip to content

Commit

Permalink
読み込み時に画像の向き以外のEXIFを削除
Browse files Browse the repository at this point in the history
  • Loading branch information
4ster1sk committed Nov 7, 2024
1 parent 47cefb3 commit e3420aa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
);
}
Expand Down Expand Up @@ -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,
),
),
Expand All @@ -617,6 +618,30 @@ class NoteCreateNotifier extends _$NoteCreateNotifier {
}
}

Future<Uint8List> 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;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ packages:
source: hosted
version: "1.0.5"
mime:
dependency: transitive
dependency: "direct main"
description:
name: mime
sha256: "801fd0b26f14a4a58ccb09d5892c3fbdeff209594300a542492cf13fba9d247a"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e3420aa

Please sign in to comment.