yavsc/src/Yavsc/ApiControllers/Blogspot/FileSystemApiController.cs

189 lines
5.8 KiB
C#
Raw Normal View History

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;
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;
using Yavsc.Exceptions;
2018-01-26 13:51:09 +01:00
using Yavsc.Models.FileSystem;
2019-09-04 01:25:36 +01:00
using System.ComponentModel.DataAnnotations;
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
}
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="")
2019-08-23 22:21:19 +01:00
{
2016-11-14 12:17:07 +01:00
if (subdir !=null)
if (!subdir.IsValidYavscPath())
2016-11-14 12:17:07 +01:00
return new BadRequestResult();
2019-08-25 01:47:46 +01:00
// _logger.LogInformation($"listing files from {User.Identity.Name}{subdir}");
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
{
2019-09-04 01:25:36 +01: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;
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
);
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-09-04 01:25:36 +01:00
[Route("/api/fsc/addquota/{uname}/{len}")]
2019-03-31 00:13:56 +00:00
[Authorize("AdministratorOnly")]
2019-08-22 16:06:45 +01:00
public IActionResult AddQuota(string uname, int len)
2019-03-31 00:13:56 +00:00
{
var uid = User.GetUserId();
var user = dbContext.Users.Single(
2019-08-22 16:06:45 +01:00
u => u.UserName == uname
2019-03-31 00:13:56 +00:00
);
user.AddQuota(len);
dbContext.SaveChanges(uid);
return Ok(len);
}
2019-09-04 01:25:36 +01:00
[Route("/api/fsc/movefile")]
2019-08-21 17:23:58 +01:00
[Authorize()]
2019-08-22 16:06:45 +01:00
public IActionResult MoveFile(string from, string to)
2019-08-21 17:23:58 +01:00
{
var uid = User.GetUserId();
var user = dbContext.Users.Single(
u => u.Id == uid
);
2019-09-04 01:25:36 +01:00
var info = user.MoveUserFile(from, to);
if (!info.Done)
return new BadRequestObjectResult(info);
2019-08-22 16:06:45 +01:00
return Ok();
}
2019-08-21 17:23:58 +01:00
2019-09-04 01:25:36 +01:00
[HttpPatch]
[Route("/api/fsc/movedir")]
2019-08-22 16:06:45 +01:00
[Authorize()]
public IActionResult MoveDir(string from, string to)
{
var uid = User.GetUserId();
var user = dbContext.Users.Single(
u => u.Id == uid
);
2019-09-04 01:25:36 +01:00
try {
var result = user.MoveUserDir(from, to);
if (!result.Done)
return new BadRequestObjectResult(result);
}
catch (Exception ex)
{
return new BadRequestObjectResult(
new FsOperationInfo {
Done = false,
Error = ex.Message
});
}
2019-08-21 17:23:58 +01:00
return Ok();
}
2017-05-28 00:42:27 +02:00
[HttpDelete]
2019-09-04 01:25:36 +01:00
[Route("/api/fsc/rm/{*id}")]
2017-05-28 00:42:27 +02:00
public async Task <IActionResult> Delete (string id)
{
var user = dbContext.Users.Single(
u => u.Id == User.GetUserId()
);
try {
user.DeleteUserFile(id);
await dbContext.SaveChangesAsync(User.GetUserId());
2019-09-04 01:25:36 +01:00
}
catch (Exception ex)
{
return new BadRequestObjectResult(
new FsOperationInfo {
Done = false,
Error = ex.Message
});
}
return Ok(new { deleted=id });
}
[HttpDelete]
[Route("/api/fsc/rmdir/{*id}")]
public IActionResult RemoveDir (string id)
{
var user = dbContext.Users.Single(
u => u.Id == User.GetUserId()
);
try {
var result = user.DeleteUserDir(id);
if (!result.Done)
return new BadRequestObjectResult(result);
}
catch (Exception ex)
{
return new BadRequestObjectResult(
new FsOperationInfo {
Done = false,
Error = ex.Message
});
}
return Ok(new { deleted=id });
2017-05-28 00:42:27 +02:00
}
2016-09-23 12:26:50 +02:00
}
2019-09-04 01:25:36 +01:00
}