Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOSでHEICで画像が添付されてしまう問題の修正 #659

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "dart:io";
import "dart:typed_data";

import "package:dio/dio.dart";
Expand All @@ -6,7 +7,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 +161,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)) {
if (["jpg", "png", "gif", "webp", "heic"].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 @@ -592,8 +594,8 @@ class NoteCreateNotifier extends _$NoteCreateNotifier {
final result = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: true,
allowCompression: false,
compressionQuality: 0,
allowCompression: Platform.isIOS, // v8.1.3ではiOS以外でこの値を使用していない
compressionQuality: 0, // Androidでは0にすることで圧縮パススルー
);
if (result == null || result.files.isEmpty) return;

Expand All @@ -607,16 +609,45 @@ 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,
),
),
);

state = state.copyWith(files: [...state.files, ...files]);
state = state.copyWith(files: [
...state.files,
...files.where((file) => file.data.isNotEmpty)
]);
}
}

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 img = injectJpgExif(imageBytes, exif);
if (img == null) {
return Uint8List(0);
}

return img;
} else if (mime == "image/heic") {
return Uint8List(0);
}
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