This commit is contained in:
Paul Schneider 2026-02-14 16:54:27 +00:00
commit 45514010f2
3505 changed files with 154 additions and 523 deletions

View file

@ -1,92 +0,0 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using Yavsc.ViewModels.UserFiles;
namespace Yavsc.Server.Helpers
{
public static class AbstractFileSystemHelpers
{
public static string UserBillsDirName { set; get; }
public static string UserFilesDirName { set; get; }
/// <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)
{
if (string.IsNullOrEmpty(path)) return true;
// disallow full path specification
if (path[0]==Path.DirectorySeparatorChar) return false;
foreach (var name in path.Split(Path.DirectorySeparatorChar))
{
if (!IsValidDirectoryName(name) || name.Equals("..") || name.Equals("."))
return false;
}
// disallow trailling slash
if (path[path.Length-1]==RemoteDirectorySeparator) return false;
return true;
}
public static bool IsValidDirectoryName(this string name)
{
return !name.Any(c => !ValidFileNameChars.Contains(c));
}
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;
}
// 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)
{
if (ValidFileNameChars.Contains(c))
sb.Append(c);
else sb.Append("#" + ((int)c).ToString("D3"));
}
return sb.ToString();
}
public static UserDirectoryInfo GetUserFiles(string userId, string subdir)
{
UserDirectoryInfo di = new UserDirectoryInfo(UserFilesDirName, userId, subdir);
return di;
}
public static bool IsRegularFile(string userName, string subdir)
{
FileInfo fi = new FileInfo( Path.Combine(Path.Combine(UserFilesDirName, userName), subdir));
return fi.Exists;
}
// Server side only supports POSIX file systems
public const char RemoteDirectorySeparator = '/';
public static char[] AlfaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
// Only accept descent remote file names
public static char[] ValidFileNameChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_~. %#@".ToCharArray();
// Estimate signature file name format
public static Func<string, string, long, string>
SignFileNameFormat = new Func<string, string, long, string>((signType, billingCode, estimateId) => $"sign-{billingCode}-{signType}-{estimateId}.png");
}
}

View file

@ -1,9 +0,0 @@
using Yavsc.Abstract.FileSystem;
namespace Yavsc.ViewModels.UserFiles
{
public class DirectoryShortInfo: IDirectoryShortInfo {
public string Name { get; set; }
public bool IsEmpty { get; set; }
}
}

View file

@ -1,18 +0,0 @@
namespace Yavsc.Abstract.Helpers
{
public enum ErrorCode {
NotFound,
InternalError,
DestExists,
InvalidRequest
}
public class FsOperationInfo {
public bool Done { get; set; } = false;
public ErrorCode ErrorCode { get; set; }
public string? ErrorMessage { get; set; }
}
}

View file

@ -1,8 +0,0 @@
namespace Yavsc.Abstract.FileSystem {
public interface IDirectoryShortInfo
{
string Name { get; set; }
bool IsEmpty { get; set; }
}
}

View file

@ -1,14 +0,0 @@
namespace Yavsc.Abstract.FileSystem
{
public interface IFileReceivedInfo
{
string DestDir { get; set; }
string FileName { get; set; }
bool Overridden { get; set; }
bool QuotaOffense { get; set; }
}
}

View file

@ -1,17 +0,0 @@
using System.ComponentModel.DataAnnotations;
using Yavsc.Attributes.Validation;
namespace Yavsc.Models.FileSystem
{
public class MoveFileQuery
{
[ValidRemoteUserFilePath]
[StringLength(512)]
public required string Id { get; set; }
[StringLength(512)]
[ValidRemoteUserFilePath]
public required string To { get; set; }
}
}

View file

@ -1,17 +0,0 @@
using System;
namespace Yavsc.ViewModels
{
public class RemoteFileInfo
{
public string Name { get; set; }
public long Size { get; set; }
public DateTime CreationTime { get; set; }
public DateTime LastModified { get; set; }
}
}

View file

@ -1,54 +0,0 @@
using System;
using System.IO;
using System.Linq;
using Yavsc.Abstract.FileSystem;
using Yavsc.Server.Helpers;
namespace Yavsc.ViewModels.UserFiles
{
public class UserDirectoryInfo
{
public string UserName { get; set; }
public string SubPath { get; set; }
public RemoteFileInfo [] Files {
get; set;
}
public DirectoryShortInfo [] SubDirectories { 
get; set;
}
private readonly DirectoryInfo dInfo;
// for deserialization
public UserDirectoryInfo()
{
}
public UserDirectoryInfo(string userReposPath, string userId, string path)
{
if (string.IsNullOrWhiteSpace(userId))
throw new NotSupportedException("No user name, no user dir.");
UserName = userId;
var finalPath = path == null ? userId : Path.Combine(userId, path);
if (!finalPath.IsValidYavscPath())
throw new InvalidOperationException(
$"File name contains invalid chars ({finalPath})");
dInfo = new DirectoryInfo(
userReposPath+Path.DirectorySeparatorChar+finalPath);
if (dInfo.Exists) {
Files = dInfo.GetFiles().Select
( entry => new RemoteFileInfo { Name = entry.Name, Size = entry.Length,
CreationTime = entry.CreationTime, LastModified = entry.LastWriteTime }).ToArray();
SubDirectories = dInfo.GetDirectories().Select
( d=> new DirectoryShortInfo { Name= d.Name, IsEmpty=false } ).ToArray();
}
else {
// don't return null, but empty arrays
Files = new RemoteFileInfo[0];
SubDirectories = new DirectoryShortInfo[0];
}
}
}
}