//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
namespace slskd
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using slskd.Validation;
///
/// Extensions.
///
public static class Extensions
{
///
/// Returns the directory from the given path, regardless of separator format.
///
/// The path.
/// The directory.
public static string DirectoryName(this string path)
{
var separator = path.Contains('\\') ? '\\' : '/';
var parts = path.Split(separator);
return string.Join(separator, parts.Take(parts.Length - 1));
}
///
/// Returns a "pretty" string representation of the provided Type; specifically, corrects the naming of generic Types
/// and appends the type parameters for the type to the name as it appears in the code editor.
///
/// The type for which the colloquial name should be created.
/// A "pretty" string representation of the provided Type.
public static string ToColloquialString(this Type type)
{
return !type.IsGenericType ? type.Name : type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(a => a.ToColloquialString())) + ">";
}
///
/// Returns the SHA1 hash of the given string.
///
/// The string to hash.
/// The hash.
public static string Sha1(this string str)
{
using var sha1 = new SHA1Managed();
return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty);
}
///
/// Formats byte to nearest size (KB, MB, etc.)
///
/// The value to format.
/// The number of decimal places to include.
/// The formatted string.
public static string SizeSuffix(this double value, int decimalPlaces = 1)
{
string[] sizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
if (value < 0)
{
return "-" + SizeSuffix(-value);
}
if (value == 0)
{
return string.Format("{0:n" + decimalPlaces + "} bytes", 0);
}
// mag is 0 for bytes, 1 for KB, 2, for MB, etc.
int mag = (int)Math.Log(value, 1024);
// 1L << (mag * 10) == 2 ^ (10 * mag) [i.e. the number of bytes in the unit corresponding to mag]
decimal adjustedSize = (decimal)value / (1L << (mag * 10));
// make adjustment when the value is large enough that it would round up to 1000 or more
if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
{
mag += 1;
adjustedSize /= 1024;
}
return string.Format(
"{0:n" + decimalPlaces + "} {1}",
adjustedSize,
sizeSuffixes[mag]);
}
///
/// Converts a fully qualified remote filename to a local filename, swapping directory characters for those specific
/// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the
/// remote store (including the filename and the parent folder).
///
/// The fully qualified remote filename to convert.
/// The converted filename.
public static string ToLocalRelativeFilename(this string remoteFilename)
{
var localFilename = remoteFilename.ToLocalOSPath();
var path = $"{Path.GetDirectoryName(localFilename).Replace(Path.GetDirectoryName(Path.GetDirectoryName(localFilename)), string.Empty)}";
var sanitizedFilename = Path.GetFileName(localFilename);
foreach (var c in Path.GetInvalidFileNameChars())
{
sanitizedFilename = sanitizedFilename.Replace(c, '_');
}
return Path.Combine(path, sanitizedFilename).TrimStart('\\').TrimStart('/');
}
///
/// Converts a fully qualified remote filename to a local filename based in the provided , swapping directory characters for those specific
/// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the
/// remote store (including the filename and the parent folder).
///
/// The fully qualified remote filename to convert.
/// The base directory for the local filename.
/// The converted filename.
public static string ToLocalFilename(this string remoteFilename, string baseDirectory)
{
return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename());
}
///
/// Converts the given path to the local format (normalizes path separators).
///
/// The path to convert.
/// The converted path.
public static string ToLocalOSPath(this string path)
{
return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
}
///
/// Validates options.
///
/// The options instance to validate.
/// The result of the validation, if invalid.
/// A value indicating whether the instance is valid.
public static bool TryValidate(this Options options, out CompositeValidationResult result)
{
result = null;
var results = new List();
if (!Validator.TryValidateObject(options, new ValidationContext(options), results, true))
{
result = new CompositeValidationResult("Invalid configuration", results);
return false;
}
return true;
}
}
}