From e17a283e60f721db0246a3ac5d6ed9751c6f1138 Mon Sep 17 00:00:00 2001 From: Bo Lopker Date: Sun, 3 Nov 2024 12:51:36 +0700 Subject: [PATCH] Fix overwritting and add proper logging --- lib/compressor.dart | 20 ++++++++++++++------ lib/config.dart | 5 +++-- lib/dropper.dart | 9 +++++---- lib/imagefiles.dart | 3 ++- lib/log.dart | 3 +++ lib/main.dart | 3 ++- lib/table.dart | 3 ++- lib/update.dart | 7 ++++--- lib/workqueue.dart | 5 +++-- macos/Podfile.lock | 2 +- pubspec.lock | 24 ++++++++++++++++-------- pubspec.yaml | 1 + rust/src/api/compressor.rs | 20 +++++++++++++++++--- 13 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 lib/log.dart diff --git a/lib/compressor.dart b/lib/compressor.dart index d9a9c94..b750179 100644 --- a/lib/compressor.dart +++ b/lib/compressor.dart @@ -1,9 +1,9 @@ import 'dart:io'; import 'package:alic/imagefiles.dart'; +import 'package:alic/log.dart'; import 'package:alic/src/rust/api/compressor.dart'; import 'package:alic/workqueue.dart'; -import 'package:flutter/foundation.dart'; import './config.dart'; @@ -44,8 +44,7 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) { timer.stop(); } - debugPrint( - 'Compressed ${imageFile.file} in ${timer.elapsedMilliseconds}ms'); + log.d('Compressed ${imageFile.file} in ${timer.elapsedMilliseconds}ms'); final outFile = File(result.outPath); final sizeAfterOptimization = await outFile.length(); @@ -59,10 +58,10 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) { return; } // Success! - if (!config.enablePostfix && config.convertExtension == null) { - // If postfix is disabled (and no conversion is needed), replace the original file with the optimized one + if (!config.enablePostfix) { + // If postfix is disabled, replace the original file with the optimized one File(imageFile.path).delete(); - outFile.rename(imageFile.path); + outFile.rename(replaceLast(outFile.path, config.postfix, '')); } callback(imageFile.copyWith( sizeAfterOptimization: sizeAfterOptimization, @@ -70,3 +69,12 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) { )); }); } + +String replaceLast(String string, String from, String to) { + final lastIndex = string.lastIndexOf(from); + if (lastIndex < 0) return string; + + return string.substring(0, lastIndex) + + to + + string.substring(lastIndex + from.length); +} diff --git a/lib/config.dart b/lib/config.dart index a29b75d..b1bd1f2 100644 --- a/lib/config.dart +++ b/lib/config.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:alic/log.dart'; import 'package:flutter/material.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:signals/signals.dart' as signals; @@ -47,7 +48,7 @@ class Config { static void init() { signal.value = readConfig(); signals.effect(() { - debugPrint('Config changed: ${signal.value}'); + log.d('Config changed: ${signal.value}'); writeConfig(signal.value); }); } @@ -55,7 +56,7 @@ class Config { static ConfigData readConfig() { ensureConfigExists(); var configData = configFile.readAsStringSync(); - debugPrint('Read config: $configData'); + log.d('Read config: $configData'); return ConfigData.fromJson(jsonDecode(configData)); } diff --git a/lib/dropper.dart b/lib/dropper.dart index faaf6c8..7c6ac70 100644 --- a/lib/dropper.dart +++ b/lib/dropper.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:io'; +import 'package:alic/log.dart'; import 'package:alic/imagefiles.dart'; import 'package:flutter/material.dart'; import 'package:super_clipboard/super_clipboard.dart'; @@ -64,7 +65,7 @@ class _ImageDropRegionState extends State { formats: Formats.standardFormats, hitTestBehavior: HitTestBehavior.opaque, onDropOver: (event) { - debugPrint('onDropOver'); + log.d('onDropOver'); for (var item in event.session.items) { for (var format in formats) { if (item.canProvide(format)) { @@ -75,15 +76,15 @@ class _ImageDropRegionState extends State { return DropOperation.none; }, onDropEnter: (event) { - debugPrint('onDropEnter'); + log.d('onDropEnter'); showOverlay(); }, onDropLeave: (event) { - debugPrint('onDropLeave'); + log.d('onDropLeave'); hideOverlay(); }, onPerformDrop: (event) async { - debugPrint('onPerformDrop'); + log.d('onPerformDrop'); final items = event.session.items; final mixedPaths = []; for (var item in items) { diff --git a/lib/imagefiles.dart b/lib/imagefiles.dart index 1319d96..14889f0 100644 --- a/lib/imagefiles.dart +++ b/lib/imagefiles.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:io'; +import 'package:alic/log.dart'; import 'package:alic/compressor.dart'; import 'package:flutter/material.dart'; import 'package:signals/signals.dart' as signals; @@ -181,7 +182,7 @@ class ImageFiles { static void compress(ImageFile file) { compressor(file, (p0) { - debugPrint('Update: $p0'); + log.d('Update: $p0'); update(p0); }); } diff --git a/lib/log.dart b/lib/log.dart new file mode 100644 index 0000000..dea991a --- /dev/null +++ b/lib/log.dart @@ -0,0 +1,3 @@ +import 'package:logger/logger.dart'; + +final log = Logger(); diff --git a/lib/main.dart b/lib/main.dart index e70fdae..dd65bb3 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,6 +5,7 @@ import 'package:alic/imagefiles.dart'; import 'package:alic/settings.dart'; import 'package:alic/src/rust/frb_generated.dart'; import 'package:alic/table.dart'; +import 'package:alic/log.dart'; import 'package:file_selector/file_selector.dart'; import 'package:flutter/material.dart'; import 'package:signals/signals_flutter.dart'; @@ -63,7 +64,7 @@ class _HomePageState extends State { body: MacMenuBar( child: ImageDropRegion( onDrop: (files) { - debugPrint('Dropped: $files'); + log.d('Dropped: $files'); ImageFiles.add(files); }, dropOverlay: Container( diff --git a/lib/table.dart b/lib/table.dart index ed84866..8de337a 100644 --- a/lib/table.dart +++ b/lib/table.dart @@ -3,6 +3,7 @@ import 'package:data_table_2/data_table_2.dart'; import 'package:flutter/material.dart'; import 'package:open_file_macos/open_file_macos.dart'; import 'package:signals/signals_flutter.dart'; +import 'package:alic/log.dart'; final _openFileMacosPlugin = OpenFileMacos(); @@ -20,7 +21,7 @@ class _FilesTableState extends State with SignalsMixin { @override void initState() { - debugPrint('initState'); + log.d('initState'); super.initState(); createEffect(() { setState(() { diff --git a/lib/update.dart b/lib/update.dart index 36e7b64..ecb5972 100644 --- a/lib/update.dart +++ b/lib/update.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:package_info_plus/package_info_plus.dart'; import 'package:url_launcher/url_launcher.dart'; +import 'package:alic/log.dart'; import 'strings.dart'; @@ -52,10 +53,10 @@ Future checkForUpdate({bool force = false}) async { // 3. Compare if (latestBuildNumber > currentBuildNumber || force) { - debugPrint('Update available: ${update.version}'); + log.d('Update available: ${update.version}'); return update; } else { - debugPrint('You have the latest version'); + log.d('You have the latest version'); return null; } } @@ -63,5 +64,5 @@ Future checkForUpdate({bool force = false}) async { void main() async { final buildNumber = await getLatestBuildNumber(); // ignore: avoid_print - print('Latest Build Number: $buildNumber'); + log.d('Latest Build Number: $buildNumber'); } diff --git a/lib/workqueue.dart b/lib/workqueue.dart index f7382f8..a96247e 100644 --- a/lib/workqueue.dart +++ b/lib/workqueue.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'dart:io'; import 'dart:math'; +import 'package:alic/log.dart'; class _WorkQueue { int _maxWorkers = max(2, Platform.numberOfProcessors - 2); @@ -46,9 +47,9 @@ void main(List args) { var queue = _WorkQueue(maxWorkers: 4); for (var i = 0; i < 10; i++) { queue.add(() async { - print('Running $i'); + log.d('Running $i'); await Future.delayed(const Duration(seconds: 1)); - print('Done $i'); + log.d('Done $i'); }); } queue.join(); diff --git a/macos/Podfile.lock b/macos/Podfile.lock index 5a4e345..2d68ad8 100644 --- a/macos/Podfile.lock +++ b/macos/Podfile.lock @@ -73,4 +73,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: f0c21717cb7ee9112f915044c74bfceb5b12e02a -COCOAPODS: 1.15.2 +COCOAPODS: 1.16.2 diff --git a/pubspec.lock b/pubspec.lock index efc1cf7..d007866 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -162,10 +162,10 @@ packages: dependency: transitive description: name: cli_util - sha256: c05b7406fdabc7a49a3929d4af76bcaccbbffcbcdcf185b082e1ae07da323d19 + sha256: ff6785f7e9e3c38ac98b2fb035701789de90154024a75b6cb926445e83197d1c url: "https://pub.dev" source: hosted - version: "0.4.1" + version: "0.4.2" clock: dependency: transitive description: @@ -178,10 +178,10 @@ packages: dependency: transitive description: name: code_builder - sha256: f692079e25e7869c14132d39f223f8eec9830eb76131925143b2129c4bb01b37 + sha256: "0ec10bf4a89e4c613960bf1e8b42c64127021740fb21640c29c909826a5eea3e" url: "https://pub.dev" source: hosted - version: "4.10.0" + version: "4.10.1" collection: dependency: transitive description: @@ -596,6 +596,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.0.0" + logger: + dependency: "direct main" + description: + name: logger + sha256: "697d067c60c20999686a0add96cf6aba723b3aa1f83ecf806a8097231529ec32" + url: "https://pub.dev" + source: hosted + version: "2.4.0" logging: dependency: transitive description: @@ -1004,10 +1012,10 @@ packages: dependency: transitive description: name: url_launcher_android - sha256: "0dea215895a4d254401730ca0ba8204b29109a34a99fb06ae559a2b60988d2de" + sha256: "6fc2f56536ee873eeb867ad176ae15f304ccccc357848b351f6f0d8d4a40d193" url: "https://pub.dev" source: hosted - version: "6.3.13" + version: "6.3.14" url_launcher_ios: dependency: transitive description: @@ -1124,10 +1132,10 @@ packages: dependency: transitive description: name: win32 - sha256: e1d0cc62e65dc2561f5071fcbccecf58ff20c344f8f3dc7d4922df372a11df1f + sha256: "84ba388638ed7a8cb3445a320c8273136ab2631cd5f2c57888335504ddab1bc2" url: "https://pub.dev" source: hosted - version: "5.7.1" + version: "5.8.0" win32_registry: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e54100f..31404c9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,6 +28,7 @@ dependencies: http: ^1.2.1 url_launcher: ^6.2.6 custom_sliding_segmented_control: ^1.8.3 + logger: ^2.4.0 dev_dependencies: flutter_test: diff --git a/rust/src/api/compressor.rs b/rust/src/api/compressor.rs index cb5fc83..799884a 100644 --- a/rust/src/api/compressor.rs +++ b/rust/src/api/compressor.rs @@ -41,11 +41,12 @@ pub fn process_img(parameters: Parameters) -> Result { let out_path = get_out_path(¶meters, new_image_type); let csparams = create_csparameters(¶meters, img.width(), img.height()); + drop(img); let should_convert = new_image_type != original_image_type; let result = if should_convert { - convert_image(¶meters.path, &out_path, csparams)? + convert_image(¶meters.path, &out_path, csparams, new_image_type)? } else { compress_image(¶meters.path, &out_path, csparams)? }; @@ -146,13 +147,26 @@ fn compress_image(path: &str, out_path: &str, mut params: CSParameters) -> Resul } } -fn convert_image(path: &str, out_path: &str, mut params: CSParameters) -> Result { +fn convert_image( + path: &str, + out_path: &str, + mut params: CSParameters, + image_type: ImageType, +) -> Result { + let supported_type = match image_type { + ImageType::JPEG => caesium::SupportedFileTypes::Jpeg, + ImageType::PNG => caesium::SupportedFileTypes::Png, + ImageType::WEBP => caesium::SupportedFileTypes::WebP, + ImageType::GIF => caesium::SupportedFileTypes::Gif, + ImageType::TIFF => caesium::SupportedFileTypes::Tiff, + }; let result = caesium::convert( path.to_string(), out_path.to_string(), &mut params, - caesium::SupportedFileTypes::WebP, + supported_type, ); + match result { Ok(_) => Ok("Success".to_string()), Err(err) => Err(format!("Error: {}", err)),