* README.md: blanked: not clear enough

* FileSystemController.cs: Fixes the route to user's Files by an
  Admin.

* FileSystemManager.cs: * Fixes the dir separator usage
* Adds a method to validate a directory or file name

* YavscModel.csproj:
* Commande.cs: FileInfoCollection is now removed

* FileInfoCollection.cs:
* DirNotFoundException.cs: Removes useless code
This commit is contained in:
Paul Schneider 2015-08-14 01:43:55 +02:00
commit 3fa55acf2e
10 changed files with 86 additions and 202 deletions

View file

@ -1,3 +1,14 @@
2015-08-14 Paul Schneider <paul@pschneider.fr>
* FileSystemManager.cs: * Fixes the dir separator usage
* Adds a method to validate a directory or file name
* YavscModel.csproj:
* Commande.cs: FileInfoCollection is now removed
* FileInfoCollection.cs:
* DirNotFoundException.cs: Removes useless code
2015-08-05 Paul Schneider <paul@pschneider.fr>
* BlogEntryCollection.cs: adds xml doc

View file

@ -1,46 +0,0 @@
//
// DirNotFoundException.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
using System.IO;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Web.Security;
namespace Yavsc.Model.FileSystem
{
/// <summary>
/// Dir not found exception.
/// </summary>
public class DirNotFoundException : Exception {
/// <summary>
/// Initializes a new instance of the <see cref="Yavsc.Model.FileSystem.DirNotFoundException"/> class.
/// </summary>
/// <param name="dir">Dir.</param>
public DirNotFoundException(string dir)
: base(string.Format( "Directory not found : {0}", dir))
{
}
}
}

View file

@ -1,27 +0,0 @@
using System;
using System.IO;
using System.Collections.Generic;
namespace Yavsc.Model.FileSystem
{
/// <summary>
/// File info collection.
/// </summary>
public class FileInfoCollection: List<FileInfo>
{
/// <summary>
/// Gets or sets the owner.
/// </summary>
/// <value>The owner.</value>
public string Owner { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Yavsc.Model.FileSystem.FileInfoCollection"/> class.
/// </summary>
/// <param name="files">Files.</param>
public FileInfoCollection (FileInfo [] files)
{
AddRange (files);
}
}
}

View file

@ -56,18 +56,37 @@ namespace Yavsc.Model.FileSystem
/// <summary>
/// Initializes a new instance of the <see cref="Yavsc.Model.FileSystem.FileSystemManager"/> class.
/// </summary>
public FileSystemManager (string rootDirectory="~/users/{0}", char dirSep = '/')
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));
DirectorySeparator = dirSep;
}
/// <summary>
/// Initializes a new instance of the <see cref="Yavsc.Model.FileSystem.FileSystemManager"/> class.
/// </summary>
public FileSystemManager (string username, string rootDirectory="~/users/{0}")
{
Prefix = HttpContext.Current.Server.MapPath (
string.Format (rootDirectory, username));
}
string regexFileName = "^[A-Za-z0-9#^!+ _~\\-.]+$";
/// <summary>
/// Determines if the specified name is OK.
/// </summary>
/// <returns><c>true</c> if is this name O the specified name; otherwise, <c>false</c>.</returns>
/// <param name="name">Name.</param>
public static bool IsThisNameOK(string name)
{
foreach (char x in Path.GetInvalidPathChars()) {
if (name.Contains (x))
return false;
}
return true;
}
/// <summary>
/// Put the specified files in destDir, as sub dir of the current user's home dir.
/// </summary>
@ -86,7 +105,7 @@ namespace Yavsc.Model.FileSystem
}
// do the job
checkSubDir (destDir);
CheckSubDir (destDir);
DirectoryInfo di = new DirectoryInfo (
Path.Combine (Prefix, destDir));
if (!di.Exists)
@ -114,45 +133,50 @@ namespace Yavsc.Model.FileSystem
prefix = value;
}
}
/// <summary>
/// Gets or sets the directory name separator.
/// </summary>
/// <value>The separator.</value>
public char DirectorySeparator { get; set; }
/// <summary>
/// Checks the sub dir name.
/// Checks the sub dir name against model specifications,
/// concerning the allowed character class.
/// </summary>
/// <param name="subdir">Subdir.</param>
private void checkSubDir (string subdir)
private void CheckSubDir (string subdir)
{
foreach (string dirname in subdir.Split(DirectorySeparator))
foreach (string dirname in subdir.Split(Path.DirectorySeparatorChar)) {
if (!Regex.Match (dirname, regexFileName).Success)
throw new InvalidDirNameException (dirname);
foreach (char x in Path.GetInvalidPathChars())
if (subdir.Contains (x))
throw new InvalidDirNameException (subdir);
foreach (char x in dirname)
if (subdir.Contains (x))
throw new InvalidDirNameException (subdir);
}
}
/// <summary>
/// Gets the files.
/// Gets the files owned by the current logged user.
/// The web user must be authenticated,
/// The given username must be registered.
/// </summary>
/// <returns>The files.</returns>
/// <param name="subdir">Subdir.</param>
public FileInfoCollection GetFiles (string subdir)
public IEnumerable<FileInfo> GetFiles (string subdir)
{
string path = Prefix;
if (subdir != null) {
checkSubDir (subdir); // checks for specification validity
CheckSubDir (subdir); // checks for specification validity
path = Path.Combine (Prefix, subdir);
}
DirectoryInfo di = new DirectoryInfo (path);
FileInfoCollection res = new FileInfoCollection (di.GetFiles ());
// TODO define an Owner
return res;
return (di.GetFiles ());
}
public IEnumerable<FileInfo> 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 ());
}
/// <summary>
/// Files the info.
/// </summary>
@ -160,7 +184,7 @@ namespace Yavsc.Model.FileSystem
/// <param name="id">Identifier.</param>
public FileInfo FileInfo(string id)
{
checkSubDir (id);
CheckSubDir (id);
return new FileInfo(Path.Combine (Prefix, id));
}
}

View file

@ -5,6 +5,7 @@ using Yavsc.Model.WorkFlow;
using Yavsc.Model.FileSystem;
using System.Web;
using System.Collections.Generic;
using System.IO;
namespace Yavsc.Model.FrontOffice
@ -43,7 +44,7 @@ namespace Yavsc.Model.FrontOffice
/// </summary>
public Dictionary<string,string> Parameters = new Dictionary<string,string> ();
FileInfoCollection Files {
IEnumerable<FileInfo> Files {
get {
return GetFSM().GetFiles (Id.ToString());
}

View file

@ -46,7 +46,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Admin\RestoreQuery.cs" />
<Compile Include="Admin\DataAccess.cs" />
<Compile Include="FileSystem\FileInfoCollection.cs" />
<Compile Include="WorkFlow\Writting.cs" />
<Compile Include="WorkFlow\Estimate.cs" />
<Compile Include="WorkFlow\IContentProvider.cs" />
@ -113,7 +112,6 @@
<Compile Include="FrontOffice\Catalog\CatalogManager.cs" />
<Compile Include="FrontOffice\Catalog\CatalogProvider.cs" />
<Compile Include="FileSystem\FileSystemManager.cs" />
<Compile Include="FileSystem\DirNotFoundException.cs" />
<Compile Include="FileSystem\InvalidDirNameException.cs" />
<Compile Include="FileSystem\WebFileInfo.cs" />
<Compile Include="FrontOffice\CommandStatus.cs" />