yavsc/src/Yavsc.Abstract/FileSystem/AbstractFileSystemHelpers.cs

92 lines
3.4 KiB
C#
Raw Normal View History

2020-06-07 17:46:08 +01:00
using System;
using System.IO;
using System.Linq;
2018-01-26 13:51:09 +01:00
using System.Text;
2018-03-26 19:27:29 +02:00
using Yavsc.ViewModels.UserFiles;
2019-08-21 17:23:58 +01:00
namespace Yavsc.Helpers
{
2018-03-26 19:27:29 +02:00
public static class AbstractFileSystemHelpers
{
2018-03-26 19:27:29 +02:00
public static string UserBillsDirName { set; get; }
public static string UserFilesDirName { set; get; }
2020-09-12 01:11:30 +01:00
2019-09-04 01:25:36 +01:00
/// <summary>
/// Is Valid this Path?
/// Return true when given value is a valid user file sub-path,
/// regarding chars used to specify it.
/// </summary>
/// <param name="path">Path to validate</param>
/// <returns></returns>
public static bool IsValidYavscPath(this string path)
{
2018-01-26 13:51:09 +01:00
if (string.IsNullOrEmpty(path)) return true;
2019-09-04 01:25:36 +01:00
// disallow full path specification
if (path[0]==Path.DirectorySeparatorChar) return false;
2018-07-30 11:20:41 +02:00
foreach (var name in path.Split(Path.DirectorySeparatorChar))
{
if (!IsValidDirectoryName(name) || name.Equals("..") || name.Equals("."))
return false;
}
2019-09-04 01:25:36 +01:00
// disallow trailling slash
2019-08-21 17:23:58 +01:00
if (path[path.Length-1]==RemoteDirectorySeparator) return false;
return true;
}
2019-08-23 22:19:32 +01:00
public static bool IsValidDirectoryName(this string name)
{
2019-08-21 17:23:58 +01:00
return !name.Any(c => !ValidFileNameChars.Contains(c));
}
2020-09-12 01:11:30 +01:00
2020-10-18 23:00:17 +01:00
public static bool IsValidShortFileName(this string name)
{
if (name.Any(c => !ValidFileNameChars.Contains(c)))
return false;
if (!name.Any(c => !AlfaNum.Contains(c)))
return false;
return true;
}
2018-01-26 13:51:09 +01:00
// Ensure this path is canonical,
// No "dirto/./this", neither "dirt/to/that/"
// no .. and each char must be listed as valid in constants
public static string FilterFileName(string fileName)
{
if (fileName==null) return null;
StringBuilder sb = new StringBuilder();
foreach (var c in fileName)
{
2019-08-21 17:23:58 +01:00
if (ValidFileNameChars.Contains(c))
2018-01-26 13:51:09 +01:00
sb.Append(c);
2020-09-12 01:11:30 +01:00
else sb.Append("#" + ((int)c).ToString("D3"));
2018-01-26 13:51:09 +01:00
}
2020-09-12 01:11:30 +01:00
return sb.ToString();
2018-01-26 13:51:09 +01:00
}
2020-09-12 01:11:30 +01:00
2019-08-21 17:23:58 +01:00
public static UserDirectoryInfo GetUserFiles(string userName, string subdir)
2018-03-26 19:27:29 +02:00
{
2019-08-21 17:23:58 +01:00
UserDirectoryInfo di = new UserDirectoryInfo(UserFilesDirName, userName, subdir);
2018-03-26 19:27:29 +02:00
return di;
}
2019-09-08 05:17:21 +01:00
public static bool IsRegularFile(string userName, string subdir)
{
FileInfo fi = new FileInfo( Path.Combine(Path.Combine(UserFilesDirName, userName), subdir));
return fi.Exists;
}
2019-08-23 22:19:32 +01:00
// Server side only supports POSIX file systems
public const char RemoteDirectorySeparator = '/';
2019-08-23 22:19:32 +01:00
2020-10-18 23:00:17 +01:00
public static char[] AlfaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
2019-08-23 22:19:32 +01:00
// Only accept descent remote file names
2020-06-07 17:46:08 +01:00
public static char[] ValidFileNameChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_~. %#".ToCharArray();
2020-09-12 01:11:30 +01:00
2019-08-23 22:19:32 +01:00
// Estimate signature file name format
2020-09-12 01:11:30 +01:00
public static Func<string, string, long, string>
SignFileNameFormat = new Func<string, string, long, string>((signType, billingCode, estimateId) => $"sign-{billingCode}-{signType}-{estimateId}.png");
}
2019-08-23 22:19:32 +01:00
}