refactoring fs
This commit is contained in:
@ -1,24 +1,36 @@
|
||||
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Yavsc.Models.Billing;
|
||||
using Yavsc.ViewModels.UserFiles;
|
||||
|
||||
namespace Yavsc.Helpers
|
||||
{
|
||||
public static class FileSystemHelpers {
|
||||
public static IDirectoryContents GetFileContent(this Estimate estimate, string userFileDir)
|
||||
{
|
||||
if (estimate?.Query?.PerformerProfile?.Performer == null)
|
||||
return null;
|
||||
var di = new DirectoryInfo(Path.Combine(
|
||||
userFileDir,
|
||||
estimate.Query.PerformerProfile.Performer.UserName
|
||||
));
|
||||
if (!di.Exists) return null;
|
||||
|
||||
var fsp = new PhysicalFileProvider(di.FullName);
|
||||
return fsp.GetDirectoryContents(
|
||||
Path.Combine(Constants.UserBillsFilesDir, estimate.Id.ToString())
|
||||
);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user