Skip to content

Commit

Permalink
Send old images to trash instead of nuking them
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Nov 3, 2024
1 parent 52e99bc commit 18dff33
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
5 changes: 3 additions & 2 deletions lib/compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:alic/imagefiles.dart';
import 'package:alic/log.dart';
import 'package:alic/src/rust/api/compressor.dart';
import 'package:alic/trash.dart';
import 'package:alic/workqueue.dart';

import './config.dart';
Expand Down Expand Up @@ -51,7 +52,7 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) {
if (sizeAfterOptimization.toDouble() / imageFile.size >
compressionThreshold) {
// delete the file if it's not smaller
outFile.delete();
trash(outFile);
callback(imageFile.copyWith(
status: ImageFileStatus.unoptimized,
));
Expand All @@ -60,7 +61,7 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) {
// Success!
if (!config.enablePostfix) {
// If postfix is disabled, replace the original file with the optimized one
File(imageFile.path).delete();
trash(File(imageFile.path));
outFile.rename(replaceLast(outFile.path, config.postfix, ''));
}
callback(imageFile.copyWith(
Expand Down
21 changes: 21 additions & 0 deletions lib/trash.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:io';

import 'package:flutter/services.dart';

import 'log.dart';

class NativeFileManager {
static const MethodChannel _channel = MethodChannel('io.kbl.alic');

static Future<void> trashItem(String filePath) async {
try {
await _channel.invokeMethod('trash', {'filePath': filePath});
} on PlatformException catch (e) {
log.d("Failed to trash item: '${e.message}'.");
}
}
}

trash(File file) {
NativeFileManager.trashItem(file.path);
}
56 changes: 46 additions & 10 deletions macos/Runner/MainFlutterWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,50 @@ import Cocoa
import FlutterMacOS

class MainFlutterWindow: NSWindow {
override func awakeFromNib() {
let flutterViewController = FlutterViewController()
let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)
self.setContentSize(NSSize(width: 600, height: 400))
RegisterGeneratedPlugins(registry: flutterViewController)

super.awakeFromNib()
}
override func awakeFromNib() {
let flutterViewController = FlutterViewController()
let windowFrame = self.frame
self.contentViewController = flutterViewController
self.setFrame(windowFrame, display: true)
self.setContentSize(NSSize(width: 600, height: 400))

let trashChannel = FlutterMethodChannel(
name: "io.kbl.alic",
binaryMessenger: flutterViewController.engine.binaryMessenger)
trashChannel.setMethodCallHandler { (call, result) in
switch call.method {
case "trash":
if let args = call.arguments as? [String: Any],
let filePath = args["filePath"] as? String
{
self.trashItem(filePath: filePath, result: result)
} else {
result(
FlutterError(
code: "INVALID_ARGUMENTS", message: "File path is required",
details: nil))
}
default:
result(FlutterMethodNotImplemented)
}
}

RegisterGeneratedPlugins(registry: flutterViewController)
super.awakeFromNib()
}

private func trashItem(filePath: String, result: FlutterResult) {
let fileURL = URL(fileURLWithPath: filePath)
do {
var resultingURL: NSURL?
try FileManager.default.trashItem(at: fileURL, resultingItemURL: &resultingURL)
result(nil)
} catch {
result(
FlutterError(
code: "TRASH_FAILED",
message: "Failed to trash the item: \(error.localizedDescription)", details: nil
))
}
}
}

0 comments on commit 18dff33

Please sign in to comment.