Skip to content

Commit

Permalink
Strip Image Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jigar-f committed May 7, 2024
1 parent 18ce9bc commit d00a195
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 43 deletions.
63 changes: 56 additions & 7 deletions lib/messaging/conversation/conversation.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:file_picker/file_picker.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:intl/intl.dart' as intl;
import 'package:lantern/core/router/router.gr.dart' as router_gr;
import 'package:lantern/messaging/conversation/unaccepted_contact_sticker.dart';
Expand Down Expand Up @@ -264,21 +265,41 @@ class ConversationState extends State<Conversation>
context.loaderOverlay.show(widget: spinner);
for (var i = 0; i < result.files.length; i++) {
final el = result.files[i];
final title = el.path.toString().split('file_picker/')[1].split('.')[
0]; // example path: /data/user/0/org.getlantern.lantern/cache/file_picker/alpha_png.png
final fileExtension =
el.path.toString().split('file_picker/')[1].split('.')[1];
final title = el.path.toString().split('file_picker/')[1].split('.')[
0]; // example path: /data/user/0/org.getlantern.lantern/cache/file_picker/alpha_png.png
Uint8List? attachmentBytes;

final metadata = {
'title': title,
'fileExtension': fileExtension,
};
final attachment = await messagingModel.filePickerLoadAttachment(
el.path.toString(),
metadata,
);
if (fileExtension.isCompressSupported()) {
final targetPath =
'${el.path.toString().split('file_picker/')[0]}file_picker/${title}_compressed.$fileExtension';

//Removing Metadata from attachments
final stripedImage = await FlutterImageCompress.compressAndGetFile(
el.path!,
targetPath,
format: fileExtension.getFormat(),
keepExif: false, //this removes metadata from image
);

attachmentBytes = await messagingModel.filePickerLoadAttachment(
stripedImage!.path.toString(),
metadata,
);
} else {
attachmentBytes = await messagingModel.filePickerLoadAttachment(
el.path!.toString(),
metadata,
);
}
await sendMessage(
newMessage.value.text,
attachments: [attachment],
attachments: [attachmentBytes],
replyToSenderId: quotedMessage?.senderId,
replyToId: quotedMessage?.id,
);
Expand Down Expand Up @@ -909,3 +930,31 @@ class ConversationState extends State<Conversation>
}

enum KeyboardMode { none, native, emoji, emojiReaction }

extension FileExtension on String {
CompressFormat getFormat() {
if (toLowerCase() == 'png') {
return CompressFormat.png;
}
if (toLowerCase() == 'jpeg') {
return CompressFormat.jpeg;
}
if (toLowerCase() == 'heic') {
return CompressFormat.heic;
}
if (toLowerCase() == 'webp') {
return CompressFormat.webp;
}
return CompressFormat.png;
}

bool isCompressSupported() {
if (toLowerCase() == 'png' ||
toLowerCase() == 'jpeg' ||
toLowerCase() == 'heic' ||
toLowerCase() == 'webp') {
return true;
}
return false;
}
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import app_links
import audioplayers_darwin
import device_info_plus
import emoji_picker_flutter
import flutter_image_compress_macos
import flutter_inappwebview_macos
import flutter_local_notifications
import package_info_plus
Expand All @@ -29,6 +30,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
EmojiPickerFlutterPlugin.register(with: registry.registrar(forPlugin: "EmojiPickerFlutterPlugin"))
FlutterImageCompressMacosPlugin.register(with: registry.registrar(forPlugin: "FlutterImageCompressMacosPlugin"))
InAppWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "InAppWebViewFlutterPlugin"))
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
Expand Down
96 changes: 60 additions & 36 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,54 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_image_compress:
dependency: "direct main"
description:
name: flutter_image_compress
sha256: "4edadb0ca2f957b85190e9c3aa728569b91b64b6e06e0eec5b622d47a8692ab2"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
flutter_image_compress_common:
dependency: transitive
description:
name: flutter_image_compress_common
sha256: "7f79bc6c8a363063620b4e372fa86bc691e1cb28e58048cd38e030692fbd99ee"
url: "https://pub.dev"
source: hosted
version: "1.0.5"
flutter_image_compress_macos:
dependency: transitive
description:
name: flutter_image_compress_macos
sha256: "26df6385512e92b3789dc76b613b54b55c457a7f1532e59078b04bf189782d47"
url: "https://pub.dev"
source: hosted
version: "1.0.2"
flutter_image_compress_ohos:
dependency: transitive
description:
name: flutter_image_compress_ohos
sha256: "70360371698be994786e5dd2e364a6525b1c5a4f843bff8af9b8a2fbe808d8d8"
url: "https://pub.dev"
source: hosted
version: "0.0.2"
flutter_image_compress_platform_interface:
dependency: transitive
description:
name: flutter_image_compress_platform_interface
sha256: "579cb3947fd4309103afe6442a01ca01e1e6f93dc53bb4cbd090e8ce34a41889"
url: "https://pub.dev"
source: hosted
version: "1.0.5"
flutter_image_compress_web:
dependency: transitive
description:
name: flutter_image_compress_web
sha256: f02fe352b17f82b72f481de45add240db062a2585850bea1667e82cc4cd6c311
url: "https://pub.dev"
source: hosted
version: "0.1.4+1"
flutter_inappwebview:
dependency: "direct main"
description:
Expand Down Expand Up @@ -909,30 +957,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.8.1"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "2.0.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -993,10 +1017,10 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.5.0"
menu_base:
dependency: transitive
description:
Expand All @@ -1009,10 +1033,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.12.0"
mime:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1606,26 +1630,26 @@ packages:
dependency: "direct dev"
description:
name: test
sha256: a1f7595805820fcc05e5c52e3a231aedd0b72972cb333e8c738a8b1239448b6f
sha256: "7ee446762c2c50b3bd4ea96fe13ffac69919352bd3b4b17bac3f3465edc58073"
url: "https://pub.dev"
source: hosted
version: "1.24.9"
version: "1.25.2"
test_api:
dependency: transitive
description:
name: test_api
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
version: "0.7.0"
test_core:
dependency: transitive
description:
name: test_core
sha256: a757b14fc47507060a162cc2530d9a4a2f92f5100a952c7443b5cad5ef5b106a
sha256: "2bc4b4ecddd75309300d8096f781c0e3280ca1ef85beda558d33fcbedc2eead4"
url: "https://pub.dev"
source: hosted
version: "0.5.9"
version: "0.6.0"
timezone:
dependency: transitive
description:
Expand Down Expand Up @@ -1822,10 +1846,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
sha256: a13d5503b4facefc515c8c587ce3cf69577a7b064a9f1220e005449cf1f64aad
url: "https://pub.dev"
source: hosted
version: "13.0.0"
version: "12.0.0"
wakelock:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dependencies:
video_thumbnail: ^0.5.3
file_picker: ^5.3.3
filesize: ^2.0.1
flutter_image_compress: ^2.2.0

# Networking
cached_network_image: ^3.3.1
Expand Down

0 comments on commit d00a195

Please sign in to comment.