yavsc/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs

53 lines
1.8 KiB
C#

8 years ago
using System;
using System.IO;
using System.Linq;
using Yavsc.Abstract.FileSystem;
8 years ago
namespace Yavsc.ViewModels.UserFiles
{
public class UserDirectoryInfo
{
public string UserName { get; set; }
public string SubPath { get; set; }
public RemoteFileInfo [] Files {
get; set;
8 years ago
}
public DirectoryShortInfo [] SubDirectories { 
get; set;
8 years ago
}
private DirectoryInfo dInfo;
// for deserialization
public UserDirectoryInfo()
{
}
public UserDirectoryInfo(string userReposPath, string username, string path)
8 years ago
{
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())
8 years ago
throw new InvalidOperationException(
$"File name contains invalid chars, using path {finalPath}");
8 years ago
dInfo = new DirectoryInfo(
userReposPath+FileSystemConstants.RemoteDirectorySeparator+finalPath);
if (!dInfo.Exists) dInfo.Create();
8 years ago
Files = dInfo.GetFiles().Select
( entry => new RemoteFileInfo { Name = entry.Name, Size = entry.Length,
8 years ago
CreationTime = entry.CreationTime, LastModified = entry.LastWriteTime }).ToArray();
SubDirectories = dInfo.GetDirectories().Select
( d=> new DirectoryShortInfo { Name= d.Name, IsEmpty=false } ).ToArray();
8 years ago
}
}
public class DirectoryShortInfo: IDirectoryShortInfo {
public string Name { get; set; }
public bool IsEmpty { get; set; }
}
}