yavsc/Yavsc/Helpers/FileSystemHelpers.cs

37 lines
1.1 KiB
C#

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

using System.IO;
using System.Linq;
using System.Security.Claims;
using Yavsc.ViewModels.UserFiles;
namespace Yavsc.Helpers
{
public static class FileSystemHelpers {
public static UserDirectoryInfo GetUserFiles(this ClaimsPrincipal user,string subdir) {
UserDirectoryInfo di = new UserDirectoryInfo(user.Identity.Name,subdir);
return di;
}
static char [] ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_~.".ToCharArray();
public static bool IsValidDirectoryName(this string name)
{ 
return !name.Any(c=> !ValidChars.Contains(c));
}
public static bool IsValidPath(this string path)
{ 
if (path==null) return true;
foreach (var name in path.Split(Path.DirectorySeparatorChar))
{
if (name!=null)
if (!IsValidDirectoryName(name)
|| name.Equals("..")) return false;
}
return true;
}
}
}