-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed saving file names with unsupported characters on Windows / Linu…
…x / MacOS
- Loading branch information
1 parent
224a607
commit aafe39d
Showing
7 changed files
with
174 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), "_")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters