enforced file system

This commit is contained in:
Paul Schneider 2018-01-26 13:51:09 +01:00
commit 6066497a67
26 changed files with 143 additions and 76 deletions

View file

@ -10,8 +10,11 @@ using Yavsc.Models;
namespace Yavsc.ApiControllers
{
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Yavsc.Abstract.FileSystem;
using Yavsc.Exceptions;
using Yavsc.Models.FileSystem;
public class FSQuotaException : Exception {
}
@ -21,12 +24,16 @@ namespace Yavsc.ApiControllers
{
ApplicationDbContext dbContext;
private IAuthorizationService AuthorizationService;
private ILogger logger;
public FileSystemApiController(ApplicationDbContext context,
IAuthorizationService authorizationService)
IAuthorizationService authorizationService,
ILoggerFactory loggerFactory)
{
AuthorizationService = authorizationService;
dbContext = context;
logger = loggerFactory.CreateLogger<FileSystemApiController>();
}
[HttpGet()]
@ -46,11 +53,11 @@ namespace Yavsc.ApiControllers
}
[HttpPost]
public IEnumerable<IActionResult> Post(string subdir="", string names = null)
public IActionResult Post(string subdir="", string names = null)
{
string root = null;
string [] destinationFileNames = names?.Split('/');
List<FileRecievedInfo> received = new List<FileRecievedInfo>();
InvalidPathException pathex = null;
try {
root = User.InitPostToFileSystem(subdir);
@ -58,20 +65,26 @@ namespace Yavsc.ApiControllers
pathex = ex;
}
if (pathex!=null)
yield return new BadRequestObjectResult(pathex);
return new BadRequestObjectResult(pathex);
var uid = User.GetUserId();
var user = dbContext.Users.Single(
u => u.Id == User.GetUserId()
u => u.Id == uid
);
int i=0;
logger.LogInformation($"Recieving {Request.Form.Files.Count} files.");
foreach (var f in Request.Form.Files)
{
var destFileName = destinationFileNames?.Length >i ? destinationFileNames[i] : null;
var item = user.ReceiveUserFile(root, f, destFileName);
dbContext.SaveChanges(User.GetUserId());
yield return Ok(item);
received.Add(item);
logger.LogInformation($"Recieved '{item.FileName}'.");
if (item.QuotaOffensed)
break;
i++;
};
return Ok(received);
}
[HttpDelete]

View file

@ -4,10 +4,8 @@ using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Security.Claims;
using System.Text;
using System.Web;
using Microsoft.AspNet.Http;
using Yavsc.Abstract.FileSystem;
@ -31,22 +29,7 @@ namespace Yavsc.Helpers
return di;
}
// Ensure this path is canonical,
// No "dirto/./this", neither "dirt/to/that/"
// no .. and each char must be listed as valid in constants
public static string FilterFileName(string fileName)
{
if (fileName==null) return null;
StringBuilder sb = new StringBuilder();
foreach (var c in fileName)
{
if (FileSystemConstants.ValidFileNameChars.Contains(c))
sb.Append(c);
else sb.Append('_');
}
return sb.ToString();
}
public static string InitPostToFileSystem(
this ClaimsPrincipal user,
string subpath)
@ -81,10 +64,14 @@ namespace Yavsc.Helpers
var item = new FileRecievedInfo();
// form-data; name="file"; filename="capt0008.jpg"
ContentDisposition contentDisposition = new ContentDisposition(f.ContentDisposition);
item.FileName = FilterFileName (destFileName ?? contentDisposition.FileName);
item.FileName = Yavsc.Abstract.FileSystem.FileSystemHelpers.FilterFileName (destFileName ?? contentDisposition.FileName);
item.MimeType = contentDisposition.DispositionType;
var fi = new FileInfo(Path.Combine(root, item.FileName));
if (fi.Exists) item.Overriden = true;
if (fi.Exists)
{
item.Overriden = true;
usage -= fi.Length;
}
using (var dest = fi.OpenWrite())
{
using (var org = f.OpenReadStream())
@ -92,7 +79,7 @@ namespace Yavsc.Helpers
byte[] buffer = new byte[1024];
long len = org.Length;
if (len > (user.DiskQuota - usage)) {
item.QuotaOffensed = true;
return item;
}
usage += len;

View file

@ -21,13 +21,23 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using Yavsc.Abstract.FileSystem;
namespace Yavsc.Models.FileSystem
{
public class FileRecievedInfo
public class FileRecievedInfo : IFileRecievedInfo
{
public FileRecievedInfo()
{
public string MimeType { get; set; }
public string DestDir { get; set; }
public string FileName { get; set; }
public bool Overriden { get; set; }
QuotaOffensed = Overriden = false;
MimeType = DestDir = FileName = null;
}
public string MimeType { get; set; }
public string DestDir { get; set; }
public string FileName { get; set; }
public bool Overriden { get; set; }
public bool QuotaOffensed { get; set; }
}
}

View file

@ -28,7 +28,8 @@ namespace Yavsc
{
FileProvider = new PhysicalFileProvider(UserFilesDirName),
RequestPath = new PathString(Constants.UserFilesPath),
EnableDirectoryBrowsing = env.IsDevelopment()
EnableDirectoryBrowsing = env.IsDevelopment(),
};
UserFilesOptions.EnableDefaultFiles=true;

View file

@ -67,21 +67,17 @@
<div class="row @trclass">
<div class="col-xs-10">
<a asp-action="Title" asp-route-id="@title" >
<markdown>@first.Title</markdown>
<h3 markdown="@first.Title"></h3>
@if (first.Photo==null) { }
else {<img src="@first.Photo" class="blogphoto" alt="@first.Title">}
</a>
<markdown>@((first.Content?.Length > 120) ? first.Content.Substring(0, 120) + " ..." : first.Content)</markdown>
<span style="font-size:x-small;">(@first.Author.UserName </span>,
<span style="font-size:xx-small;">
posté le @first.DateCreated.ToString("dddd d MMM yyyy à H:mm")
<div class="smalltext" markdown="@((first.Content?.Length > 256) ? first.Content.Substring(0, 256) + "..." : first.Content)"></div>
<div class="smalltext"> @first.Author.UserName ,
(posté le @first.DateCreated.ToString("dddd d MMM yyyy à H:mm")
@if ((first.DateModified - first.DateCreated).Minutes > 0){ 
@:- Modifié le @first.DateModified.ToString("dddd d MMM yyyy à H:mm",System.Globalization.CultureInfo.CurrentUICulture)
})
</span>
})</div>
</div>
<div class="col-xs-2">
<ul class="actiongroup">

View file

@ -30,7 +30,7 @@
<img src="@item.Photo" class="blogphoto"></a>
</td>
<td>
<markdown>@((item.Content?.Length > 120) ? item.Content.Substring(0, 122) + " ..." : item.Content)</markdown>
<markdown>@((item.Content?.Length > 256) ? item.Content.Substring(0, 256) + " ..." : item.Content)</markdown>
<span style="font-size:x-small;">(@item.Author.UserName </span>,
<span style="font-size:xx-small;">
posté le @item.DateCreated.ToString("dddd d MMM yyyy à H:mm")

View file

@ -142,7 +142,7 @@
@if (Model.DiskQuota>0)
{
<text>
@((Model.DiskUsage/Model.DiskQuota).ToString("%#0")) :
@(((double)Model.DiskUsage/Model.DiskQuota).ToString("%#0")) :
</text>
}
<text>

View file

@ -3,8 +3,8 @@
<div class="dirinfo">
<strong>@Model.UserName / @Model.SubPath</strong>
<div class="subdirs">
@foreach (string subdir in Model.SubDirectories) {
<a href="@subdir" class="subdir">@subdir</a>
@foreach (var subdir in Model.SubDirectories) {
<a href="@subdir" class="subdir">@subdir.Name</a>
}
</div>
<style>

View file

@ -114,7 +114,7 @@
"System.Json": "4.0.20126.16343",
"Yavsc.Abstract": {
"type": "build",
"version": "1.0.2"
"version": "1.0.5"
},
"Extensions.AspNet.Authentication.Instagram": "1.0.0-t150809211713",
"Microsoft.AspNet.Http.Extensions": "1.0.0-rc1-final",
@ -166,4 +166,4 @@
"postpublish": "echo after publish"
},
"embed": "Views/**/*.cshtml"
}
}

View file

@ -2823,7 +2823,7 @@
"lib/WebGrease.dll": {}
}
},
"Yavsc.Abstract/1.0.2": {
"Yavsc.Abstract/1.0.5": {
"type": "project",
"framework": ".NETFramework,Version=v4.5.1",
"dependencies": {
@ -5656,7 +5656,7 @@
"lib/WebGrease.dll": {}
}
},
"Yavsc.Abstract/1.0.2": {
"Yavsc.Abstract/1.0.5": {
"type": "project",
"framework": ".NETFramework,Version=v4.5.1",
"dependencies": {
@ -8489,7 +8489,7 @@
"lib/WebGrease.dll": {}
}
},
"Yavsc.Abstract/1.0.2": {
"Yavsc.Abstract/1.0.5": {
"type": "project",
"framework": ".NETFramework,Version=v4.5.1",
"dependencies": {
@ -8503,7 +8503,7 @@
}
},
"libraries": {
"Yavsc.Abstract/1.0.2": {
"Yavsc.Abstract/1.0.5": {
"type": "project",
"path": "../Yavsc.Abstract/project.json"
},
@ -11437,7 +11437,7 @@
"Microsoft.AspNet.Mvc.Formatters.Json >= 6.0.0-rc1-final",
"Microsoft.AspNet.OWin >= 1.0.0-rc1-final",
"System.Json >= 4.0.20126.16343",
"Yavsc.Abstract >= 1.0.2",
"Yavsc.Abstract >= 1.0.5",
"Extensions.AspNet.Authentication.Instagram >= 1.0.0-t150809211713",
"Microsoft.AspNet.Http.Extensions >= 1.0.0-rc1-final",
"Microsoft.DiaSymReader.Native >= 1.5.0",

View file

@ -390,6 +390,10 @@ p.small {
font-size: 16px;
}
.smalltext {
font-size: 10px;
}
h1,
h2,
h3,