yavsc/src/Yavsc/Helpers/FileSystemHelpers.cs

235 lines
8.6 KiB
C#

9 years ago
9 years ago
using System;
using System.Collections.Generic;
9 years ago
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
9 years ago
using System.Net.Mime;
9 years ago
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
9 years ago
using Microsoft.AspNet.Http;
using Microsoft.Extensions.Logging;
using Yavsc.Abstract.FileSystem;
using Yavsc.Exceptions;
9 years ago
using Yavsc.Models;
using Yavsc.Models.FileSystem;
7 years ago
using Yavsc.Models.Streaming;
using Yavsc.ViewModels;
namespace Yavsc.Helpers
{
9 years ago
public static class FileSystemHelpers
{
8 years ago
public static Func<string,string,long,string>
SignFileNameFormat = new Func<string,string,long,string> ((signType,billingCode,estimateId) => $"sign-{billingCode}-{signType}-{estimateId}.png");
9 years ago
8 years ago
public static FileRecievedInfo ReceiveProSignature(this ClaimsPrincipal user, string billingCode, long estimateId, IFormFile formFile, string signtype)
9 years ago
{
8 years ago
var item = new FileRecievedInfo();
item.FileName = SignFileNameFormat("pro",billingCode,estimateId);
item.MimeType = formFile.ContentDisposition;
8 years ago
var destFileName = Path.Combine(Startup.SiteSetup.Bills, item.FileName);
9 years ago
8 years ago
var fi = new FileInfo(destFileName);
if (fi.Exists) item.Overriden = true;
8 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;
9 years ago
}
6 years ago
/// <summary>
/// Create avatars
/// </summary>
/// <param name="user"></param>
/// <param name="source"></param>
8 years ago
private static void CreateAvatars(this ApplicationUser user, Bitmap source)
{
8 years ago
var dir = Startup.SiteSetup.Avatars;
8 years ago
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);
}
}
9 years ago
public static string InitPostToFileSystem(
this ClaimsPrincipal user,
string subpath)
{
8 years ago
var root = Path.Combine(AbstractFileSystemHelpers.UserFilesDirName, user.Identity.Name);
9 years ago
var diRoot = new DirectoryInfo(root);
if (!diRoot.Exists) diRoot.Create();
if (!string.IsNullOrWhiteSpace(subpath)) {
if (!subpath.IsValidYavscPath())
9 years ago
{
throw new InvalidPathException();
9 years ago
}
root = Path.Combine(root, subpath);
}
var di = new DirectoryInfo(root);
if (!di.Exists) di.Create();
return di.FullName;
9 years ago
}
7 years ago
/// <summary>
/// Deletes user file.
/// User info is modified, but not save in db.
/// </summary>
/// <param name="user"></param>
/// <param name="fileName"></param>
public static void DeleteUserFile(this ApplicationUser user, string fileName)
{
var root = Path.Combine(AbstractFileSystemHelpers.UserFilesDirName, user.UserName);
var fi = new FileInfo(Path.Combine(root, fileName));
if (!fi.Exists) return ;
fi.Delete();
user.DiskUsage -= fi.Length;
}
public static void AddQuota(this ApplicationUser user, int quota)
{
user.DiskQuota += quota;
}
public static FileRecievedInfo ReceiveUserFile(this ApplicationUser user, string root, IFormFile f, string destFileName = null)
9 years ago
{
return ReceiveUserFile(user, root, f.OpenReadStream(), destFileName ?? f.ContentDisposition, f.ContentType, CancellationToken.None);
}
public static FileRecievedInfo ReceiveUserFile(this ApplicationUser user, string root, Stream inputStream, string destFileName, string contentType, CancellationToken token)
{
// TODO lock user's disk usage for this scope,
// this process is not safe at concurrent access.
long usage = user.DiskUsage;
9 years ago
var item = new FileRecievedInfo();
item.FileName = Yavsc.Abstract.FileSystem.AbstractFileSystemHelpers.FilterFileName (destFileName);
item.MimeType = contentType;
item.DestDir = root;
var fi = new FileInfo(Path.Combine(root, item.FileName));
if (fi.Exists)
{
item.Overriden = true;
usage -= fi.Length;
}
using (var dest = fi.OpenWrite())
{
using (inputStream)
{
const int blen = 1024;
byte[] buffer = new byte[blen];
int len = 0;
while (!token.IsCancellationRequested && (len=inputStream.Read(buffer, 0, blen))>0)
9 years ago
{
dest.Write(buffer, 0, len);
usage += len;
if (usage >= user.DiskQuota) break;
9 years ago
}
user.DiskUsage = usage;
9 years ago
dest.Close();
inputStream.Close();
9 years ago
}
}
if (usage >= user.DiskQuota) {
item.QuotaOffensed = true;
}
9 years ago
user.DiskUsage = usage;
9 years ago
return item;
}
public static HtmlString FileLink(this RemoteFileInfo info, string username, string subpath)
{
return new HtmlString( Startup.UserFilesOptions.RequestPath+"/"+ username +
"/" + (( subpath == null ) ? "" : "/" + subpath ) +
info.Name );
}
9 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.Avatars, item.FileName);
9 years ago
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);
}
}
}
}
9 years ago
item.DestDir = Startup.AvatarsOptions.RequestPath.ToUriComponent();
user.Avatar = $"{item.DestDir}/{item.FileName}";
9 years ago
return item;
}
7 years ago
public static string GetFileUrl (this LiveFlow flow)
{
if (flow.DifferedFileName==null) return null;
7 years ago
// no server-side backup for this stream
return $"{Startup.UserFilesOptions.RequestPath}/{flow.Owner.UserName}/live/"+GetFileName(flow);
}
6 years ago
public static string GetFileName (this LiveFlow flow)
{
7 years ago
var fileInfo = new FileInfo(flow.DifferedFileName);
var ext = fileInfo.Extension;
var namelen = flow.DifferedFileName.Length - ext.Length;
var basename = flow.DifferedFileName.Substring(0,namelen);
return $"{basename}-{flow.SequenceNumber}{ext}";
7 years ago
}
9 years ago
}
}