Avatar
This commit is contained in:
@ -138,40 +138,34 @@ namespace Yavsc.WebApi.Controllers
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Actually only updates the user's name.
|
||||
/// </summary>
|
||||
/// <param name="me">MyUpdate containing the new user name </param>
|
||||
/// <returns>Ok when all is ok.</returns>
|
||||
[HttpPut("~/api/me")]
|
||||
public async Task<IActionResult> 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);
|
||||
}
|
||||
}
|
||||
if (result.Succeeded)
|
||||
return Ok();
|
||||
else return new BadRequestObjectResult(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the avatar
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost("~/api/setavatar")]
|
||||
public async Task<IActionResult> 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);
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
|
BIN
Yavsc/Avatars/Paul.png
Normal file
BIN
Yavsc/Avatars/Paul.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
BIN
Yavsc/Avatars/Paul.s.png
Normal file
BIN
Yavsc/Avatars/Paul.s.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
BIN
Yavsc/Avatars/Paul.xs.png
Normal file
BIN
Yavsc/Avatars/Paul.xs.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.2 KiB |
@ -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"},
|
||||
|
@ -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);
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -483,7 +483,7 @@ namespace Yavsc.Controllers
|
||||
[HttpGet, Authorize]
|
||||
public IActionResult SetAvatar()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet, Authorize]
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -31,6 +31,7 @@ namespace Yavsc.Models.Auth
|
||||
}
|
||||
|
||||
public class MyUpdate {
|
||||
|
||||
public string UserName { get; set; }
|
||||
public string Avatar { get; set; }
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
[<a asp-controller="Manage"
|
||||
asp-action="CHUN">@SR["Change"]</a>]
|
||||
</dd>
|
||||
|
||||
@if (Model.Roles.Count()>0) {
|
||||
<dt>@SR["Roles"]:</dt>
|
||||
<dd>
|
||||
@ -45,7 +46,9 @@
|
||||
</dd>
|
||||
|
||||
<dt>@SR["Avatar"]:</dt>
|
||||
<dd>@Model.Avatar
|
||||
<dd>
|
||||
<img src="~/Avatars/@(User.Identity.Name).png">
|
||||
@Model.Avatar
|
||||
[<a asp-controller="Manage" asp-action="SetAvatar"
|
||||
>@SR[@Model.Avatar==null?"Set":"Modify"]</a>]
|
||||
</dd>
|
||||
|
32
Yavsc/Views/Manage/SetAvatar.cshtml
Normal file
32
Yavsc/Views/Manage/SetAvatar.cshtml
Normal file
@ -0,0 +1,32 @@
|
||||
@model PerformerProfile
|
||||
@{ ViewData["Title"] = SR["Edit your avatar"]; }
|
||||
@section header{
|
||||
<link href="~/css/dropzone.css" rel="stylesheet">
|
||||
}
|
||||
@section scripts{
|
||||
<script src="~/js/dropzone.js"></script>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
Dropzone.options.postavatar= {
|
||||
maxFilesize: 2, // MB
|
||||
autoProcessQueue: true,
|
||||
accept: function(file, done) {
|
||||
if (file.name == "justinbieber.jpg") {
|
||||
done("Naha, you don't.");
|
||||
}
|
||||
else { done(); }
|
||||
},
|
||||
url: "/api/setavatar"
|
||||
};
|
||||
});
|
||||
</script>
|
||||
}
|
||||
<img src="~/Avatars/@(User.Identity.Name).png">
|
||||
|
||||
<form id="postavatar" class="dropzone" method="post" enctype="multipart/form-data">
|
||||
<div class="fallback">
|
||||
<input name="Avatar" type="file" id="Avatar" />
|
||||
</div>
|
||||
|
||||
@Html.AntiForgeryToken()
|
||||
</form>
|
@ -5,7 +5,9 @@
|
||||
<form asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm" class="navbar-right">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li>
|
||||
<a asp-controller="Manage" class="navbar-link" asp-action="Index" title="Manage">@SR["Hello"] @User.GetUserName()!</a>
|
||||
<a asp-controller="Manage" class="navbar-link" asp-action="Index" title="Manage">
|
||||
<img src="~/Avatars/@(User.Identity.Name).xs.png" />
|
||||
@SR["Hello"] @User.GetUserName()!</a>
|
||||
</li>
|
||||
<li>
|
||||
<button type="submit" class="btn btn-link navbar-btn navbar-link" >@SR["Logout"]</button>
|
||||
|
@ -26,8 +26,8 @@
|
||||
"Address": "contact@company.com"
|
||||
},
|
||||
"UserFiles": {
|
||||
"DirName": "UserFiles",
|
||||
"Quota": "200M",
|
||||
"Avatars": "Avatars",
|
||||
"Quota": 200000000,
|
||||
"Bills":"Bills",
|
||||
"Blog":"Blog"
|
||||
},
|
||||
|
Reference in New Issue
Block a user