diff --git a/Yavsc/ApiControllers/AccountController.cs b/Yavsc/ApiControllers/AccountController.cs index 3627ef22..cf28fd5c 100644 --- a/Yavsc/ApiControllers/AccountController.cs +++ b/Yavsc/ApiControllers/AccountController.cs @@ -138,40 +138,34 @@ namespace Yavsc.WebApi.Controllers return Ok(user); } + /// + /// Actually only updates the user's name. + /// + /// MyUpdate containing the new user name + /// Ok when all is ok. [HttpPut("~/api/me")] public async Task UpdateMe(MyUpdate me) { - var ko = new BadRequestObjectResult( - new { error = "Specify some valid update request." }); - if (me==null) return ko; - if (me.Avatar==null && me.UserName == null) return ko; + if (!ModelState.IsValid) return new BadRequestObjectResult( + new { error = "Specify some valid user update request." }); var user = await _userManager.FindByIdAsync(User.GetUserId()); - - if (me.UserName !=null) { - var result = await _userManager.SetUserNameAsync(user, me.UserName); - } - if (me.Avatar!=null) { - user.Avatar = me.Avatar; - var result = await _userManager.UpdateAsync(user); - if (!result.Succeeded) - { - AddErrors("Avatar", result); - return new BadRequestObjectResult(ModelState); - } - } - return Ok(); + var result = await _userManager.SetUserNameAsync(user, me.UserName); + if (result.Succeeded) + return Ok(); + else return new BadRequestObjectResult(result); } - + /// + /// Updates the avatar + /// + /// [HttpPost("~/api/setavatar")] public async Task SetAvatar() { var root = User.InitPostToFileSystem(null); var user = await _userManager.FindByIdAsync(User.GetUserId()); - long usage = user.DiskUsage; if (Request.Form.Files.Count!=1) return new BadRequestResult(); - var info = user.ReceiveUserFile(root, user.DiskQuota, ref usage, Request.Form.Files[0]); - user.DiskUsage = usage; + var info = user.ReceiveAvatar(Request.Form.Files[0]); await _userManager.UpdateAsync(user); return Ok(info); } diff --git a/Yavsc/ApiControllers/FileSystemApiController.cs b/Yavsc/ApiControllers/FileSystemApiController.cs index fa54c75d..b2231447 100644 --- a/Yavsc/ApiControllers/FileSystemApiController.cs +++ b/Yavsc/ApiControllers/FileSystemApiController.cs @@ -53,13 +53,10 @@ namespace Yavsc.ApiControllers var user = dbContext.Users.Single( u => u.Id == User.GetUserId() ); - var quota = user.DiskQuota; - var usage = user.DiskUsage; foreach (var f in Request.Form.Files) { - var item = user.ReceiveUserFile(root,quota,ref usage,f); - user.DiskUsage = usage; + var item = user.ReceiveUserFile(root, f); dbContext.SaveChanges(); yield return item; }; diff --git a/Yavsc/Avatars/Paul.png b/Yavsc/Avatars/Paul.png new file mode 100644 index 00000000..cff89237 Binary files /dev/null and b/Yavsc/Avatars/Paul.png differ diff --git a/Yavsc/Avatars/Paul.s.png b/Yavsc/Avatars/Paul.s.png new file mode 100644 index 00000000..89b956bf Binary files /dev/null and b/Yavsc/Avatars/Paul.s.png differ diff --git a/Yavsc/Avatars/Paul.xs.png b/Yavsc/Avatars/Paul.xs.png new file mode 100644 index 00000000..34b28314 Binary files /dev/null and b/Yavsc/Avatars/Paul.xs.png differ diff --git a/Yavsc/Constants.cs b/Yavsc/Constants.cs index 5d741499..3fd2c095 100644 --- a/Yavsc/Constants.cs +++ b/Yavsc/Constants.cs @@ -23,10 +23,10 @@ namespace Yavsc StarHunterGroupName = "StarHunter", BlogModeratorGroupName = "Moderator", FrontOfficeGroupName = "FrontOffice", - UserBillsFilesDir= "Bills", - UserFilesDir = "UserFiles", GCMNotificationUrl = "https://gcm-http.googleapis.com/gcm/send", - KeyProtectorPurpose = "OAuth.AspNet.AuthServer"; + KeyProtectorPurpose = "OAuth.AspNet.AuthServer", + UserFilesPath = "/UserFiles", + AvatarsPath = "/Avatars" ; public static readonly Scope[] SiteScopes = {  new Scope { Id = "profile", Description = "Your profile informations" },   new Scope { Id = "book" , Description ="Your booking interface"},   diff --git a/Yavsc/Controllers/AccountController.cs b/Yavsc/Controllers/AccountController.cs index d72b22d1..4bd786d9 100644 --- a/Yavsc/Controllers/AccountController.cs +++ b/Yavsc/Controllers/AccountController.cs @@ -183,6 +183,7 @@ namespace Yavsc.Controllers var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { + user.DiskQuota = Startup.SiteSetup.UserFiles.Quota; // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); diff --git a/Yavsc/Controllers/EstimateController.cs b/Yavsc/Controllers/EstimateController.cs index 919a2db5..e9aeaefa 100644 --- a/Yavsc/Controllers/EstimateController.cs +++ b/Yavsc/Controllers/EstimateController.cs @@ -1,9 +1,9 @@ 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.FileProviders; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using Microsoft.Data.Entity; @@ -62,9 +62,6 @@ namespace Yavsc.Controllers { return HttpNotFound(); } - DirectoryInfo di = new DirectoryInfo(_site.UserFiles.DirName); - - return View(estimate); } @@ -106,21 +103,26 @@ namespace Yavsc.Controllers cmd => cmd.Id == estimate.CommandId ); - var userdir = Path.Combine( - _site.UserFiles.DirName, + var billsdir = Path.Combine( + _site.UserFiles.Bills, perfomerProfile.Performer.UserName ); - var fsp = new PhysicalFileProvider(userdir); - var billsdir = Path.Combine(userdir, - Constants.UserBillsFilesDir); - foreach (var gr in newGraphics) { + ContentDisposition contentDisposition = new ContentDisposition(gr.ContentDisposition); gr.SaveAs( Path.Combine( Path.Combine(billsdir, estimate.Id.ToString()), - gr.ContentDisposition)); + contentDisposition.FileName)); + } + foreach (var formFile in newFiles) + { + ContentDisposition contentDisposition = new ContentDisposition(formFile.ContentDisposition); + formFile.SaveAs( + Path.Combine( + Path.Combine(billsdir, estimate.Id.ToString()), + contentDisposition.FileName)); } return RedirectToAction("Index"); } diff --git a/Yavsc/Controllers/ManageController.cs b/Yavsc/Controllers/ManageController.cs index 71bda019..655f51bf 100644 --- a/Yavsc/Controllers/ManageController.cs +++ b/Yavsc/Controllers/ManageController.cs @@ -483,7 +483,7 @@ namespace Yavsc.Controllers [HttpGet, Authorize] public IActionResult SetAvatar() { - throw new NotImplementedException(); + return View(); } [HttpGet, Authorize] diff --git a/Yavsc/Helpers/FileSystemHelpers.cs b/Yavsc/Helpers/FileSystemHelpers.cs index 7d1aef6a..cc294e89 100644 --- a/Yavsc/Helpers/FileSystemHelpers.cs +++ b/Yavsc/Helpers/FileSystemHelpers.cs @@ -1,4 +1,7 @@ + +using System.Drawing; +using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Net.Mime; @@ -59,8 +62,10 @@ namespace Yavsc.Helpers return root; } - public static FileRecievedInfo ReceiveUserFile(this ApplicationUser user, string root, long quota, ref long usage, IFormFile f) + public static FileRecievedInfo ReceiveUserFile(this ApplicationUser user, string root, IFormFile f) { + long usage = user.DiskUsage; + var item = new FileRecievedInfo(); // form-data; name="file"; filename="capt0008.jpg" ContentDisposition contentDisposition = new ContentDisposition(f.ContentDisposition); @@ -73,8 +78,8 @@ namespace Yavsc.Helpers { byte[] buffer = new byte[1024]; long len = org.Length; - user.DiskUsage += len; - if (len > (quota - usage)) throw new FSQuotaException(); + if (len > (user.DiskQuota - usage)) throw new FSQuotaException(); + usage += len; while (len > 0) { @@ -87,8 +92,79 @@ namespace Yavsc.Helpers org.Close(); } } + user.DiskUsage = usage; return item; } - } + public static FileRecievedInfo ReceiveAvatar(this ApplicationUser user, IFormFile formFile) + { + var item = new FileRecievedInfo(); + item.FileName = user.UserName + ".png"; + var destFileName = Path.Combine(Startup.SiteSetup.UserFiles.Avatars, item.FileName); + ImageProcessor.ImageFactory f = new ImageProcessor.ImageFactory(); + + ImageProcessor.Web.Processors.Resize r = new ImageProcessor.Web.Processors.Resize(); + + var fi = new FileInfo(destFileName); + if (fi.Exists) item.Overriden = true; + Rectangle cropRect = new Rectangle(); + + using (var org = formFile.OpenReadStream()) + { + Image i = Image.FromStream(org); + using (Bitmap source = new Bitmap(i)) + { + if (i.Width != i.Height) + { + if (i.Width > i.Height) + { + cropRect.X = (i.Width - i.Height) / 2; + cropRect.Y = 0; + cropRect.Width = i.Height; + cropRect.Height = i.Height; + } + else + { + cropRect.X = 0; + cropRect.Y = (i.Height - i.Width) / 2; + cropRect.Width = i.Width; + cropRect.Height = i.Width; + } + using (var cropped = source.Clone(cropRect, source.PixelFormat)) + { + CreateAvatars(user,cropped); + } + } + + } + + } + item.DestDir = "/Avatars"; + user.Avatar = item.FileName; + return item; + } + + private static void CreateAvatars(this ApplicationUser user, Bitmap source) + { + var dir = Startup.SiteSetup.UserFiles.Avatars; + var name = user.UserName + ".png"; + var smallname = user.UserName + ".s.png"; + var xsmallname = user.UserName + ".xs.png"; + using (Bitmap newBMP = new Bitmap(source, 128, 128)) + { + newBMP.Save(Path.Combine( + dir, name), ImageFormat.Png); + } + using (Bitmap newBMP = new Bitmap(source, 64, 64)) + { + newBMP.Save(Path.Combine( + dir, smallname), ImageFormat.Png); + } + using (Bitmap newBMP = new Bitmap(source, 32, 32)) + { + newBMP.Save(Path.Combine( + dir, xsmallname), ImageFormat.Png); + } + } + } } diff --git a/Yavsc/Settings/ThirdPartyFiles.cs b/Yavsc/Settings/ThirdPartyFiles.cs index b076c7ed..4c3d0a53 100644 --- a/Yavsc/Settings/ThirdPartyFiles.cs +++ b/Yavsc/Settings/ThirdPartyFiles.cs @@ -2,8 +2,8 @@ namespace Yavsc { public class ThirdPartyFiles { - public string DirName { get; set; } - public string Quota { get; set; } + public string Avatars { get; set; } + public long Quota { get; set; } public string Blog { get; set; } public string Bills { get; set; } diff --git a/Yavsc/Startup/Startup.FileServer.cs b/Yavsc/Startup/Startup.FileServer.cs index e46898df..5cfda1f9 100644 --- a/Yavsc/Startup/Startup.FileServer.cs +++ b/Yavsc/Startup/Startup.FileServer.cs @@ -13,10 +13,11 @@ namespace Yavsc public static string UserFilesDirName { get; private set; } public static FileServerOptions UserFilesOptions { get; private set; } + public static FileServerOptions AvatarsOptions { get; set; } public void ConfigureFileServerApp(IApplicationBuilder app, SiteSettings siteSettings, IHostingEnvironment env) { - var userFilesDirInfo = new DirectoryInfo( siteSettings.UserFiles.DirName ); + var userFilesDirInfo = new DirectoryInfo( siteSettings.UserFiles.Blog ); UserFilesDirName = userFilesDirInfo.FullName; if (!userFilesDirInfo.Exists) userFilesDirInfo.Create(); @@ -24,10 +25,24 @@ namespace Yavsc UserFilesOptions = new FileServerOptions() { FileProvider = new PhysicalFileProvider(UserFilesDirName), - RequestPath = new PathString("/" + siteSettings.UserFiles.DirName), + RequestPath = new PathString(Constants.UserFilesPath), EnableDirectoryBrowsing = env.IsDevelopment() }; + var avatarsDirInfo = new DirectoryInfo(Startup.SiteSetup.UserFiles.Avatars); + if (!avatarsDirInfo.Exists) avatarsDirInfo.Create(); + AvatarsDirName = avatarsDirInfo.FullName; + + AvatarsOptions = new FileServerOptions() + { + FileProvider = new PhysicalFileProvider(AvatarsDirName), + RequestPath = new PathString(Constants.AvatarsPath), + EnableDirectoryBrowsing = env.IsDevelopment() + }; + app.UseFileServer(UserFilesOptions); + + app.UseFileServer(AvatarsOptions); + app.UseStaticFiles(); } } diff --git a/Yavsc/Startup/Startup.cs b/Yavsc/Startup/Startup.cs index 3d11ff41..9a4638fb 100755 --- a/Yavsc/Startup/Startup.cs +++ b/Yavsc/Startup/Startup.cs @@ -33,6 +33,7 @@ namespace Yavsc { public static string ConnectionString { get; private set; } public static string UserBillsDirName { private set; get; } + public static string AvatarsDirName { private set; get; } public static string Authority { get; private set; } public static string Audience { get; private set; } public static SiteSettings SiteSetup { get; private set; } @@ -229,8 +230,8 @@ namespace Yavsc ILoggerFactory loggerFactory) { SiteSetup = siteSettings.Value; - Startup.UserFilesDirName = siteSettings.Value.UserFiles.DirName; - Startup.UserBillsDirName = siteSettings.Value.UserFiles.Bills; + Startup.UserFilesDirName = new DirectoryInfo(siteSettings.Value.UserFiles.Blog).FullName; + Startup.UserBillsDirName = new DirectoryInfo(siteSettings.Value.UserFiles.Bills).FullName; // TODO implement an installation & upgrade procedure // Create required directories diff --git a/Yavsc/ViewModels/Account/Me.cs b/Yavsc/ViewModels/Account/Me.cs index c79093d4..4b0e498d 100644 --- a/Yavsc/ViewModels/Account/Me.cs +++ b/Yavsc/ViewModels/Account/Me.cs @@ -31,6 +31,7 @@ namespace Yavsc.Models.Auth } public class MyUpdate { + public string UserName { get; set; } public string Avatar { get; set; } } diff --git a/Yavsc/Views/Manage/Index.cshtml b/Yavsc/Views/Manage/Index.cshtml index 5f0056be..94e04629 100755 --- a/Yavsc/Views/Manage/Index.cshtml +++ b/Yavsc/Views/Manage/Index.cshtml @@ -16,6 +16,7 @@ [@SR["Change"]] + @if (Model.Roles.Count()>0) {
@SR["Roles"]:
@@ -45,7 +46,9 @@
@SR["Avatar"]:
-
@Model.Avatar +
+ + @Model.Avatar [@SR[@Model.Avatar==null?"Set":"Modify"]]
diff --git a/Yavsc/Views/Manage/SetAvatar.cshtml b/Yavsc/Views/Manage/SetAvatar.cshtml new file mode 100644 index 00000000..09dd811d --- /dev/null +++ b/Yavsc/Views/Manage/SetAvatar.cshtml @@ -0,0 +1,32 @@ +@model PerformerProfile +@{ ViewData["Title"] = SR["Edit your avatar"]; } +@section header{ + +} +@section scripts{ + + +} + + +
+
+ +
+ + @Html.AntiForgeryToken() +
diff --git a/Yavsc/Views/Shared/_LoginPartial.cshtml b/Yavsc/Views/Shared/_LoginPartial.cshtml index 02ad72f5..c0ed3151 100755 --- a/Yavsc/Views/Shared/_LoginPartial.cshtml +++ b/Yavsc/Views/Shared/_LoginPartial.cshtml @@ -5,7 +5,9 @@