Skip to content

Commit

Permalink
Fixed saving file names with unsupported characters on Windows / Linu…
Browse files Browse the repository at this point in the history
…x / MacOS
  • Loading branch information
DamianMorozov committed Nov 5, 2024
1 parent 224a607 commit aafe39d
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 159 deletions.
4 changes: 2 additions & 2 deletions Clients/TgDownloaderConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
try
{
menu.ShowTableMain(tgDownloadSettings);
tgLog.MarkupLine(tgLocale.TypeAnyKeyForReturn);
Console.ReadKey();
//tgLog.MarkupLine(tgLocale.TypeAnyKeyForReturn);
//Console.ReadKey();
string prompt = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title($" {tgLocale.MenuSwitchNumber}")
Expand Down
286 changes: 131 additions & 155 deletions Core/TgDownloader/Helpers/TgClientHelper.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Core/TgStorage/Common/TgEfRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public virtual TgEfStorageResult<TEntity> SaveOrRecreate(TEntity item, string ta
item = new();
item.Fill(itemBackup, true);
Delete(item, isSkipFind: false);
return Save(itemBackup);
//return Save(itemBackup);
#if DEBUG
Debug.WriteLine(ex);
#endif
Expand All @@ -471,7 +471,7 @@ public virtual async Task<TgEfStorageResult<TEntity>> SaveOrRecreateAsync(TEntit
{
TEntity itemBackup = item;
await DeleteAsync(item, isSkipFind: true);
return await SaveAsync(itemBackup);
//return await SaveAsync(itemBackup);
#if DEBUG
Debug.WriteLine(ex);
#endif
Expand Down
36 changes: 36 additions & 0 deletions Core/TgStorage/Models/TgMediaInfoModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

namespace TgStorage.Models;

public sealed class TgMediaInfoModel(string remote, long size, DateTime dtCreate)
{
public string Remote { get; set; } = remote;
public long Size { get; set; } = size;
public DateTime DtCreate { get; set; } = dtCreate;
public string LocalFileWithNumber { get; set; } = remote;
public string LocalFileWithoutNumber { get; set; } = remote;
public string LocalPath { get; set; } = string.Empty;
public string LocalFullWithNumber => Path.Combine(LocalPath, LocalFileWithNumber);
private static readonly char[] InvalidChars = Path.GetInvalidFileNameChars();

public TgMediaInfoModel() : this(string.Empty, default, default) { }

public void Normalize()
{
LocalFileWithNumber = Normalize(LocalFileWithNumber);
LocalFileWithoutNumber = Normalize(LocalFileWithoutNumber);
}

public string Normalize(string fileName)
{
fileName = fileName.Trim();
// Replace characters in the file name depending on the OS
fileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? fileName.Replace("/", "\\") : fileName.Replace("\\", "/");
return SanitizeFileName(fileName);
}

/// <summary> Replace invalid characters for file names </summary>
private string SanitizeFileName(string fileName) =>
InvalidChars.Aggregate(fileName, (current, c) => current.Replace(c.ToString(), "_"));
}
1 change: 1 addition & 0 deletions Core/TgStorage/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
global using System.IO.Enumeration;
global using System.Linq.Expressions;
global using System.Reflection;
global using System.Runtime.InteropServices;
global using System.Text.RegularExpressions;
global using System.Text;
global using System.Xml.Linq;
Expand Down
1 change: 1 addition & 0 deletions Docs/CHANGELOG-RUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- GitHub workflows actions
## Изменено
- Использование EF Core вместо DevExpress XPO
- Исправлено сохранение имен файлов с неподдерживаемыми символами под Windows / Linux / MacOS
## Добавлено
- Добавлен проект TgDownloaderWinUI

Expand Down
1 change: 1 addition & 0 deletions Docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- GitHub workflows actions
## Changed
- Using EF Core instead of DevExpress XPO
- Fixed saving file names with unsupported characters on Windows / Linux / MacOS
## Added projects
- Added TgDownloaderWinUI app

Expand Down

0 comments on commit aafe39d

Please sign in to comment.