Skip to content
Merged

ehehe #386

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Application/Documents/Commands/UploadFileToDocument.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Application.Common.Exceptions;
using Application.Common.Extensions;
using Application.Common.Interfaces;
using Application.Common.Models.Dtos.Physical;
using AutoMapper;
using Domain.Entities;
using Domain.Entities.Digital;
using Domain.Entities.Physical;
using Domain.Statuses;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -62,6 +64,11 @@ public async Task<DocumentDto> Handle(Command request, CancellationToken cancell
_context.Files.Remove(document.File);
}

if (document.Status is DocumentStatus.Issued or DocumentStatus.Lost )
{
throw new ConflictException("This document cannot be uploaded to.");
}

var file = new FileEntity
{
FileData = request.FileData.ToArray(),
Expand Down
25 changes: 21 additions & 4 deletions src/Application/Entries/Commands/CreateEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,33 @@ namespace Application.Entries.Commands;
public class CreateEntry {
public class Validator : AbstractValidator<Command>
{
private readonly string[] _allowedExtensions =
{
"pdf",
"doc",
"docx",
"xls",
"xlsx",
"ppt",
"pptx",
};
public Validator()
{
RuleLevelCascadeMode = CascadeMode.Stop;

RuleFor(x => x.FileExtension)
.Must(extension =>
{
if (!_allowedExtensions.Contains(extension))
{
throw new ConflictException("This file is not supported.");
}
return true;
});

RuleFor(x => x.Name)
.NotEmpty().WithMessage("Entry's name is required.")
.Matches("^[\\p{L}A-Za-z_.\\s\\-0-9]*$").WithMessage("Invalid name format.")
.Matches("^[\\p{L}A-Za-z()_.\\s\\-0-9]*$").WithMessage("Invalid name format.")
.MaximumLength(256).WithMessage("Name cannot exceed 256 characters.");

RuleFor(x => x.Path)
Expand Down Expand Up @@ -104,9 +124,6 @@ public async Task<EntryDto> Handle(Command request, CancellationToken cancellati
throw new ConflictException("File size must be lower than 20MB");
}

var lastDotIndex = request.Name.LastIndexOf(".", StringComparison.Ordinal);
var fileExtension = request.Name.Substring(lastDotIndex + 1, request.Name.Length - lastDotIndex - 1);

var fileEntity = new FileEntity()
{
FileData = request.FileData.ToArray(),
Expand Down
1 change: 1 addition & 0 deletions src/Application/Entries/Queries/GetAllEntriesPaginated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public QueryHandler(IApplicationDbContext context, IMapper mapper)
public async Task<PaginatedList<EntryDto>> Handle(Query request, CancellationToken cancellationToken)
{
var entries = _context.Entries
.Include(x => x.File)
.Where(x => x.Path.Equals(request.EntryPath) &&
x.OwnerId == request.CurrentUser.Id);

Expand Down