yavsc/src/Api/Controllers/Blogspot/FileSystemApiController.cs

187 lines
6.6 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
2016-11-30 11:05:10 +01:00
using System.Security.Claims;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
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;
2018-01-26 13:51:09 +01:00
using Yavsc.Models.FileSystem;
2019-09-04 01:25:36 +01:00
using System.ComponentModel.DataAnnotations;
using Yavsc.Attributes.Validation;
2019-09-08 05:17:21 +01:00
using System.IO;
2025-02-14 00:20:35 +00:00
using Yavsc.Exceptions;
2025-02-17 23:56:28 +00:00
using Yavsc.Server.Helpers;
using Yavsc.Abstract.Helpers;
2016-11-30 11:05:10 +01:00
2016-09-23 12:26:50 +02:00
[Authorize,Route("api/fs")]
2019-09-08 05:17:21 +01:00
public partial class FileSystemApiController : Controller
2016-09-23 12:26:50 +02:00
{
2020-10-09 19:35:39 +01:00
readonly ApplicationDbContext dbContext;
private readonly IAuthorizationService AuthorizationService;
private readonly ILogger _logger;
2018-01-26 13:51:09 +01:00
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;
2019-09-08 05:17:21 +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}")]
public IActionResult GetDir([ValidRemoteUserFilePath] string subdir="")
2019-08-23 22:21:19 +01:00
{
if (!ModelState.IsValid) return new BadRequestObjectResult(ModelState);
2019-08-25 01:47:46 +01:00
// _logger.LogInformation($"listing files from {User.Identity.Name}{subdir}");
2024-11-06 13:00:34 +00:00
var files = AbstractFileSystemHelpers.GetUserFiles(User.GetUserId(), 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}")]
public IActionResult Post([ValidRemoteUserFilePath] string subdir="")
2016-09-23 12:26:50 +02:00
{
if (!ModelState.IsValid) return new BadRequestObjectResult(ModelState);
2018-03-12 23:49:05 +01:00
string destDir = null;
2025-03-04 18:05:21 +00:00
List<FileReceivedInfo> received = new List<FileReceivedInfo>();
2017-05-28 00:42:27 +02:00
InvalidPathException pathex = null;
try {
2025-11-16 15:53:13 +00:00
destDir = User.EnsureDestinationDirectory(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) {
2019-09-08 05:17:21 +01:00
_logger.LogError($"invalid sub path: '{subdir}'.");
2023-03-19 17:57:55 +00:00
return BadRequest(pathex);
2018-07-30 09:37:35 +02:00
}
2019-09-08 05:17:21 +01:00
_logger.LogInformation($"Receiving files, saved in '{destDir}' (specified as '{subdir}').");
2018-07-30 09:37:35 +02:00
2024-11-10 23:12:02 +00: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;
2019-09-08 05:17:21 +01: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);
2019-09-08 05:17:21 +01:00
_logger.LogInformation($"Received '{item.FileName}'.");
2025-03-04 18:05:21 +00:00
if (item.QuotaOffense)
2018-01-26 13:51:09 +01:00
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
{
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
var user = dbContext.Users.FirstOrDefault(
2019-08-22 16:06:45 +01:00
u => u.UserName == uname
2019-03-31 00:13:56 +00:00
);
if (user==null) return new BadRequestObjectResult(new { error = "no such use" });
2019-03-31 00:13:56 +00:00
user.AddQuota(len);
dbContext.SaveChanges(uid);
return Ok(len);
}
2019-09-08 05:17:21 +01:00
[HttpPost]
[Route("/api/fsc/mvftd")]
2019-08-21 17:23:58 +01:00
[Authorize()]
2019-09-08 05:17:21 +01:00
public IActionResult MoveFile([FromBody] RenameFileQuery query)
2019-08-21 17:23:58 +01:00
{
if (!ModelState.IsValid) return new BadRequestObjectResult(ModelState);
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2019-08-21 17:23:58 +01:00
var user = dbContext.Users.Single(
u => u.Id == uid
);
2025-07-07 07:49:18 +01:00
var info = user.MoveUserFileToDir(query.Id, query.To);
2019-09-08 05:17:21 +01:00
if (!info.Done) return new BadRequestObjectResult(info);
2025-07-07 07:49:18 +01:00
return Ok(new { moved = query.Id });
2019-08-22 16:06:45 +01:00
}
2019-08-21 17:23:58 +01:00
2019-09-08 05:17:21 +01:00
[HttpPost]
[Route("/api/fsc/mvf")]
2019-08-22 16:06:45 +01:00
[Authorize()]
2019-09-08 05:17:21 +01:00
public IActionResult RenameFile([FromBody] RenameFileQuery query)
2019-08-22 16:06:45 +01:00
{
2019-09-08 05:17:21 +01:00
if (!ModelState.IsValid) {
var idvr = new ValidRemoteUserFilePathAttribute();
2025-07-07 07:49:18 +01:00
return this.BadRequest(new { id = idvr.IsValid(query.Id), to = idvr.IsValid(query.To), errors = ModelState });
2019-09-08 05:17:21 +01:00
}
2025-07-07 07:49:18 +01:00
_logger.LogInformation($"Valid move query: {query.Id} => {query.To}");
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
2019-08-22 16:06:45 +01:00
var user = dbContext.Users.Single(
u => u.Id == uid
);
2019-09-04 01:25:36 +01:00
try {
2025-07-07 07:49:18 +01:00
if (Config.UserFilesOptions.FileProvider.GetFileInfo(Path.Combine(user.UserName, query.Id)).Exists)
2019-09-08 05:17:21 +01:00
{
2025-07-07 07:49:18 +01:00
var result = user.MoveUserFile(query.Id, query.To);
2019-09-08 05:17:21 +01:00
if (!result.Done) return new BadRequestObjectResult(result);
}
else {
2025-07-07 07:49:18 +01:00
var result = user.MoveUserDir(query.Id, query.To);
2019-09-08 05:17:21 +01:00
if (!result.Done) return new BadRequestObjectResult(result);
}
2019-09-04 01:25:36 +01:00
}
catch (Exception ex)
{
return new BadRequestObjectResult(
new FsOperationInfo {
Done = false,
2019-09-08 05:17:21 +01:00
ErrorCode = ErrorCode.InternalError,
ErrorMessage = ex.Message
2019-09-04 01:25:36 +01:00
});
}
2019-08-21 17:23:58 +01:00
return Ok();
}
2019-09-05 21:01:44 +01:00
[HttpDelete("{*id}")]
public IActionResult RemoveDirOrFile ([ValidRemoteUserFilePath] string id)
2017-05-28 00:42:27 +02:00
{
if (!ModelState.IsValid) return new BadRequestObjectResult(ModelState);
2019-09-04 01:25:36 +01:00
var user = dbContext.Users.Single(
u => u.Id == User.GetUserId()
);
2019-09-05 21:01:44 +01:00
2019-09-04 01:25:36 +01:00
try {
2019-09-05 21:01:44 +01:00
var result = user.DeleteUserDirOrFile(id);
2019-09-04 01:25:36 +01:00
if (!result.Done)
return new BadRequestObjectResult(result);
}
2019-09-05 21:01:44 +01:00
2019-09-04 01:25:36 +01:00
catch (Exception ex)
{
return new BadRequestObjectResult(
new FsOperationInfo {
Done = false,
2019-09-08 05:17:21 +01:00
ErrorCode = ErrorCode.InternalError,
ErrorMessage = ex.Message
2019-09-04 01:25:36 +01:00
});
}
return Ok(new { deleted=id });
2017-05-28 00:42:27 +02:00
}
2020-10-18 23:00:17 +01:00
2016-09-23 12:26:50 +02:00
}
2019-09-04 01:25:36 +01:00
}