Skip to content
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
530 changes: 276 additions & 254 deletions App.Cmd/ViewModels/SaveViewModel.cs

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions App.Core/Models/LoggerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
{
public class LoggerModel
{


public string Name { get; set; } = string.Empty; // Name of the save
public string FileSource { get; set; } = string.Empty; // Path of the source file
public string FileTarget { get; set; } = string.Empty; // Path of the target file
public string FileSize { get; set; } = string.Empty; // Size of the file
public string FileTransferTime { get; set; } = string.Empty; // Time of the transfer
public string FileEncryptionTime { get; set; } = string.Empty; // Time of the encryption
public DateTime Time { get; set; } = DateTime.Now; // Date of the save

}
}
3 changes: 1 addition & 2 deletions App.Core/Models/SaveModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class SaveModel
public string OutPath { get; set; } = ""; // Path of the target file
public string Type { get; set; } = ""; // Type of the save // false = Complete, true = Sequentiel
public string SaveName { get; set; } = "" ; // Name of the save
public DateTime Date { get; set; } // Date of the save
public StateManagerModel StateManager { get; set; } = new(); // State of the save
public DateTime Date { get; } = DateTime.Now; // Date of the save
}
}
1 change: 1 addition & 0 deletions App.Core/Models/StateManagerModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace App.Core.Models
{

public class StateManagerModel
{
public string SaveName { get; set; } = string.Empty; // Name of the save
Expand Down
2 changes: 1 addition & 1 deletion App.Core/Resources/ResourcesEN-UK.resx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<value>1-3 =&gt; 1 to 3 |</value>
</data>
<data name="ListAnswer2" xml:space="preserve">
<value>1,3 =&gt; 1 and 3 |</value>
<value>1;3 =&gt; 1 and 3 |</value>
</data>
<data name="MaxSaveError" xml:space="preserve">
<value>Error : You have reached the maximum number of saves</value>
Expand Down
2 changes: 1 addition & 1 deletion App.Core/Resources/ResourcesFR-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
<value>1-3 =&gt; 1 à 3 |</value>
</data>
<data name="ListAnswer2" xml:space="preserve">
<value>1,3 =&gt; 1 et 3 |</value>
<value>1;3 =&gt; 1 et 3 |</value>
</data>
<data name="MaxSaveError" xml:space="preserve">
<value>Erreur : Vous avez atteint le nombre maximum de sauvegardes</value>
Expand Down
233 changes: 123 additions & 110 deletions App.Core/Services/CopyService.cs
Original file line number Diff line number Diff line change
@@ -1,136 +1,149 @@
using App.Core.Models;
using System.Resources;
using System.IO;
using System.Threading;

namespace App.Core.Services
{
public class CopyService
{
private readonly LoggerService loggerService = new();
private readonly StateManagerService stateManagerService = new();

/// <summary>
/// Method to execute the copy service
/// </summary>
/// <param name="copyModel"></param>
/// <param name="saveModel"></param>
/// <param name="listSavesModel"></param>
/// <param name="listStateManager"></param>
public void RunCopy(CopyModel copyModel, SaveModel saveModel)
public LoggerService loggerService = new();
public StateManagerService stateManagerService = new();
private bool isStopped;
private bool isPaused;

public CopyModel CopyModel { get; set; }

public CopyService()
{
string? Output;
Output = DisplayService.GetResource("IsRunning");
Console.WriteLine(saveModel.SaveName + Output);
this.CopyModel = new();
}

// Check if the source path exists
if (File.Exists(copyModel.SourcePath))
{
// File copying operation
File.Copy(copyModel.SourcePath, copyModel.TargetPath, true);
Output = DisplayService.GetResource("FileCopied");
Console.WriteLine(Output);
public CopyService(StateManagerService stateManagerService)
{
this.CopyModel = new();
this.stateManagerService = stateManagerService;
}

}
// Check if the source path is a directory
else if (Directory.Exists(copyModel.SourcePath))
{
// Directory copying operation
CopyDirectory(copyModel.SourcePath, copyModel.TargetPath, saveModel);
Output = DisplayService.GetResource("DirectoryCopied");
Console.WriteLine(Output);
public void ExecuteCopy(SaveModel saveModel)
{
isPaused = false;
isStopped = false;
ProcessCopy(saveModel);
}

}
// If the source path does not exist
else
public void PauseCopy(SaveModel saveModel)
{
isPaused = true;
// Implement any necessary logic for pausing the copying process
}

public void StopCopy(SaveModel saveModel)
{
isStopped = true;
// Implement any necessary logic for stopping the copying process
}

public void ResumeCopy(SaveModel saveModel)
{
isPaused = false;
// Implement any necessary logic for resuming the copying process
ProcessCopy(saveModel);
}

private void ProcessCopy(SaveModel saveModel)
{
if (isPaused || isStopped)
return;

foreach (StateManagerModel stateModel in stateManagerService.listStateModel!)
{
Output = DisplayService.GetResource("SourceError");
Console.WriteLine(Output);
if (stateModel.SaveName == saveModel.SaveName)
{
stateModel.State = "ACTIVE";
stateManagerService.UpdateStateFile();

if (System.IO.File.Exists(this.CopyModel.SourcePath))
{
// File copying operation
System.IO.File.Copy(this.CopyModel.SourcePath, this.CopyModel.TargetPath, true);
stateModel.SourceFilePath = this.CopyModel.SourcePath;
stateModel.TargetFilePath = this.CopyModel.TargetPath;

loggerService!.loggerModel!.Name = saveModel.SaveName;
loggerService.loggerModel.FileSource = this.CopyModel.SourcePath;
loggerService.loggerModel.FileTarget = this.CopyModel.TargetPath;
loggerService.loggerModel.FileSize = new FileInfo(this.CopyModel.SourcePath).Length.ToString();
loggerService.AddEntryLog();
stateManagerService.UpdateStateFile();
}
else if (Directory.Exists(this.CopyModel.SourcePath))
{
// Directory copying operation
CopyDirectory(this.CopyModel.SourcePath, this.CopyModel.TargetPath, stateModel);
}
else
{
// Log the error and handle it gracefully
loggerService!.loggerModel!.Name = saveModel.SaveName;
loggerService.AddEntryLog();
}

stateModel.State = "END";
stateManagerService.UpdateStateFile();
}
}
}

/// <summary>
/// Method to copy a directory
/// </summary>
/// <param name="sourceDirPath"></param>
/// <param name="targetDirPath"></param>
/// <param name="saveModel"></param>
/// <param name="listSavesModel"></param>
/// <param name="listStateManager"></param>
private void CopyDirectory(string sourceDirPath, string targetDirPath, SaveModel saveModel)

private void CopyDirectory(string sourceDirPath, string targetDirPath, StateManagerModel stateModel)
{
// Check for nulls
ArgumentNullException.ThrowIfNull(sourceDirPath);
ArgumentNullException.ThrowIfNull(targetDirPath);
ArgumentNullException.ThrowIfNull(saveModel);

// Get the total number of files and the total size of the files
int TotalFilesCount = Directory.GetFiles(sourceDirPath, "*", SearchOption.AllDirectories).Length;
long TotalFilesSize = Directory.GetFiles(sourceDirPath, "*", SearchOption.AllDirectories).Sum(f => new FileInfo(f).Length);

// Update the state manager
saveModel.StateManager.SaveName = saveModel.SaveName;
saveModel.StateManager.TotalFilesSize = TotalFilesSize;
saveModel.StateManager.TotalFilesToCopy = TotalFilesCount;
saveModel.StateManager.NbFilesLeftToDo = TotalFilesCount;
saveModel.StateManager.State = "ACTIVE";
int Percentage = 0;

//Calculate the progression
saveModel.StateManager.Progression = (Percentage / Directory.GetFiles(sourceDirPath, "*", SearchOption.AllDirectories).Length);
try
if (isPaused || isStopped)
return;

// Create the target directory if it does not exist yet
if (!Directory.Exists(targetDirPath))
{
//stateManagerService.UpdateState(listStateManager, saveModel);
Directory.CreateDirectory(targetDirPath);
}
catch (Exception e)

// Get the list of files and subdirectories in the source directory
string[] files = Directory.GetFiles(sourceDirPath);
string[] subdirectories = Directory.GetDirectories(sourceDirPath);

// Update total files to copy
stateModel.TotalFilesToCopy = Directory.GetFiles(sourceDirPath, "*.*", SearchOption.AllDirectories).Length;
stateManagerService.UpdateStateFile();

// Copy files
foreach (string filePath in files)
{
Console.WriteLine($"Error: {e.Message}");
if (isPaused || isStopped)
return;

string fileName = Path.GetFileName(filePath);
string targetFilePath = Path.Combine(targetDirPath, fileName);
System.IO.File.Copy(filePath, targetFilePath, true); // The 'true' parameter allows overwriting the file if it already exists in the target directory
loggerService!.loggerModel!.Name = stateModel.SaveName;
loggerService.loggerModel.FileSource = this.CopyModel.SourcePath;
loggerService.loggerModel.FileTarget = this.CopyModel.TargetPath;
loggerService.loggerModel.FileSize = new FileInfo(this.CopyModel.SourcePath).Length.ToString();
loggerService.AddEntryLog();

// Update state model
stateModel.NbFilesLeftToDo--;
stateModel.Progression = (int)(((double)(stateModel.TotalFilesToCopy - stateModel.NbFilesLeftToDo) / stateModel.TotalFilesToCopy) * 100);
stateModel.SourceFilePath = filePath;
stateModel.TargetFilePath = targetFilePath;
stateManagerService.UpdateStateFile();
}

// Copy files recursively
foreach (string filePath in Directory.GetFiles(sourceDirPath, "*", SearchOption.AllDirectories))
// Recursively copy subdirectories
foreach (string subdirectory in subdirectories)
{
Percentage += 1;
// Get the file name
string FileName = Path.GetFileName(filePath);
// Get the destination file path
string DestinationFilePath = Path.Combine(targetDirPath, FileName);
// Create the directory if it does not exist
System.IO.Directory.CreateDirectory(targetDirPath);

// Create a log model
LoggerModel logModel = new()
{
Name = saveModel.SaveName,
FileSource = filePath,
FileTarget = DestinationFilePath,
FileSize = new FileInfo(filePath).Length.ToString()
};
// File copying operation
File.Copy(filePath, DestinationFilePath, true);

// Write the log
logModel.FileTransferTime = (DateTime.Now - logModel.Time).ToString();
loggerService.WriteLog(logModel, saveModel);
saveModel.StateManager.NbFilesLeftToDo = saveModel.StateManager.NbFilesLeftToDo - 1;
saveModel.StateManager.Progression = ((Percentage * 100) / Directory.GetFiles(sourceDirPath, "*", SearchOption.AllDirectories).Length);
saveModel.StateManager.TargetFilePath = DestinationFilePath;
saveModel.StateManager.SourceFilePath = filePath;

//Delay for avoid the stateManagerService to update the state too fast

try
{
//stateManagerService.UpdateState(listStateManager, saveModel);
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
string subdirectoryName = Path.GetFileName(subdirectory);
string targetSubdirectoryPath = Path.Combine(targetDirPath, subdirectoryName);
CopyDirectory(subdirectory, targetSubdirectoryPath, stateModel); // Recursive call to copy subdirectories
}

// Update the state manager
saveModel.StateManager.State = "END";
//stateManagerService.UpdateState(listStateManager, saveModel);
}
}
}
2 changes: 1 addition & 1 deletion App.Core/Services/DisplayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static void DisplayWelcomeMessage()
" \r\n |___/ " +
" \r\n ");
Console.WriteLine(" Developped by \n ");
Console.WriteLine(" Louis JAGUENEAU - Nathan LORIT - Julien DESPEZ - Paul PESCHEL");
Console.WriteLine(" Louis JAGUENEAU - Nathan LORIT - Julien DESPREZ - Paul PESCHEL");
Console.ResetColor();
}
/// <summary>
Expand Down
40 changes: 29 additions & 11 deletions App.Core/Services/LoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,27 @@ namespace App.Core.Services
{
public class LoggerService
{

public LoggerModel? loggerModel;
private readonly string jsonlogFilePath = "logs.json";
private readonly string xmlLogFilePath = "logs.xml";
private readonly JsonSerializerOptions options = new()
{
WriteIndented = true
};

/// <summary>
/// Method to write a log entry
/// </summary>
/// <param name="loggerModel"></param>
/// <param name="saveModel"></param>
public void WriteLog(LoggerModel loggerModel, SaveModel saveModel)
public LoggerService()
{
loggerModel = new();
}


public void WriteLog()
{
// Check for nulls
//TODO : Gerer les exeptions
ArgumentNullException.ThrowIfNull(loggerModel);
ArgumentNullException.ThrowIfNull(saveModel);

try
{
loggerModel.Name = saveModel.SaveName;
{
// Serialize the log model to JSON

string logEntry = JsonSerializer.Serialize(loggerModel, options) + ",";
Expand Down Expand Up @@ -61,5 +59,25 @@ public void OpenLogFile()
// Open the log file in notepad
System.Diagnostics.Process.Start("notepad.exe", jsonlogFilePath);
}

public void ClearLogFile()
{
// Clear the log file
File.WriteAllText(jsonlogFilePath, "[]");
}

public void CreateLogFile()
{
// Create the log file
File.WriteAllText(jsonlogFilePath, "");
}

public void AddEntryLog()
{
using StreamWriter logWriter = File.AppendText(jsonlogFilePath);
logWriter.WriteLineAsync(JsonSerializer.Serialize(logWriter, options) + ",");
}
}


}
Loading