Skip to content

Commit

Permalink
#2024.07.29
Browse files Browse the repository at this point in the history
- Reduced storage validation errors message size.
  • Loading branch information
drweb86 committed Jul 29, 2024
1 parent 0634b52 commit bb7c1fc
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 74 deletions.
10 changes: 10 additions & 0 deletions help/Version History (Changelog).md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 2024.07.29

## Changes

- Reduced storage validation errors message size.

## Bug Fixes

- FTPS validation could prevent creation of task.

# 2024.07.16

## Changes
Expand Down
5 changes: 3 additions & 2 deletions sources/BUtil.Core/Logs/FileLog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using BUtil.Core.Localization;
using BUtil.Core.Misc;
using System;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -40,7 +41,7 @@ public FileLog(string taskName)
}
catch (ArgumentException e)
{
throw new LogException(e.Message);
throw new LogException(ExceptionHelper.ToString(e));
}
_taskName = taskName;
}
Expand All @@ -61,7 +62,7 @@ public override void Open()
}
catch (Exception e)
{
throw new LogException(e.Message, e);
throw new LogException(ExceptionHelper.ToString(e), e);
}
}

Expand Down
30 changes: 30 additions & 0 deletions sources/BUtil.Core/Misc/ExceptionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Text;

namespace BUtil.Core.Misc;

public static class ExceptionHelper
{
public static string ToString(Exception ex)
{
var builder = new StringBuilder();
builder.Append(ex.Message);

int depth = 5;
Exception? innerException;

do
{
depth--;
innerException = ex.InnerException;
if (innerException == null)
break;

builder.AppendLine();
builder.Append(innerException.Message);
}
while (depth > 0);

return builder.ToString();
}
}
8 changes: 4 additions & 4 deletions sources/BUtil.Core/Misc/ExecuteFailover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static void TryNTimes(Action<string> logError, Action func, int times = 1
catch (Exception e)
{
logError(e.ToString());
if (--times <= 0)
throw new InvalidOperationException("No more attempts left");
logError("Retrying in 30 seconds");
Thread.Sleep(30 * 1000);
if (--times <= 0)
throw;
}
}
}
Expand All @@ -36,10 +36,10 @@ public static T TryNTimes<T>(Action<string> logError, Func<T> func, int times =
catch (Exception e)
{
logError(e.ToString());
if (--times <= 0)
throw new InvalidOperationException("No more attempts left");
logError("Retrying in 30 seconds");
Thread.Sleep(30 * 1000);
if (--times <= 0)
throw;
}
}
throw new NotImplementedException();
Expand Down
2 changes: 1 addition & 1 deletion sources/BUtil.Core/Misc/ImproveIt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void ProcessUnhandledException(Exception exception)
builder.AppendLine("BUtil " + CopyrightInfo.Version + " - Bug report (" + DateTime.Now.ToString("g", CultureInfo.InvariantCulture) + ")");
builder.AppendLine("Please report about it here: ");
builder.AppendLine(ApplicationLinks.HomePage);
builder.AppendLine(exception.Message);
builder.AppendLine(ExceptionHelper.ToString(exception));
builder.AppendLine(exception.StackTrace);
builder.AppendLine(exception.Source);

Expand Down
31 changes: 20 additions & 11 deletions sources/BUtil.Core/Storages/FailoverStorageWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

namespace BUtil.Core.Storages;

public class FailoverStorageWrapper(ILog log, IStorage storage) : IStorage
public class FailoverStorageWrapper(ILog log, IStorage storage, int? triesCount) : IStorage
{
private readonly ILog _log = log;
private readonly IStorage _storage = storage;
private readonly int _triesCount = triesCount ?? 10;

public void Delete(string file)
{
Expand All @@ -29,60 +30,68 @@ public void Download(string relativeFileName, string targetFileName)
{
ExecuteFailover.TryNTimes(
error => _log.WriteLine(LoggingEvent.Error, $"Download \"{relativeFileName}\" to \"{targetFileName}\": {error}"),
() => _storage.Download(relativeFileName, targetFileName));
() => _storage.Download(relativeFileName, targetFileName),
_triesCount);
}

public bool Exists(string relativeFileName)
{
return ExecuteFailover.TryNTimes(
error => _log.WriteLine(LoggingEvent.Error, $"Exists \"{relativeFileName}\": {error}"),
() => _storage.Exists(relativeFileName));
() => _storage.Exists(relativeFileName),
_triesCount);
}

public void DeleteFolder(string relativeFolderName)
{
ExecuteFailover.TryNTimes(
error => _log.WriteLine(LoggingEvent.Error, $"Delete folder \"{relativeFolderName}\": {error}"),
() => _storage.DeleteFolder(relativeFolderName));
() => _storage.DeleteFolder(relativeFolderName),
_triesCount);
}

public string[] GetFolders(string relativeFolderName, string? mask = null)
{
return ExecuteFailover.TryNTimes(
error => _log.WriteLine(LoggingEvent.Error, $"Get folders \"{relativeFolderName}\" by mask \"{mask}\": {error}"),
() => _storage.GetFolders(relativeFolderName, mask));
() => _storage.GetFolders(relativeFolderName, mask),
_triesCount);
}

public string? Test(bool writeMode)
public string? Test()
{
return _storage.Test(writeMode);
return _storage.Test();
}

public IStorageUploadResult Upload(string sourceFile, string relativeFileName)
{
return ExecuteFailover.TryNTimes(
error => _log.WriteLine(LoggingEvent.Error, $"Upload \"{sourceFile}\" to \"{relativeFileName}\": {error}"),
() => _storage.Upload(sourceFile, relativeFileName));
() => _storage.Upload(sourceFile, relativeFileName),
_triesCount);
}

public string[] GetFiles(string? relativeFolderName = null, SearchOption option = SearchOption.TopDirectoryOnly)
{
return ExecuteFailover.TryNTimes(
error => _log.WriteLine(LoggingEvent.Error, $"Get files \"{relativeFolderName}\" with option \"{option}\": {error}"),
() => _storage.GetFiles(relativeFolderName, option));
() => _storage.GetFiles(relativeFolderName, option),
_triesCount);
}

public DateTime GetModifiedTime(string relativeFileName)
{
return ExecuteFailover.TryNTimes(
error => _log.WriteLine(LoggingEvent.Error, $"Get file \"{relativeFileName}\" modified date: {error}"),
() => _storage.GetModifiedTime(relativeFileName));
() => _storage.GetModifiedTime(relativeFileName),
_triesCount);
}

public void Move(string fromRelativeFileName, string toRelativeFileName)
{
ExecuteFailover.TryNTimes(
error => _log.WriteLine(LoggingEvent.Error, $"Move file \"{fromRelativeFileName}\" to \"{toRelativeFileName}\": {error}"),
() => _storage.Move(fromRelativeFileName, toRelativeFileName));
() => _storage.Move(fromRelativeFileName, toRelativeFileName),
_triesCount);
}
}
10 changes: 1 addition & 9 deletions sources/BUtil.Core/Storages/FolderStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,13 @@ private void Unmount()
}
}

public override string? Test(bool writeMode)
public override string? Test()
{
if (!Directory.Exists(Settings.DestinationFolder))
return string.Format(Localization.Resources.DirectoryStorage_Field_Directory_Validation_NotFound, Settings.DestinationFolder);
if (!Path.IsPathFullyQualified(Settings.DestinationFolder))
return string.Format(Localization.Resources.DirectoryStorage_Field_Directory_Validation_NotFound, Settings.DestinationFolder);

if (writeMode)
{
var folder = Guid.NewGuid().ToString();
var file = Path.Combine("BUtil check " + folder, Guid.NewGuid().ToString());
_ = Upload(Assembly.GetExecutingAssembly().Location, file) ?? throw new Exception("Failed to upload!");
DeleteFolder(folder);
}

return null;
}

Expand Down
14 changes: 3 additions & 11 deletions sources/BUtil.Core/Storages/FtpsStorage.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

using BUtil.Core.ConfigurationFileModels.V2;
using BUtil.Core.Logs;
using BUtil.Core.Misc;
using FluentFTP;
using System;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Reflection;
using System.Security;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -137,7 +137,7 @@ private void Unmount()
_client.Dispose();
}

public override string? Test(bool writeMode)
public override string? Test()
{
if (!string.IsNullOrWhiteSpace(Settings.Folder) && !_client.DirectoryExists(Settings.Folder))
{
Expand All @@ -151,15 +151,7 @@ private void Unmount()
}
catch (Exception e)
{
return e.Message;
}

if (writeMode)
{
var folder = Guid.NewGuid().ToString();
var file = Path.Combine("BUtil check " + folder, Guid.NewGuid().ToString());
_ = Upload(Assembly.GetExecutingAssembly().Location, file) ?? throw new Exception("Failed to upload!");
DeleteFolder(folder);
return ExceptionHelper.ToString(e);
}
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion sources/BUtil.Core/Storages/IStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface IStorage : IDisposable
string[] GetFolders(string relativeFolderName, string? mask = null);
string[] GetFiles(string? relativeFolderName = null, SearchOption option = SearchOption.TopDirectoryOnly);
DateTime GetModifiedTime(string relativeFileName);
string? Test(bool writeMode);
string? Test();
}
2 changes: 1 addition & 1 deletion sources/BUtil.Core/Storages/StorageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class StorageBase<TStorageSettings>(ILog log, TStorageSettings s
protected readonly TStorageSettings Settings = settings;

public abstract IStorageUploadResult Upload(string sourceFile, string relativeFileName);
public abstract string? Test(bool writeMode);
public abstract string? Test();

public abstract void Delete(string relativeFileName);
public abstract void DeleteFolder(string relativeFolderName);
Expand Down
24 changes: 16 additions & 8 deletions sources/BUtil.Core/Storages/StorageFactory.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
using BUtil.Core.ConfigurationFileModels.V2;
using BUtil.Core.Logs;
using BUtil.Core.Misc;
using System;

namespace BUtil.Core.Storages;

public class StorageFactory
{
public static IStorage Create(ILog log, IStorageSettingsV2 storageSettings, bool autodetectConnectionSettings)
public static IStorage Create(ILog log, IStorageSettingsV2 storageSettings, bool autodetectConnectionSettings, int? triesCount = null)
{
if (storageSettings is FolderStorageSettingsV2 folder)
return new FailoverStorageWrapper(log, new FolderStorage(log, folder));
return new FailoverStorageWrapper(log, new FolderStorage(log, folder), triesCount);
else if (storageSettings is SambaStorageSettingsV2 samba)
return new FailoverStorageWrapper(log, PlatformSpecificExperience.Instance.GetSmbStorage(log, samba));
return new FailoverStorageWrapper(log, PlatformSpecificExperience.Instance.GetSmbStorage(log, samba), triesCount);
else if (storageSettings is FtpsStorageSettingsV2 ftps)
return new FailoverStorageWrapper(log, new FtpsStorage(log, ftps, autodetectConnectionSettings));
return new FailoverStorageWrapper(log, new FtpsStorage(log, ftps, autodetectConnectionSettings), triesCount);
else if (storageSettings is MtpStorageSettings mtp)
{
var mtpStorage = PlatformSpecificExperience.Instance.GetMtpStorage(log, mtp) ?? throw new NotSupportedException("Your OS does not support MTP storage");
return new FailoverStorageWrapper(log, mtpStorage);
return new FailoverStorageWrapper(log, mtpStorage, triesCount);
}
throw new ArgumentOutOfRangeException(nameof(storageSettings));
}
Expand All @@ -32,12 +33,19 @@ public static IStorage Create(ILog log, IStorageSettingsV2 storageSettings, bool

try
{
using var storage = Create(log, storageSettings, true);
return storage.Test(writeMode);
using var storage = Create(log, storageSettings, true, 1);
var readonlyChecksError = storage.Test();
if (!string.IsNullOrWhiteSpace(readonlyChecksError))
return readonlyChecksError;

if (writeMode)
StorageHelper.WriteTest(storage);

return null;
}
catch (Exception ex)
{
return ex.Message;
return ExceptionHelper.ToString(ex);
}
}
}
16 changes: 16 additions & 0 deletions sources/BUtil.Core/Storages/StorageHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.IO;
using System.Reflection;

namespace BUtil.Core.Storages;

public class StorageHelper
{
public static void WriteTest(IStorage storage)
{
var folder = "BUtil check " + Guid.NewGuid().ToString();
var file = Path.Combine(folder, Guid.NewGuid().ToString());
storage.Upload(Assembly.GetExecutingAssembly().Location, file);
storage.DeleteFolder(folder);
}
}
4 changes: 2 additions & 2 deletions sources/BUtil.Linux/Services/LinuxSambaStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ killall gvfsd
});
}

public override string? Test(bool writeMode)
public override string? Test()
{
return _proxy.Test(writeMode);
return _proxy.Test();
}

public override IStorageUploadResult Upload(string sourceFile, string relativeFileName)
Expand Down
12 changes: 1 addition & 11 deletions sources/BUtil.Windows/Services/MtpStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using BUtil.Core.Logs;
using BUtil.Core.Storages;
using MediaDevices;
using System.Diagnostics;
using System.Reflection;
using System.Security;

namespace BUtil.Windows.Services;
Expand Down Expand Up @@ -78,21 +76,13 @@ private void Unmount()
_mediaDevice?.Dispose();
}

public override string? Test(bool writeMode)
public override string? Test()
{
if (!_mediaDevice.DirectoryExists(Settings.Folder))
{
return BUtil.Core.Localization.Resources.Field_Folder_Validation_NotExist;
}

if (writeMode)
{
var folder = Guid.NewGuid().ToString();
var file = Path.Combine("BUtil check " + folder, Guid.NewGuid().ToString());
_ = Upload(Assembly.GetExecutingAssembly().Location, file) ?? throw new Exception("Failed to upload!");
DeleteFolder(folder);
}

return null;
}

Expand Down
Loading

0 comments on commit bb7c1fc

Please sign in to comment.