diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-05-14 14:34:46 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-14 14:34:46 -0500 |
| commit | e22472791be56eb3b44d7af7064400aa5e6ef9fc (patch) | |
| tree | 8b6629c2a81de5889a0084a8f12d5c73b0cfdf36 /src/slskd/Program.cs | |
| parent | 5926fd44420da613b4c4c00d043151916f24b989 (diff) | |
| parent | a0458827d19f21fe3efe40b5630bc18d0e28fd38 (diff) | |
| download | slskd-e22472791be56eb3b44d7af7064400aa5e6ef9fc.tar.gz | |
Merge pull request #70 from slskd/incomplete-downloads
Save incomplete downloads in a separate directory
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 65 |
1 files changed, 57 insertions, 8 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 00d6d5b4..d70b50c1 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -20,6 +20,7 @@ namespace slskd using System; using System.Collections.Generic; using System.ComponentModel; + using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Net; @@ -52,9 +53,9 @@ namespace slskd public static readonly string AppName = "slskd"; /// <summary> - /// The default database filename. + /// The default application data directory. /// </summary> - public static readonly string DefaultDatabaseFile = Path.Combine(AppContext.BaseDirectory, "data", $"{AppName}.db"); + public static readonly string DefaultAppDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), AppName); /// <summary> /// The default configuration filename. @@ -64,7 +65,22 @@ namespace slskd /// <summary> /// The default XML documentation filename. /// </summary> - public static readonly string DefaultXmlDocumentaitonFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml"); + public static readonly string DefaultXmlDocumentationFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml"); + + /// <summary> + /// The default incomplete download directory. + /// </summary> + public static readonly string DefaultIncompleteDirectory = Path.Combine(DefaultAppDirectory, "incomplete"); + + /// <summary> + /// The default downloads directory. + /// </summary> + public static readonly string DefaultDownloadsDirectory = Path.Combine(DefaultAppDirectory, "downloads"); + + /// <summary> + /// The default shared directory. + /// </summary> + public static readonly string DefaultSharedDirectory = Path.Combine(DefaultAppDirectory, "shared"); /// <summary> /// The global prefix for environment variables. @@ -198,6 +214,37 @@ namespace slskd return; } + // ensure the application directory exists and is writeable. the most comprehensive way to test this is to try + // to write a file to it. + try + { + if (!Directory.Exists(Options.Directories.App)) + { + Directory.CreateDirectory(Options.Directories.App); + } + } + catch (Exception ex) + { + Console.WriteLine($"App directory {Options.Directories.App} does not exist, and could not be created: {ex.Message}"); + } + + try + { + if (!Directory.Exists(Options.Directories.App)) + { + Directory.CreateDirectory(Options.Directories.App); + } + + var probe = Path.Combine(Options.Directories.App, "probe"); + File.WriteAllText(Path.Combine(Options.Directories.App, "probe"), string.Empty); + File.Delete(probe); + } + catch (Exception ex) + { + Console.WriteLine($"App directory {Options.Directories.App} is not writeable: {ex.Message}"); + return; + } + if (!Options.NoLogo) { PrintLogo(Version); @@ -374,6 +421,7 @@ namespace slskd { var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(ArgumentAttribute)); var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute)); + var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)); if (attribute != default) { @@ -381,9 +429,9 @@ namespace slskd var longName = (string)attribute.ConstructorArguments[1].Value; var description = descriptionAttribute?.ConstructorArguments[0].Value; - var suffix = property.PropertyType == typeof(bool) ? string.Empty : $" (default: {property.GetValue(defaults) ?? "<null>"})"; - var item = $"{shortName}{(shortName == default ? " " : "|")}--{GetLongName(longName, property.PropertyType)}"; - var desc = $"{description}{suffix}"; + var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? "<null>"})"; + var item = $"{(shortName == default ? " " : $"{shortName}|")}--{GetLongName(longName, property.PropertyType)}"; + var desc = $"{description}{(property.PropertyType == typeof(bool) ? string.Empty : suffix)}"; lines.Add(new(item, desc)); } else @@ -421,15 +469,16 @@ namespace slskd { var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EnvironmentVariableAttribute)); var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute)); + var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)); if (attribute != default) { var name = (string)attribute.ConstructorArguments[0].Value; var description = descriptionAttribute?.ConstructorArguments[0].Value; - var suffix = type == typeof(bool) ? string.Empty : $" (default: {property.GetValue(defaults) ?? "<null>"})"; + var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? "<null>"})"; var item = $"{prefix}{GetName(name, property.PropertyType)}"; - var desc = $"{description}{suffix}"; + var desc = $"{description}{(type == typeof(bool) ? string.Empty : suffix)}"; lines.Add(new(item, desc)); } else |
