// // UserFileSystemManager.cs // // Author: // Paul Schneider // // Copyright (c) 2015 GNU GPL // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . using System; using System.IO; using System.Web; using System.Web.Security; using System.Collections.Generic; using System.Collections.Specialized; namespace Yavsc.Model.FileSystem { /// /// User file system. /// internal class UserFileSystem : WebFileSystemManager { /// /// Initializes a new instance of the class. /// /// Username. /// Root. public UserFileSystem(string username, string root="~/users") { base.Prefix = UserFileSystemManager.UserFileRoot(username,root); } } /// /// User file system manager. /// public static class UserFileSystemManager { /// /// Initializes a new instance of the class. /// /// Root directory. public static string CurrentUserFileRoot (string rootDirectory="~/users") { if (!HttpContext.Current.User.Identity.IsAuthenticated) throw new Exception ("Not membership available"); return UserFileRoot (HttpContext.Current.User.Identity.Name,rootDirectory); } /// /// Users the file root. /// /// The file root. /// Username. /// Root directory. public static string UserFileRoot (string username, string rootDirectory="~/users") { string rootpath = HttpContext.Current.Server.MapPath (rootDirectory); return Path.Combine(rootpath, username); } private static WebFileSystemManager manager=null; /// /// Gets or sets the file manager. /// /// The file manager. public static WebFileSystemManager FileManager { get { if (manager == null) manager = new UserFileSystem ( HttpContext.Current.User.Identity.Name); return manager; } set { manager = value; } } /// /// Gets the files. /// /// The files. /// Subdir. public static IEnumerable GetFiles (string subdir) { return GetFiles (HttpContext.Current.User.Identity.Name, subdir); } /// /// Gets the files. /// /// The files. /// Username. /// Subdir. public static IEnumerable GetFiles (string username, string subdir) { return FileManager.GetFiles (Path.Combine(UserFileRoot(username),subdir)); } /// /// Put the specified destDir and files. /// /// Destination dir. /// Files. public static void Put(string destDir, NameObjectCollectionBase files) { FileManager.ValidateSubDir (destDir); FileManager.Put( Path.Combine(CurrentUserFileRoot(),destDir), files); } /// /// Detail the specified filePath and username. /// /// File path. /// Username. public static FileInfo Detail(string filePath, string username=null) { return FileManager.FileInfo (UserFileRoot (username)); } } }