yavsc/Yavsc/Helpers/FileSystemHelpers.cs

194 lines
7.1 KiB
C#

8 years ago
8 years ago
using System;
8 years ago
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
8 years ago
using System.Net.Mime;
8 years ago
using System.Security.Claims;
using System.Web;
8 years ago
using Microsoft.AspNet.Http;
using Yavsc.Abstract.FileSystem;
using Yavsc.Exceptions;
8 years ago
using Yavsc.Models;
using Yavsc.Models.FileSystem;
using Yavsc.ViewModels;
8 years ago
using Yavsc.ViewModels.UserFiles;
namespace Yavsc.Helpers
{
8 years ago
public static class FileSystemHelpers
{
7 years ago
public static Func<string,string,long,string>
SignFileNameFormat = new Func<string,string,long,string> ((signType,billingCode,estimateId) => $"sign-{billingCode}-{signType}-{estimateId}.png");
8 years ago
7 years ago
public static FileRecievedInfo ReceiveProSignature(this ClaimsPrincipal user, string billingCode, long estimateId, IFormFile formFile, string signtype)
8 years ago
{
7 years ago
var item = new FileRecievedInfo();
item.FileName = SignFileNameFormat("pro",billingCode,estimateId);
item.MimeType = formFile.ContentDisposition;
var destFileName = Path.Combine(Startup.SiteSetup.UserFiles.Bills, item.FileName);
8 years ago
7 years ago
var fi = new FileInfo(destFileName);
if (fi.Exists) item.Overriden = true;
7 years ago
using (var org = formFile.OpenReadStream())
{
Image i = Image.FromStream(org);
using (Bitmap source = new Bitmap(i))
{
source.Save(destFileName, ImageFormat.Png);
}
}
return item;
8 years ago
}
7 years ago
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);
}
}
8 years ago
public static string InitPostToFileSystem(
this ClaimsPrincipal user,
string subpath)
{
7 years ago
var root = Path.Combine(AbstractFileSystemHelpers.UserFilesDirName, user.Identity.Name);
8 years ago
var diRoot = new DirectoryInfo(root);
if (!diRoot.Exists) diRoot.Create();
if (!string.IsNullOrWhiteSpace(subpath)) {
if (!subpath.IsValidYavscPath())
8 years ago
{
throw new InvalidPathException();
8 years ago
}
root = Path.Combine(root, subpath);
}
var di = new DirectoryInfo(root);
if (!di.Exists) di.Create();
return di.FullName;
8 years ago
}
public static void DeleteUserFile(this ApplicationUser user, string fileName)
{
var root = Path.Combine(Startup.UserFilesDirName, user.UserName);
var fi = new FileInfo(Path.Combine(root, fileName));
if (!fi.Exists) return ;
fi.Delete();
user.DiskUsage -= fi.Length;
}
public static FileRecievedInfo ReceiveUserFile(this ApplicationUser user, string root, IFormFile f, string destFileName = null)
8 years ago
{
8 years ago
long usage = user.DiskUsage;
8 years ago
var item = new FileRecievedInfo();
// form-data; name="file"; filename="capt0008.jpg"
ContentDisposition contentDisposition = new ContentDisposition(f.ContentDisposition);
7 years ago
item.FileName = Yavsc.Abstract.FileSystem.AbstractFileSystemHelpers.FilterFileName (destFileName ?? contentDisposition.FileName);
item.MimeType = contentDisposition.DispositionType;
item.DestDir = root;
8 years ago
var fi = new FileInfo(Path.Combine(root, item.FileName));
if (fi.Exists)
{
item.Overriden = true;
usage -= fi.Length;
}
8 years ago
using (var dest = fi.OpenWrite())
{
using (var org = f.OpenReadStream())
{
byte[] buffer = new byte[1024];
long len = org.Length;
if (len > (user.DiskQuota - usage)) {
item.QuotaOffensed = true;
return item;
}
8 years ago
usage += len;
8 years ago
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();
}
}
8 years ago
user.DiskUsage = usage;
8 years ago
return item;
}
public static HtmlString FileLink(this RemoteFileInfo info, string username, string subpath)
{
return new HtmlString( Startup.UserFilesOptions.RequestPath+"/"+ username + "/" + subpath + "/" +
HttpUtility.UrlEncode(info.Name) );
}
8 years ago
public static FileRecievedInfo ReceiveAvatar(this ApplicationUser user, IFormFile formFile)
{
var item = new FileRecievedInfo();
item.FileName = user.UserName + ".png";
8 years ago
var destFileName = Path.Combine(Startup.SiteSetup.UserFiles.Avatars, item.FileName);
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);
}
}
}
}
8 years ago
item.DestDir = Startup.AvatarsOptions.RequestPath.ToUriComponent();
user.Avatar = $"{item.DestDir}/{item.FileName}";
8 years ago
return item;
}
8 years ago
}
}