diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-12-30 20:22:55 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-12-30 20:22:55 +0100 |
| commit | 3c188c2949af9d0e08db74a850f8a87dad52de49 (patch) | |
| tree | 5dba85786aa9a5a72d91328092a017f16318b8c0 /code/api/Endpoints/Storage | |
| parent | f5374b0b3543b0bd7d280f71ed8bf5175bad3834 (diff) | |
| download | storage-3c188c2949af9d0e08db74a850f8a87dad52de49.tar.xz storage-3c188c2949af9d0e08db74a850f8a87dad52de49.zip | |
feat: Yes
Diffstat (limited to 'code/api/Endpoints/Storage')
10 files changed, 135 insertions, 2 deletions
diff --git a/code/api/Endpoints/Storage/Files/CreateEndpoint.cs b/code/api/Endpoints/Storage/Files/CreateEndpoint.cs new file mode 100644 index 0000000..c53a8e3 --- /dev/null +++ b/code/api/Endpoints/Storage/Files/CreateEndpoint.cs @@ -0,0 +1,6 @@ +namespace I2R.Storage.Api.Endpoints.Storage.Files; + +public class CreateEndpoint +{ + +}
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/Files/DeleteEndpoint.cs b/code/api/Endpoints/Storage/Files/DeleteEndpoint.cs new file mode 100644 index 0000000..ab95d94 --- /dev/null +++ b/code/api/Endpoints/Storage/Files/DeleteEndpoint.cs @@ -0,0 +1,6 @@ +namespace I2R.Storage.Api.Endpoints.Storage.Files; + +public class DeleteEndpoint +{ + +}
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/Files/PutEndpoint.cs b/code/api/Endpoints/Storage/Files/PutEndpoint.cs new file mode 100644 index 0000000..129eeed --- /dev/null +++ b/code/api/Endpoints/Storage/Files/PutEndpoint.cs @@ -0,0 +1,6 @@ +namespace I2R.Storage.Api.Endpoints.Storage.Files; + +public class PutEndpoint +{ + +}
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/Folders/CreateEndpoint.cs b/code/api/Endpoints/Storage/Folders/CreateEndpoint.cs new file mode 100644 index 0000000..2eb1352 --- /dev/null +++ b/code/api/Endpoints/Storage/Folders/CreateEndpoint.cs @@ -0,0 +1,6 @@ +namespace I2R.Storage.Api.Endpoints.Storage.Folders; + +public class CreateEndpoint +{ + +}
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/Folders/DeleteEndpoint.cs b/code/api/Endpoints/Storage/Folders/DeleteEndpoint.cs new file mode 100644 index 0000000..73fa907 --- /dev/null +++ b/code/api/Endpoints/Storage/Folders/DeleteEndpoint.cs @@ -0,0 +1,6 @@ +namespace I2R.Storage.Api.Endpoints.Storage.Folders; + +public class DeleteEndpoint +{ + +}
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/Shares/CreateShareEndpoint.cs b/code/api/Endpoints/Storage/Shares/CreateShareEndpoint.cs new file mode 100644 index 0000000..dd049e7 --- /dev/null +++ b/code/api/Endpoints/Storage/Shares/CreateShareEndpoint.cs @@ -0,0 +1,6 @@ +namespace I2R.Storage.Api.Endpoints.Storage.Shares; + +public class CreateShareEndpoint +{ + +}
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/Shares/DeleteShareEndpoint.cs b/code/api/Endpoints/Storage/Shares/DeleteShareEndpoint.cs new file mode 100644 index 0000000..ad85887 --- /dev/null +++ b/code/api/Endpoints/Storage/Shares/DeleteShareEndpoint.cs @@ -0,0 +1,6 @@ +namespace I2R.Storage.Api.Endpoints.Storage.Shares; + +public class DeleteShareEndpoint +{ + +}
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/Shares/EditShareEndpoint.cs b/code/api/Endpoints/Storage/Shares/EditShareEndpoint.cs new file mode 100644 index 0000000..30e4bf7 --- /dev/null +++ b/code/api/Endpoints/Storage/Shares/EditShareEndpoint.cs @@ -0,0 +1,6 @@ +namespace I2R.Storage.Api.Endpoints.Storage.Shares; + +public class EditShareEndpoint +{ + +}
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/TreeEndpoint.cs b/code/api/Endpoints/Storage/TreeEndpoint.cs index 857a570..f832034 100644 --- a/code/api/Endpoints/Storage/TreeEndpoint.cs +++ b/code/api/Endpoints/Storage/TreeEndpoint.cs @@ -2,8 +2,32 @@ namespace I2R.Storage.Api.Endpoints.Storage; public class TreeEndpoint : EndpointBase { + private readonly AppDatabase _database; + private readonly IPaginationService _pagination; + + public TreeEndpoint(AppDatabase database, IPaginationService pagination) { + _database = database; + _pagination = pagination; + } + [HttpGet("~/storage/tree")] - public async Task<ActionResult> Handle(Guid parent = default) { - return Ok(); + public async Task<ActionResult<KeysetPaginationResult<FileSystemEntry>>> Handle(Guid parent = default) { + return Ok(await _pagination.KeysetPaginateAsync( + _database.Folders.Include(c => c.Files).ConditionalWhere(() => parent != default, folder => folder.ParentId == parent), + b => b.Descending(a => a.Name), + async id => await _database.Folders.FirstOrDefaultAsync(c => c.Id == id.AsGuid()), + query => query.Select(p => new FileSystemEntry() { + Id = p.Id, + Name = p.Name, + MimeType = SystemConstants.FolderMimeType, + SizeInBytes = -1, + Files = p.Files.Select(c => new FileSystemEntry() { + Id = c.Id, + Name = c.Name, + MimeType = c.MimeType, + SizeInBytes = c.SizeInBytes + }).ToList() + }) + )); } }
\ No newline at end of file diff --git a/code/api/Endpoints/Storage/UploadEndpoint.cs b/code/api/Endpoints/Storage/UploadEndpoint.cs new file mode 100644 index 0000000..e3feffb --- /dev/null +++ b/code/api/Endpoints/Storage/UploadEndpoint.cs @@ -0,0 +1,61 @@ +using File = I2R.Storage.Api.Database.Models.File; + +namespace I2R.Storage.Api.Endpoints.Storage; + +public class UploadEndpoint : EndpointBase +{ + private readonly DefaultResourceService _resourceService; + private readonly IStringLocalizer<SharedResources> _localizer; + private readonly AppDatabase _database; + private readonly StorageService _storageService; + + public UploadEndpoint(DefaultResourceService resourceService, IStringLocalizer<SharedResources> localizer, AppDatabase database, StorageService storageService) { + _resourceService = resourceService; + _localizer = localizer; + _database = database; + _storageService = storageService; + } + + public class Request + { + public IFormFileCollection FormFileCollection { get; set; } + public Guid FolderId { get; set; } + } + + [HttpPost("~/storage/upload")] + public async Task<ActionResult> Handle(Request request) { + var folder = await _storageService.GetFileSystemEntryAsync(request.FolderId); + if (folder == default) { + return NotFound(); + } + + var problem = new KnownProblemModel(); + foreach (var formFile in request.FormFileCollection) { + if (!formFile.FileName.IsValidFileName()) { + problem.AddError("file_" + formFile.Name, _localizer["{fileName} is an invalid file name"]); + continue; + } + + if (problem.Errors.Any()) { + return KnownProblem(problem); + } + + var file = new File(LoggedInUser.Id) { + Name = formFile.FileName, + FolderId = folder.Id, + MimeType = formFile.ContentType, + SizeInBytes = formFile.Length, + OwningUserId = LoggedInUser.Id + }; + var id = new StorageBlobId() { + Id = file.Id, + Bucket = LoggedInUser.Id + }; + await _resourceService.SetBlobAsync(id, formFile.OpenReadStream()); + await _database.Files.AddAsync(file); + await _database.SaveChangesAsync(); + } + + return Ok(await _storageService.GetFileSystemEntryAsync(folder.Id)); + } +}
\ No newline at end of file |
