2016-11-30 11:05:10 +01:00
|
|
|
using System;
|
2016-09-23 12:26:50 +02:00
|
|
|
using System.Collections.Generic;
|
2016-11-30 11:05:10 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
2016-09-23 12:26:50 +02:00
|
|
|
using Microsoft.AspNet.Authorization;
|
|
|
|
|
using Microsoft.AspNet.Mvc;
|
2016-11-14 12:17:07 +01:00
|
|
|
using Yavsc.Helpers;
|
2016-09-23 12:26:50 +02:00
|
|
|
using Yavsc.Models;
|
|
|
|
|
|
|
|
|
|
namespace Yavsc.ApiControllers
|
|
|
|
|
{
|
2017-05-28 00:42:27 +02:00
|
|
|
using System.Threading.Tasks;
|
2018-01-26 13:51:09 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-08-21 17:23:58 +01:00
|
|
|
using Yavsc.Helpers;
|
2017-04-10 23:54:06 +02:00
|
|
|
using Yavsc.Exceptions;
|
2018-01-26 13:51:09 +01:00
|
|
|
using Yavsc.Models.FileSystem;
|
|
|
|
|
|
2016-11-30 11:05:10 +01:00
|
|
|
public class FSQuotaException : Exception {
|
2017-04-10 23:54:06 +02:00
|
|
|
|
2016-11-30 11:05:10 +01:00
|
|
|
}
|
|
|
|
|
|
2016-09-23 12:26:50 +02:00
|
|
|
[Authorize,Route("api/fs")]
|
|
|
|
|
public class FileSystemApiController : Controller
|
|
|
|
|
{
|
2016-11-30 11:05:10 +01:00
|
|
|
ApplicationDbContext dbContext;
|
2016-09-23 12:26:50 +02:00
|
|
|
private IAuthorizationService AuthorizationService;
|
2018-01-26 13:51:09 +01:00
|
|
|
private ILogger logger;
|
|
|
|
|
|
2016-09-23 12:26:50 +02:00
|
|
|
public FileSystemApiController(ApplicationDbContext context,
|
2018-01-26 13:51:09 +01:00
|
|
|
IAuthorizationService authorizationService,
|
|
|
|
|
ILoggerFactory loggerFactory)
|
2016-09-23 12:26:50 +02:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
AuthorizationService = authorizationService;
|
2016-11-30 11:05:10 +01:00
|
|
|
dbContext = context;
|
2018-01-26 13:51:09 +01:00
|
|
|
logger = loggerFactory.CreateLogger<FileSystemApiController>();
|
2016-09-23 12:26:50 +02:00
|
|
|
}
|
2017-04-10 23:54:06 +02:00
|
|
|
|
2016-09-23 12:26:50 +02:00
|
|
|
[HttpGet()]
|
|
|
|
|
public IActionResult Get()
|
|
|
|
|
{
|
|
|
|
|
return GetDir(null);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-30 09:37:35 +02:00
|
|
|
[HttpGet("{*subdir}")]
|
2016-11-14 12:17:07 +01:00
|
|
|
public IActionResult GetDir(string subdir="")
|
2016-09-23 12:26:50 +02:00
|
|
|
{
|
2016-11-14 12:17:07 +01:00
|
|
|
if (subdir !=null)
|
2018-01-16 13:35:54 +01:00
|
|
|
if (!subdir.IsValidYavscPath())
|
2016-11-14 12:17:07 +01:00
|
|
|
return new BadRequestResult();
|
2019-08-21 17:23:58 +01:00
|
|
|
var files = AbstractFileSystemHelpers.GetUserFiles(User.Identity.Name, subdir);
|
2016-11-14 12:17:07 +01:00
|
|
|
return Ok(files);
|
2016-09-23 12:26:50 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-30 09:37:35 +02:00
|
|
|
[HttpPost("{*subdir}")]
|
2018-03-12 23:49:05 +01:00
|
|
|
public IActionResult Post(string subdir="")
|
2016-09-23 12:26:50 +02:00
|
|
|
{
|
2018-03-12 23:49:05 +01:00
|
|
|
string destDir = null;
|
2018-01-26 13:51:09 +01:00
|
|
|
List<FileRecievedInfo> received = new List<FileRecievedInfo>();
|
2017-05-28 00:42:27 +02:00
|
|
|
InvalidPathException pathex = null;
|
2017-04-10 23:54:06 +02:00
|
|
|
try {
|
2018-03-12 23:49:05 +01:00
|
|
|
destDir = User.InitPostToFileSystem(subdir);
|
2017-05-28 00:42:27 +02:00
|
|
|
} catch (InvalidPathException ex) {
|
|
|
|
|
pathex = ex;
|
|
|
|
|
}
|
2018-07-30 09:37:35 +02:00
|
|
|
if (pathex!=null) {
|
|
|
|
|
logger.LogError($"invalid sub path: '{subdir}'.");
|
|
|
|
|
return HttpBadRequest(pathex);
|
|
|
|
|
}
|
2018-07-31 14:30:23 +02:00
|
|
|
logger.LogInformation($"Receiving files, saved in '{destDir}' (specified as '{subdir}').");
|
2018-07-30 09:37:35 +02:00
|
|
|
|
2018-01-26 13:51:09 +01:00
|
|
|
var uid = User.GetUserId();
|
2016-11-30 11:05:10 +01:00
|
|
|
var user = dbContext.Users.Single(
|
2018-01-26 13:51:09 +01:00
|
|
|
u => u.Id == uid
|
2016-11-30 11:05:10 +01:00
|
|
|
);
|
2018-01-16 13:35:54 +01:00
|
|
|
int i=0;
|
2018-07-31 14:30:23 +02:00
|
|
|
logger.LogInformation($"Receiving {Request.Form.Files.Count} files.");
|
2018-03-12 23:49:05 +01:00
|
|
|
|
2016-11-30 11:05:10 +01:00
|
|
|
foreach (var f in Request.Form.Files)
|
2016-09-23 12:26:50 +02:00
|
|
|
{
|
2018-03-12 23:49:05 +01:00
|
|
|
var item = user.ReceiveUserFile(destDir, f);
|
2017-02-23 03:10:30 +01:00
|
|
|
dbContext.SaveChanges(User.GetUserId());
|
2018-01-26 13:51:09 +01:00
|
|
|
received.Add(item);
|
2018-07-31 14:30:23 +02:00
|
|
|
logger.LogInformation($"Received '{item.FileName}'.");
|
2018-01-26 13:51:09 +01:00
|
|
|
if (item.QuotaOffensed)
|
|
|
|
|
break;
|
|
|
|
|
i++;
|
2016-09-23 12:26:50 +02:00
|
|
|
};
|
2018-01-26 13:51:09 +01:00
|
|
|
return Ok(received);
|
2016-09-23 12:26:50 +02:00
|
|
|
}
|
2017-05-28 00:42:27 +02:00
|
|
|
|
2019-08-21 17:23:58 +01:00
|
|
|
[Route("/api/fs/addquota/{len}")]
|
2019-03-31 00:13:56 +00:00
|
|
|
[Authorize("AdministratorOnly")]
|
|
|
|
|
public IActionResult AddQuota(int len)
|
|
|
|
|
{
|
|
|
|
|
var uid = User.GetUserId();
|
|
|
|
|
var user = dbContext.Users.Single(
|
|
|
|
|
u => u.Id == uid
|
|
|
|
|
);
|
|
|
|
|
user.AddQuota(len);
|
|
|
|
|
dbContext.SaveChanges(uid);
|
|
|
|
|
return Ok(len);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 17:23:58 +01:00
|
|
|
[Route("/api/fs/move")]
|
|
|
|
|
[Authorize()]
|
|
|
|
|
public IActionResult Move(string from, string to)
|
|
|
|
|
{
|
|
|
|
|
var uid = User.GetUserId();
|
|
|
|
|
var user = dbContext.Users.Single(
|
|
|
|
|
u => u.Id == uid
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-28 00:42:27 +02:00
|
|
|
[HttpDelete]
|
|
|
|
|
public async Task <IActionResult> Delete (string id)
|
|
|
|
|
{
|
|
|
|
|
var user = dbContext.Users.Single(
|
|
|
|
|
u => u.Id == User.GetUserId()
|
|
|
|
|
);
|
|
|
|
|
InvalidPathException pathex = null;
|
|
|
|
|
string root = null;
|
|
|
|
|
try {
|
|
|
|
|
root = User.InitPostToFileSystem(id);
|
|
|
|
|
} catch (InvalidPathException ex) {
|
|
|
|
|
pathex = ex;
|
|
|
|
|
}
|
|
|
|
|
if (pathex!=null)
|
|
|
|
|
return new BadRequestObjectResult(pathex);
|
|
|
|
|
user.DeleteUserFile(id);
|
|
|
|
|
await dbContext.SaveChangesAsync(User.GetUserId());
|
|
|
|
|
return Ok(new { deleted=id });
|
|
|
|
|
}
|
2016-09-23 12:26:50 +02:00
|
|
|
}
|
2017-04-10 23:54:06 +02:00
|
|
|
}
|