Disk quota
This commit is contained in:
@ -1,5 +1,9 @@
|
||||
using System;
|
||||
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.Mvc;
|
||||
using Yavsc.Helpers;
|
||||
@ -7,15 +11,22 @@ using Yavsc.Models;
|
||||
|
||||
namespace Yavsc.ApiControllers
|
||||
{
|
||||
|
||||
public class FSQuotaException : Exception {
|
||||
|
||||
}
|
||||
|
||||
[Authorize,Route("api/fs")]
|
||||
public class FileSystemApiController : Controller
|
||||
{
|
||||
ApplicationDbContext dbContext;
|
||||
private IAuthorizationService AuthorizationService;
|
||||
public FileSystemApiController(ApplicationDbContext context,
|
||||
IAuthorizationService authorizationService)
|
||||
|
||||
{
|
||||
AuthorizationService = authorizationService;
|
||||
dbContext = context;
|
||||
}
|
||||
|
||||
[HttpGet()]
|
||||
@ -37,7 +48,7 @@ namespace Yavsc.ApiControllers
|
||||
public class FileRecievedInfo
|
||||
{
|
||||
public string DestDir { get; set; }
|
||||
public string ContentDisposition { get; set; }
|
||||
public string FileName { get; set; }
|
||||
public bool Overriden { get; set; }
|
||||
}
|
||||
[HttpPost]
|
||||
@ -49,28 +60,40 @@ namespace Yavsc.ApiControllers
|
||||
var diRoot = new DirectoryInfo(root);
|
||||
if (!diRoot.Exists) diRoot.Create();
|
||||
|
||||
foreach (var f in Request.Form.Files.GetFiles("Files"))
|
||||
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 = new FileRecievedInfo();
|
||||
item.ContentDisposition = f.ContentDisposition;
|
||||
var fi = new FileInfo(Path.Combine(root, f.ContentDisposition));
|
||||
// form-data; name="file"; filename="capt0008.jpg"
|
||||
ContentDisposition contentDisposition = new ContentDisposition(f.ContentDisposition);
|
||||
item.FileName = contentDisposition.FileName;
|
||||
var fi = new FileInfo(Path.Combine(root, item.FileName));
|
||||
if (fi.Exists) item.Overriden = true;
|
||||
using (var dest = fi.OpenWrite())
|
||||
{
|
||||
using (var org = f.OpenReadStream())
|
||||
{
|
||||
byte[] buffer = new byte[1024];
|
||||
int o = 0, c;
|
||||
while ((c = org.Read(buffer, o, 1024)) > 0)
|
||||
{
|
||||
dest.Write(buffer, o, c);
|
||||
o += 1024;
|
||||
// TODO quota
|
||||
long len = org.Length;
|
||||
user.DiskUsage += len;
|
||||
if (len> (quota-usage)) throw new FSQuotaException();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
yield return item;
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user