diff --git a/Yavsc/ApiControllers/FileSystemApiController.cs b/Yavsc/ApiControllers/FileSystemApiController.cs index bb5a274f..fa54c75d 100644 --- a/Yavsc/ApiControllers/FileSystemApiController.cs +++ b/Yavsc/ApiControllers/FileSystemApiController.cs @@ -1,13 +1,12 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Net.Mime; using System.Security.Claims; using Microsoft.AspNet.Authorization; using Microsoft.AspNet.Mvc; using Yavsc.Helpers; using Yavsc.Models; +using Yavsc.Models.FileSystem; namespace Yavsc.ApiControllers { @@ -45,20 +44,11 @@ namespace Yavsc.ApiControllers return Ok(files); } - public class FileRecievedInfo - { - public string DestDir { get; set; } - public string FileName { get; set; } - public bool Overriden { get; set; } - } + [HttpPost] public IEnumerable Post(string subdir="") { - var root = Path.Combine(Startup.UserFilesDirName, User.Identity.Name); - // TOSO secure this path - // if (subdir!=null) root = Path.Combine(root, subdir); - var diRoot = new DirectoryInfo(root); - if (!diRoot.Exists) diRoot.Create(); + var root = User.InitPostToFileSystem(subdir); var user = dbContext.Users.Single( u => u.Id == User.GetUserId() @@ -68,31 +58,8 @@ namespace Yavsc.ApiControllers foreach (var f in Request.Form.Files) { - var item = new FileRecievedInfo(); - // form-data; name="file"; filename="capt0008.jpg" - ContentDisposition contentDisposition = new ContentDisposition(f.ContentDisposition); - item.FileName = contentDisposition.FileName; - var fi = new FileInfo(Path.Combine(root, item.FileName)); - if (fi.Exists) item.Overriden = true; - using (var dest = fi.OpenWrite()) - { - using (var org = f.OpenReadStream()) - { - byte[] buffer = new byte[1024]; - long len = org.Length; - user.DiskUsage += len; - if (len> (quota-usage)) throw new FSQuotaException(); - - while (len>0) { - int blen = len>1024?1024:(int)len; - org.Read(buffer, 0, blen); - dest.Write(buffer,0,blen); - len-=blen; - } - dest.Close(); - org.Close(); - } - } + var item = user.ReceiveUserFile(root,quota,ref usage,f); + user.DiskUsage = usage; dbContext.SaveChanges(); yield return item; }; diff --git a/Yavsc/Helpers/FileSystemHelpers.cs b/Yavsc/Helpers/FileSystemHelpers.cs index a4a6d745..7d1aef6a 100644 --- a/Yavsc/Helpers/FileSystemHelpers.cs +++ b/Yavsc/Helpers/FileSystemHelpers.cs @@ -1,36 +1,94 @@ using System.IO; using System.Linq; +using System.Net.Mime; using System.Security.Claims; +using Microsoft.AspNet.Http; +using Yavsc.ApiControllers; +using Yavsc.Models; +using Yavsc.Models.FileSystem; using Yavsc.ViewModels.UserFiles; namespace Yavsc.Helpers { - public static class FileSystemHelpers { + public static class FileSystemHelpers + { + + public static UserDirectoryInfo GetUserFiles(this ClaimsPrincipal user, string subdir) + { + + UserDirectoryInfo di = new UserDirectoryInfo(user.Identity.Name, subdir); - public static UserDirectoryInfo GetUserFiles(this ClaimsPrincipal user,string subdir) { - - UserDirectoryInfo di = new UserDirectoryInfo(user.Identity.Name,subdir); - return di; } - static char [] ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_~.".ToCharArray(); + static char[] ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_~.".ToCharArray(); public static bool IsValidDirectoryName(this string name) - {  - return !name.Any(c=> !ValidChars.Contains(c)); + { + return !name.Any(c => !ValidChars.Contains(c)); } public static bool IsValidPath(this string path) - {  - if (path==null) return true; + { + if (path == null) return true; foreach (var name in path.Split(Path.DirectorySeparatorChar)) - { - if (name!=null) + { + if (name != null) if (!IsValidDirectoryName(name) - || name.Equals("..")) return false; - } + || name.Equals("..")) + return false; + } return true; } + public static string InitPostToFileSystem( + this ClaimsPrincipal user, + string subpath) + { + var root = Path.Combine(Startup.UserFilesDirName, user.Identity.Name); + // TOSO secure this path + // if (subdir!=null) root = Path.Combine(root, subdir); + var diRoot = new DirectoryInfo(root); + if (!diRoot.Exists) diRoot.Create(); + if (subpath != null) + if (subpath.IsValidPath()) + { + root = Path.Combine(root, subpath); + diRoot = new DirectoryInfo(root); + if (!diRoot.Exists) diRoot.Create(); + } + + return root; + } + + public static FileRecievedInfo ReceiveUserFile(this ApplicationUser user, string root, long quota, ref long usage, IFormFile f) + { + var item = new FileRecievedInfo(); + // form-data; name="file"; filename="capt0008.jpg" + ContentDisposition contentDisposition = new ContentDisposition(f.ContentDisposition); + item.FileName = contentDisposition.FileName; + var fi = new FileInfo(Path.Combine(root, item.FileName)); + if (fi.Exists) item.Overriden = true; + using (var dest = fi.OpenWrite()) + { + using (var org = f.OpenReadStream()) + { + byte[] buffer = new byte[1024]; + long len = org.Length; + user.DiskUsage += len; + if (len > (quota - usage)) throw new FSQuotaException(); + + while (len > 0) + { + int blen = len > 1024 ? 1024 : (int)len; + org.Read(buffer, 0, blen); + dest.Write(buffer, 0, blen); + len -= blen; + } + dest.Close(); + org.Close(); + } + } + return item; + } } } diff --git a/Yavsc/Migrations/20161130084909_diskQuota.Designer.cs b/Yavsc/Migrations/20161130084909_diskQuota.Designer.cs index 30899709..97f5de91 100644 --- a/Yavsc/Migrations/20161130084909_diskQuota.Designer.cs +++ b/Yavsc/Migrations/20161130084909_diskQuota.Designer.cs @@ -1,7 +1,6 @@ using System; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Infrastructure; -using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Migrations; using Yavsc.Models; diff --git a/Yavsc/Migrations/20161130084909_diskQuota.cs b/Yavsc/Migrations/20161130084909_diskQuota.cs index 8756642d..957fde7b 100644 --- a/Yavsc/Migrations/20161130084909_diskQuota.cs +++ b/Yavsc/Migrations/20161130084909_diskQuota.cs @@ -1,5 +1,3 @@ -using System; -using System.Collections.Generic; using Microsoft.Data.Entity.Migrations; namespace Yavsc.Migrations diff --git a/Yavsc/Models/FileSystem/FileRecievedInfo.cs b/Yavsc/Models/FileSystem/FileRecievedInfo.cs new file mode 100644 index 00000000..5ff429df --- /dev/null +++ b/Yavsc/Models/FileSystem/FileRecievedInfo.cs @@ -0,0 +1,9 @@ +namespace Yavsc.Models.FileSystem +{ + public class FileRecievedInfo + { + public string DestDir { get; set; } + public string FileName { get; set; } + public bool Overriden { get; set; } + } +} \ No newline at end of file