// // FileSystemManager.cs // // Author: // Paul Schneider // // Copyright (c) 2015 Paul Schneider // // 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.Text.RegularExpressions; using System.Text; using System.Web.Security; using System.Linq; using System.Collections.Generic; using System.Collections.Specialized; namespace Yavsc.Model.FileSystem { /// /// File system manager. /// It performs the FileSystem controllers logic. /// It will not be a true file system, /// It just provides simple method for a small set of /// files, in a small tree of sub-folders . /// public class FileSystemManager { /// /// Gets or sets the size of the max file. /// /// The size of the max file. public long MaxFileSize { get; set; } /// /// Gets or sets the max user storage. /// /// The max user storage. public long MaxUserStorage { get; set; } /// /// Initializes a new instance of the class. /// public FileSystemManager (string rootDirectory="~/users/{0}") { MembershipUser user = Membership.GetUser (); if (user == null) throw new Exception ("Not membership available"); Prefix = HttpContext.Current.Server.MapPath ( string.Format (rootDirectory, user.UserName)); } /// /// Initializes a new instance of the class. /// public FileSystemManager (string username, string rootDirectory="~/users/{0}") { Prefix = HttpContext.Current.Server.MapPath ( string.Format (rootDirectory, username)); } string regexFileName = "^[A-Za-z0-9#^!+ _~\\-.]+$"; /// /// Determines if the specified name is OK. /// /// true if is this name O the specified name; otherwise, false. /// Name. public static bool IsThisNameOK(string name) { foreach (char x in Path.GetInvalidPathChars()) { if (name.Contains (x)) return false; } return true; } /// /// Put the specified files in destDir, as sub dir of the current user's home dir. /// /// Destination dir, use "." to point to the user's home dir. /// Files. public void Put (string destDir, NameObjectCollectionBase files) { // sanity check on file names foreach (object obj in files) { HttpPostedFileBase file = obj as HttpPostedFileBase; if (!Regex.Match (file.FileName, regexFileName).Success) { throw new InvalidOperationException (string.Format ( "The file name {0} dosn't match an acceptable file name ({1})", file.FileName, regexFileName)); } } // do the job CheckSubDir (destDir); DirectoryInfo di = new DirectoryInfo ( Path.Combine (Prefix, destDir)); if (!di.Exists) di.Create (); foreach (object obj in files) { HttpPostedFileBase file = obj as HttpPostedFileBase; // TODO Limit with hfc[h].ContentLength string filename = Path.Combine (di.FullName, file.FileName); file.SaveAs (filename); } } private string prefix = null; /// /// Gets the users dir. /// /// The users dir. public string Prefix { get { return prefix; } set { prefix = value; } } /// /// Checks the sub dir name against model specifications, /// concerning the allowed character class. /// /// Subdir. private void CheckSubDir (string subdir) { foreach (string dirname in subdir.Split(Path.DirectorySeparatorChar)) { if (!Regex.Match (dirname, regexFileName).Success) throw new InvalidDirNameException (dirname); foreach (char x in dirname) if (subdir.Contains (x)) throw new InvalidDirNameException (subdir); } } /// /// Gets the files owned by the current logged user. /// The web user must be authenticated, /// The given username must be registered. /// /// The files. /// Subdir. public IEnumerable GetFiles (string subdir) { string path = Prefix; if (subdir != null) { CheckSubDir (subdir); // checks for specification validity path = Path.Combine (Prefix, subdir); } DirectoryInfo di = new DirectoryInfo (path); return (di.GetFiles ()); } public IEnumerable GetFiles (string username, string subdir) { string path = Prefix; if (subdir != null) { CheckSubDir (subdir); // checks for specification validity path = Path.Combine (Prefix, subdir); } DirectoryInfo di = new DirectoryInfo (path); return (di.GetFiles ()); } /// /// Files the info. /// /// The info. /// Identifier. public FileInfo FileInfo(string id) { CheckSubDir (id); return new FileInfo(Path.Combine (Prefix, id)); } } }