Using YaStringLength and YaRequired

This commit is contained in:
Paul Schneider 2019-09-04 01:25:36 +01:00
commit 9457ba71d1
62 changed files with 396 additions and 150 deletions

View file

@ -13,10 +13,7 @@ namespace Yavsc.ApiControllers
using Yavsc.Helpers;
using Yavsc.Exceptions;
using Yavsc.Models.FileSystem;
public class FSQuotaException : Exception {
}
using System.ComponentModel.DataAnnotations;
[Authorize,Route("api/fs")]
public class FileSystemApiController : Controller
@ -55,6 +52,7 @@ namespace Yavsc.ApiControllers
[HttpPost("{*subdir}")]
public IActionResult Post(string subdir="")
{
string destDir = null;
List<FileRecievedInfo> received = new List<FileRecievedInfo>();
InvalidPathException pathex = null;
@ -89,7 +87,7 @@ namespace Yavsc.ApiControllers
return Ok(received);
}
[Route("/api/fsquota/add/{uname}/{len}")]
[Route("/api/fsc/addquota/{uname}/{len}")]
[Authorize("AdministratorOnly")]
public IActionResult AddQuota(string uname, int len)
{
@ -102,7 +100,7 @@ namespace Yavsc.ApiControllers
return Ok(len);
}
[Route("/api/movefile")]
[Route("/api/fsc/movefile")]
[Authorize()]
public IActionResult MoveFile(string from, string to)
{
@ -110,11 +108,14 @@ namespace Yavsc.ApiControllers
var user = dbContext.Users.Single(
u => u.Id == uid
);
throw new NotImplementedException();
var info = user.MoveUserFile(from, to);
if (!info.Done)
return new BadRequestObjectResult(info);
return Ok();
}
[Route("/api/movedir")]
[HttpPatch]
[Route("/api/fsc/movedir")]
[Authorize()]
public IActionResult MoveDir(string from, string to)
{
@ -122,29 +123,67 @@ namespace Yavsc.ApiControllers
var user = dbContext.Users.Single(
u => u.Id == uid
);
throw new NotImplementedException();
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
});
}
return Ok();
}
[HttpDelete]
[Route("/api/fsc/rm/{*id}")]
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 });
}
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 });
}
}
}

View file

@ -0,0 +1,15 @@
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
namespace Yavsc.ApiControllers
{
using System.ComponentModel.DataAnnotations;
using Yavsc.Attributes.Validation;
[Authorize,Route("~/api/test")]
public class TestApiController : Controller
{
}
}

View file

@ -7,6 +7,7 @@ using System.IO;
using System.Security.Claims;
using System.Threading;
using System.Web;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Http;
using Yavsc.Exceptions;
using Yavsc.Models;
@ -102,6 +103,58 @@ namespace Yavsc.Helpers
fi.Delete();
user.DiskUsage -= fi.Length;
}
public static FsOperationInfo DeleteUserDir(this ApplicationUser user, string dirName)
{
var root = Path.Combine(AbstractFileSystemHelpers.UserFilesDirName, user.UserName);
if (string.IsNullOrEmpty(dirName))
return new FsOperationInfo { Done = false, Error = "specify a dir name"} ;
var di = new DirectoryInfo(Path.Combine(root, dirName));
if (!di.Exists) return new FsOperationInfo { Done = false, Error = "non existent"} ;
if (di.GetDirectories().Length>0 || di.GetFiles().Length>0)
return new FsOperationInfo { Done = false, Error = "not eñpty"} ;
di.Delete();
return new FsOperationInfo { Done = true };
}
public static FsOperationInfo MoveUserDir(this ApplicationUser user, string fromDirName, string toDirName)
{
var root = Path.Combine(AbstractFileSystemHelpers.UserFilesDirName, user.UserName);
if (string.IsNullOrEmpty(fromDirName))
return new FsOperationInfo { Done = false, Error = "fromDirName: specify a dir name "} ;
var di = new DirectoryInfo(Path.Combine(root, fromDirName));
if (!di.Exists) return new FsOperationInfo { Done = false, Error = "fromDirName: non existent"} ;
if (string.IsNullOrEmpty(toDirName))
return new FsOperationInfo { Done = false, Error = "toDirName: specify a dir name to move"} ;
var destPath = Path.Combine(root, toDirName);
var fo = new FileInfo(destPath);
var dout = new DirectoryInfo(destPath);
if (fo.Exists) return new FsOperationInfo { Done = false, Error = "toDirName: yet a regular file" } ;
if (dout.Exists) {
destPath = Path.Combine(destPath, fo.Name);
}
di.MoveTo(destPath);
return new FsOperationInfo { Done = true };
}
public static FsOperationInfo MoveUserFile(this ApplicationUser user, string fileNameFrom, string fileNameDest)
{
var root = Path.Combine(AbstractFileSystemHelpers.UserFilesDirName, user.UserName);
var fi = new FileInfo(Path.Combine(root, fileNameFrom));
if (!fi.Exists) return new FsOperationInfo { Error = "no file to move" } ;
var fo = new FileInfo(Path.Combine(root, fileNameDest));
if (fo.Exists) return new FsOperationInfo { Error = "destination file name is an existing file" } ;
fi.MoveTo(fo.FullName);
return new FsOperationInfo { Done = true };
}
static string ParseFileNameFromDisposition(string disposition)
{
@ -167,6 +220,15 @@ namespace Yavsc.Helpers
return new HtmlString(
$"{Startup.UserFilesOptions.RequestPath}/{username}/{subpath}/{info.Name}" );
}
public static RemoteFileInfo FileInfo(this ApplicationUser user, string path)
{
IFileInfo info = Startup.UserFilesOptions.FileProvider.GetFileInfo($"{user.UserName}/{path}");
if (!info.Exists) return null;
return new RemoteFileInfo{ Name = info.Name, Size = info.Length, LastModified = info.LastModified.UtcDateTime };
}
public static FileRecievedInfo ReceiveAvatar(this ApplicationUser user, IFormFile formFile)
{
var item = new FileRecievedInfo();

View file

@ -1,16 +1,17 @@
using System.ComponentModel.DataAnnotations;
using Yavsc.Attributes.Validation;
namespace Yavsc.ViewModels
{
public partial class EnrolerViewModel {
[Display(Name="EnroledLabel", ResourceType=typeof(EnrolerViewModel))]
[Required]
[YaRequired]
public string EnroledUserId { get; set; }
[Display(Name="RoleNameLabel", ResourceType=typeof(EnrolerViewModel))]
[Required]
[YaRequired]
public string RoleName { get; set; }
}
}

View file

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using Yavsc.Attributes.Validation;
namespace Yavsc.ViewModels
{
@ -7,12 +8,12 @@ namespace Yavsc.ViewModels
[Display(Name="EnroledLabel", ResourceType=typeof(EnrolerViewModel))]
public string EnroledUserName { get; set; }
[Required]
[YaRequired]
public string EnroledUserId { get; set; }
[Display(Name="RoleNameLabel", ResourceType=typeof(EnrolerViewModel))]
[Required]
[YaRequired]
public string RoleName { get; set; }
}
}

View file

@ -1,16 +1,17 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNet.Mvc.Rendering;
using Yavsc.Attributes.Validation;
namespace Yavsc.ViewModels.Gen
{
public class PdfGenerationViewModel
{
[Required]
[YaRequired]
public string TeXSource { get; set; }
[Required]
[YaRequired]
public string BaseFileName { get; set; }
[Required]
[YaRequired]
public string DestDir { get; set; }
public bool Generated { get; set; }
public HtmlString GenerationErrorMessage { get; set; }

View file

@ -1,11 +1,12 @@
using System.ComponentModel.DataAnnotations;
using Yavsc.Attributes.Validation;
namespace Yavsc.ViewModels.Manage
{
public class ChangeUserNameViewModel
{
[Required]
[YaRequired]
[Display(Name = "New user name"),RegularExpression(Constants.UserNameRegExp)]
public string NewUserName { get; set; }

View file

@ -6,7 +6,7 @@ namespace Yavsc.ViewModels.Manage
{
public class SetFullNameViewModel
{
[Required]
[YaRequired]
[Display(Name = "Your full name"), YaStringLength(512)]
public string FullName { get; set; }
}