Skip to content

Commit

Permalink
fix: minor watcher details
Browse files Browse the repository at this point in the history
  • Loading branch information
jdaar committed May 9, 2023
1 parent c170c07 commit fcf9a5d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
Binary file added fsBuddyClient/Assets/Icons/delete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions fsBuddyClient/ViewModel/DeleteWatcherCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Client.Model;
using Client.View;
using ConnectionInterface;

namespace Client.ViewModel
{
public class DeleteWatcherCommand : ICommand
{
private readonly ServiceConnection _serviceConnection;

public event EventHandler CanExecuteChanged { add { } remove { } }

public DeleteWatcherCommand(ServiceConnection serviceConnection) {
_serviceConnection = serviceConnection;
}

public bool CanExecute(object parameter)
{
return true;
}

public async void Execute(object parameter)
{
Watcher watcher = (Watcher)parameter;

var request = new PipeRequest
{
Command = t_PipeCommand.DELETE_WATCHER,
Payload = new PipeRequestPayload
{
WatcherId = watcher.Id
}
};

var response = await _serviceConnection.SendPipeRequest(request);

if (response == null)
{
MessageBox.Show("Couldn't retrieve service response", "Pipe error", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}

await _serviceConnection.RefreshWatchers();

if (response?.Status == t_ResponseStatus.SUCCESS)
{
MessageBox.Show("Watcher was succesfully deleted", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
}
16 changes: 13 additions & 3 deletions fsBuddyService/FileSystemWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ private void PopulateWatcherActions() {
t_WatcherAction.MOVE,
delegate (Watcher watcher, FileSystemEventArgs fsEvent)
{
RegisterExecutionCallback?.Invoke(watcher.Id);
Log.Information("From watcher action {@watcher} and event {@fsEvent}", watcher, fsEvent);

if (watcher.OutputPath == null)
Expand All @@ -38,7 +37,7 @@ private void PopulateWatcherActions() {
return;

var splitFileName = fsEvent.Name.Split('.');
var partFilter = $"{String.Join("", splitFileName.Take(splitFileName.Length - 1))}*.part";
var partFilter = $"{String.Join(".", splitFileName.Take(splitFileName.Length - 1))}*.part";

var fileExists = File.Exists(fsEvent.FullPath);

Expand All @@ -48,6 +47,8 @@ private void PopulateWatcherActions() {

var fileOutputExists = File.Exists(Path.Combine(watcher.OutputPath, fsEvent.Name));

var fileIsEmpty = new FileInfo(fsEvent.FullPath).Length == 0;

Log.Information("The file {partFilter} exists: {fileIsDownloading}", partFilter, fileIsDownloading);

if (!fileExists)
Expand All @@ -60,6 +61,11 @@ private void PopulateWatcherActions() {
Log.Information("File is not ready yet");
return;
}
if (fileIsEmpty)
{
Log.Information("File is empty");
return;
}
if (!fileOutputExists)
{
try
Expand All @@ -76,7 +82,10 @@ private void PopulateWatcherActions() {

var fileExtension = splitFilename.Last();
var fileName = String.Join('.', splitFilename.Take(splitFilename.Length - 1));
fileName = $"{fileName} (new).{fileExtension}";

var fileIndex = Directory.GetFiles(watcher.OutputPath, $"{fileName}*.{fileExtension}").Length;

fileName = $"{fileName} ({fileIndex}).{fileExtension}";

Log.Information("Attempting to execute MOVE action with filename {fileName}", fileName);
try
Expand All @@ -87,6 +96,7 @@ private void PopulateWatcherActions() {
Log.Error(error, "Couldn't execute MOVE action");
}
}
RegisterExecutionCallback?.Invoke(watcher.Id);
}
);
IsWatcherActionsInitialized = true;
Expand Down

0 comments on commit fcf9a5d

Please sign in to comment.