[DONT MERGE] give a spec to input files
This commit is contained in:
30
Yavsc.Abstract/FileSystem/FileSystemHelpers.cs
Normal file
30
Yavsc.Abstract/FileSystem/FileSystemHelpers.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace Yavsc.Abstract.FileSystem
|
||||
{
|
||||
public static class FileSystemHelpers
|
||||
{
|
||||
public static bool IsValidYavscPath(this string path)
|
||||
{
|
||||
if (path == null) return true;
|
||||
foreach (var name in path.Split('/'))
|
||||
{
|
||||
if (!IsValidDirectoryName(name) || name.Equals("..") || name.Equals("."))
|
||||
return false;
|
||||
}
|
||||
if (path[path.Length]==FileSystemConstants.RemoteDirectorySeparator) return false;
|
||||
return true;
|
||||
}
|
||||
public static bool IsValidDirectoryName(this string name)
|
||||
{
|
||||
return !name.Any(c => !FileSystemConstants.ValidFileNameChars.Contains(c));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class FileSystemConstants
|
||||
{
|
||||
public const char RemoteDirectorySeparator = '/';
|
||||
public static char[] ValidFileNameChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_~. ".ToCharArray();
|
||||
}
|
||||
}
|
41
Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs
Normal file
41
Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Yavsc.Abstract.FileSystem;
|
||||
|
||||
namespace Yavsc.ViewModels.UserFiles
|
||||
{
|
||||
public class UserDirectoryInfo
|
||||
{
|
||||
public string UserName { get; private set; }
|
||||
public string SubPath { get; private set; }
|
||||
public RemoteFileInfo [] Files {
|
||||
get; private set;
|
||||
}
|
||||
public string [] SubDirectories {
|
||||
get; private set;
|
||||
}
|
||||
private DirectoryInfo dInfo;
|
||||
public UserDirectoryInfo(string userReposPath, string username, string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
throw new NotSupportedException("No user name, no user dir.");
|
||||
UserName = username;
|
||||
var finalPath = username;
|
||||
if (!string.IsNullOrWhiteSpace(path))
|
||||
finalPath += Path.DirectorySeparatorChar + path;
|
||||
if (!finalPath.IsValidYavscPath())
|
||||
throw new InvalidOperationException(
|
||||
$"File name contains invalid chars, using path {finalPath}");
|
||||
|
||||
dInfo = new DirectoryInfo(
|
||||
userReposPath+FileSystemConstants.RemoteDirectorySeparator+finalPath);
|
||||
if (!dInfo.Exists) dInfo.Create();
|
||||
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=> d.Name ).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
18
Yavsc.Abstract/FileSystem/UserFileInfo.cs
Normal file
18
Yavsc.Abstract/FileSystem/UserFileInfo.cs
Normal file
@ -0,0 +1,18 @@
|
||||
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; }
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user