Avatar
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user