From 89d7d4592e0b0c4066a88be627fd58dc5376536f Mon Sep 17 00:00:00 2001 From: Sakurai Date: Sun, 24 Mar 2024 14:17:48 +0900 Subject: [PATCH] Add: Test-Client --- Controllers/FileController.cs | 40 ++++++++++ IO/StorageProvider.cs | 24 ++++-- Models/FileModel.cs | 1 + Models/Request/FileDeleteModel.cs | 8 ++ StorageServer.csproj | 2 +- StorageServer.sln | 25 +++++++ test-client/index.html | 119 ++++++++++++++++++++++++++++++ 7 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 Models/Request/FileDeleteModel.cs create mode 100644 StorageServer.sln create mode 100644 test-client/index.html diff --git a/Controllers/FileController.cs b/Controllers/FileController.cs index e6ce3b5..6a93ce8 100644 --- a/Controllers/FileController.cs +++ b/Controllers/FileController.cs @@ -62,6 +62,24 @@ public class FileController : Controller } } + [HttpGet] + public IActionResult ReadInfo(FileReadModel model) { + if (!ModelState.IsValid) { + return BadRequest(); + } + + try { + FileModel fileModel = this.database.Read(model.FileID); + if (fileModel is null) { + return NotFound(); + } + + return Json(fileModel); + } catch (Exception) { + return Problem(); + } + } + [HttpGet] public IActionResult Read(FileReadModel model) { if (!ModelState.IsValid) { @@ -84,4 +102,26 @@ public class FileController : Controller return Problem(); } } + + [HttpPost] + public IActionResult Delete(FileDeleteModel model) { + if (!ModelState.IsValid) { + return BadRequest(); + } + + try { + FileModel fileModel = this.database.Read(model.FileID); + if (fileModel is null) { + return NotFound(); + } + + this.provider.DeleteFile(fileModel); + + return Ok(); + } catch (FileNotFoundException) { + return NotFound(); + } catch (Exception) { + return Problem(); + } + } } diff --git a/IO/StorageProvider.cs b/IO/StorageProvider.cs index 0d0afee..9996a3d 100644 --- a/IO/StorageProvider.cs +++ b/IO/StorageProvider.cs @@ -23,22 +23,34 @@ public class StorageProvider { } public async Task SaveFile(FileModel fileModel, FileStream fs) { - string extension = Path.GetExtension(fileModel.FileName); - string saveFileName = string.Format("{0}{1}", fileModel.FileID, extension); - string saveFullName = Path.Combine(this.saveDirectory, saveFileName); + string saveFullName = GetFullPath(fileModel); using (FileStream dest = new FileStream(saveFullName, FileMode.OpenOrCreate)) { await fs.CopyToAsync(dest); } } public Stream GetFile(FileModel fileModel) { - string extension = Path.GetExtension(fileModel.FileName); - string saveFileName = string.Format("{0}{1}", fileModel.FileID, extension); - string saveFullName = Path.Combine(this.saveDirectory, saveFileName); + string saveFullName = GetFullPath(fileModel); if (!File.Exists(saveFullName)) { throw new FileNotFoundException(); } return new FileStream(saveFullName, FileMode.Open); } + + public void DeleteFile(FileModel fileModel) { + string saveFullName = GetFullPath(fileModel); + if (!File.Exists(saveFullName)) { + throw new FileNotFoundException(); + } + + File.Delete(saveFullName); + } + + public string GetFullPath(FileModel fileModel) { + string extension = Path.GetExtension(fileModel.FileName); + string saveFileName = string.Format("{0}{1}", fileModel.FileID, extension); + string saveFullName = Path.Combine(this.saveDirectory, saveFileName); + return saveFullName; + } } diff --git a/Models/FileModel.cs b/Models/FileModel.cs index 231ec2a..c8a16c9 100644 --- a/Models/FileModel.cs +++ b/Models/FileModel.cs @@ -10,4 +10,5 @@ public class FileModel { public string MimeType { get; set; } public string HashValue { get; set; } public DateTime CreatedAt { get; set; } + public bool IsDeleted { get; set; } = false; } diff --git a/Models/Request/FileDeleteModel.cs b/Models/Request/FileDeleteModel.cs new file mode 100644 index 0000000..0931116 --- /dev/null +++ b/Models/Request/FileDeleteModel.cs @@ -0,0 +1,8 @@ +using System.ComponentModel.DataAnnotations; + +namespace StorageServer.Models.Request; + +public class FileDeleteModel { + [Required] + public long FileID { get; set; } +} diff --git a/StorageServer.csproj b/StorageServer.csproj index 42f17f4..e6f86bc 100644 --- a/StorageServer.csproj +++ b/StorageServer.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 disable enable diff --git a/StorageServer.sln b/StorageServer.sln new file mode 100644 index 0000000..95892e2 --- /dev/null +++ b/StorageServer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorageServer", "StorageServer.csproj", "{4BDA80C2-0B1E-4B29-993A-65572587066E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4BDA80C2-0B1E-4B29-993A-65572587066E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4BDA80C2-0B1E-4B29-993A-65572587066E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4BDA80C2-0B1E-4B29-993A-65572587066E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4BDA80C2-0B1E-4B29-993A-65572587066E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F6E3DEC7-59C2-46F8-949E-F523A72DE0AE} + EndGlobalSection +EndGlobal diff --git a/test-client/index.html b/test-client/index.html new file mode 100644 index 0000000..72c2f2f --- /dev/null +++ b/test-client/index.html @@ -0,0 +1,119 @@ + + + + + + StorageServer + + + +
+
+
+
+
+ +
+
+ +
+
+
+ +
+
+
+
+
+
+ +
+
+
    +
    +
    +
    +
    + + +