diff --git a/Dimodi.Telerik.Blazor.Templates.csproj b/Dimodi.Telerik.Blazor.Templates.csproj index b3cd23c..4dba10a 100644 --- a/Dimodi.Telerik.Blazor.Templates.csproj +++ b/Dimodi.Telerik.Blazor.Templates.csproj @@ -15,8 +15,9 @@ https://github.com/dimodi/project-templates README.md -NEW -* Add support for Telerik UI for Blazor 14.0.0. +FIXED +* Create a new file instead of appending on first chunk upload. +* Use simpler namespace declaration syntax in controllers. false Telerik;Blazor;dotnet-new;templates @@ -24,7 +25,7 @@ NEW https://github.com/dimodi/project-templates netstandard2.0 Telerik Blazor Project and Item Templates - 6.5.7 + 6.5.8 diff --git a/Items/UploadController/UploadController.cs b/Items/UploadController/UploadController.cs index 09fd474..534975c 100644 --- a/Items/UploadController/UploadController.cs +++ b/Items/UploadController/UploadController.cs @@ -5,128 +5,139 @@ using System.Text; #endif -namespace {TVAR_ROOTNAMESPACE}.Controllers -{ - [Route("{TVAR_ROUTE}")] +namespace {TVAR_ROOTNAMESPACE}.Controllers; + +[Route("{TVAR_ROUTE}")] #if (ApiController) - [ApiController] +[ApiController] #endif - public class UploadController : ControllerBase - { - private IWebHostEnvironment HostingEnvironment { get; set; } +public class UploadController : ControllerBase +{ + private IWebHostEnvironment HostingEnvironment { get; set; } - private readonly string RootPath; + private readonly string RootPath; - public UploadController(IWebHostEnvironment hostingEnvironment) + public UploadController(IWebHostEnvironment hostingEnvironment) + { + HostingEnvironment = hostingEnvironment; + RootPath = HostingEnvironment.WebRootPath; + } + + [HttpPost] + public async Task Save(IFormFile files) + { + if (files is null) { - HostingEnvironment = hostingEnvironment; - RootPath = HostingEnvironment.WebRootPath; + Response.StatusCode = 400; + await Response.WriteAsync("No file to upload."); + return new EmptyResult(); } - [HttpPost] - public async Task Save(IFormFile files) + try { - if (files != null) - { - try - { - string saveLocation = Path.Combine(RootPath, files.FileName); - - using FileStream fs = new(saveLocation, FileMode.Create); - await files.CopyToAsync(fs); - - Response.StatusCode = 201; - } - catch (Exception ex) - { - Response.StatusCode = 500; - await Response.WriteAsync($"Upload failed: {ex.Message}"); - } - } + string saveLocation = Path.Combine(RootPath, files.FileName); - return new EmptyResult(); + using FileStream fs = new(saveLocation, FileMode.Create); + await files.CopyToAsync(fs); + + Response.StatusCode = 201; + } + catch (Exception ex) + { + Response.StatusCode = 500; + await Response.WriteAsync($"Upload failed: {ex.Message}"); } + return new EmptyResult(); + } + #if (ChunkUpload) - [HttpPost] - public async Task SaveChunk(IFormFile files, [FromForm] string chunkMetadata) + [HttpPost] + public async Task SaveChunk(IFormFile files, [FromForm] string chunkMetadata) + { + if (files is null) + { + Response.StatusCode = 400; + await Response.WriteAsync("No file to upload."); + return new EmptyResult(); + } + + try { - if (files != null) + DataContractJsonSerializer dcSerializer = new(typeof(ChunkMetadata)); + using MemoryStream ms = new(Encoding.UTF8.GetBytes(chunkMetadata)); + + if (dcSerializer.ReadObject(ms) is not ChunkMetadata metadata) { - try - { - DataContractJsonSerializer dcSerializer = new(typeof(ChunkMetadata)); - MemoryStream ms = new(Encoding.UTF8.GetBytes(chunkMetadata)); - - if (dcSerializer.ReadObject(ms) is not ChunkMetadata metadata) - { - throw new NullReferenceException("Chunk metadata serialization failed."); - } - - string saveLocation = Path.Combine(RootPath, metadata.FileName); - - using FileStream fs = new(saveLocation, FileMode.Append); - await files.CopyToAsync(fs); - - Response.StatusCode = 201; - } - catch (Exception ex) - { - Response.StatusCode = 500; - await Response.WriteAsync($"Upload failed: {ex.Message}"); - } + throw new NullReferenceException("Chunk metadata serialization failed."); } - return new EmptyResult(); + string saveLocation = Path.Combine(RootPath, metadata.FileName); + + using FileStream fs = new(saveLocation, metadata.ChunkIndex == 0 ? FileMode.Create : FileMode.Append); + await files.CopyToAsync(fs); + + Response.StatusCode = 201; + } + catch (Exception ex) + { + Response.StatusCode = 500; + await Response.WriteAsync($"Upload failed: {ex.Message}"); } + return new EmptyResult(); + } + #endif - [HttpPost] - public async Task Remove([FromForm] string files) + [HttpPost] + public async Task Remove([FromForm] string files) + { + if (string.IsNullOrEmpty(files?.Trim())) + { + Response.StatusCode = 400; + await Response.WriteAsync("No file to delete."); + return new EmptyResult(); + } + + try { - if (files != null) + string fileLocation = Path.Combine(RootPath, files); + + if (System.IO.File.Exists(fileLocation)) { - try - { - string fileLocation = Path.Combine(RootPath, files); - - if (System.IO.File.Exists(fileLocation)) - { - System.IO.File.Delete(fileLocation); - } - } - catch (Exception ex) - { - Response.StatusCode = 500; - await Response.WriteAsync($"Delete failed: {ex.Message}"); - } + System.IO.File.Delete(fileLocation); } - - return new EmptyResult(); } + catch (Exception ex) + { + Response.StatusCode = 500; + await Response.WriteAsync($"Delete failed: {ex.Message}"); + } + + return new EmptyResult(); } +} #if (ChunkUpload) - [DataContract] - public class ChunkMetadata - { - [DataMember(Name = "fileId")] - public string FileId { get; set; } = string.Empty; +[DataContract] +public class ChunkMetadata +{ + [DataMember(Name = "fileId")] + public string FileId { get; set; } = string.Empty; - [DataMember(Name = "fileName")] - public string FileName { get; set; } = string.Empty; + [DataMember(Name = "fileName")] + public string FileName { get; set; } = string.Empty; - [DataMember(Name = "fileSize")] - public long FileSize { get; set; } + [DataMember(Name = "fileSize")] + public long FileSize { get; set; } - [DataMember(Name = "contentType")] - public string ContentType { get; set; } = string.Empty; + [DataMember(Name = "contentType")] + public string ContentType { get; set; } = string.Empty; - [DataMember(Name = "chunkIndex")] - public long ChunkIndex { get; set; } + [DataMember(Name = "chunkIndex")] + public long ChunkIndex { get; set; } - [DataMember(Name = "totalChunks")] - public long TotalChunks { get; set; } - } + [DataMember(Name = "totalChunks")] + public long TotalChunks { get; set; } +} #endif -} \ No newline at end of file diff --git a/Projects/Net8/TelerikBlazorWebApp/TelerikBlazorWebApp/Controllers/CultureController.cs b/Projects/Net8/TelerikBlazorWebApp/TelerikBlazorWebApp/Controllers/CultureController.cs index d23e8e8..f0205a9 100644 --- a/Projects/Net8/TelerikBlazorWebApp/TelerikBlazorWebApp/Controllers/CultureController.cs +++ b/Projects/Net8/TelerikBlazorWebApp/TelerikBlazorWebApp/Controllers/CultureController.cs @@ -3,23 +3,22 @@ // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 -namespace TelerikBlazorWebApp.Controllers +namespace TelerikBlazorWebApp.Controllers; + +[Route("[controller]/[action]")] +public class CultureController : Controller { - [Route("[controller]/[action]")] - public class CultureController : Controller + public IActionResult Set(string culture, string redirectUri) { - public IActionResult Set(string culture, string redirectUri) + if (culture != null) { - if (culture != null) - { - HttpContext.Response.Cookies.Append( - CookieRequestCultureProvider.DefaultCookieName, - CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture, culture)), - new CookieOptions() { Expires = new DateTimeOffset(DateTime.Now.AddDays(30)) } - ); - } - - return LocalRedirect(redirectUri); + HttpContext.Response.Cookies.Append( + CookieRequestCultureProvider.DefaultCookieName, + CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture, culture)), + new CookieOptions() { Expires = new DateTimeOffset(DateTime.Now.AddDays(30)) } + ); } + + return LocalRedirect(redirectUri); } } \ No newline at end of file diff --git a/Projects/Net8/TelerikBlazorWebApp/TelerikBlazorWebApp/Controllers/UploadController.cs b/Projects/Net8/TelerikBlazorWebApp/TelerikBlazorWebApp/Controllers/UploadController.cs index d94e8ab..2b5aaac 100644 --- a/Projects/Net8/TelerikBlazorWebApp/TelerikBlazorWebApp/Controllers/UploadController.cs +++ b/Projects/Net8/TelerikBlazorWebApp/TelerikBlazorWebApp/Controllers/UploadController.cs @@ -3,122 +3,133 @@ using System.Runtime.Serialization.Json; using System.Text; -namespace TelerikBlazorWebApp.Controllers +namespace TelerikBlazorWebApp.Controllers; + +[Route("api/[controller]/[action]")] +[ApiController] +public class UploadController : ControllerBase { - [Route("api/[controller]/[action]")] - [ApiController] - public class UploadController : ControllerBase - { - private IWebHostEnvironment HostingEnvironment { get; set; } + private IWebHostEnvironment HostingEnvironment { get; set; } + + private readonly string RootPath; - private readonly string RootPath; + public UploadController(IWebHostEnvironment hostingEnvironment) + { + HostingEnvironment = hostingEnvironment; + RootPath = HostingEnvironment.WebRootPath; + } - public UploadController(IWebHostEnvironment hostingEnvironment) + [HttpPost] + public async Task Save(IFormFile files) + { + if (files is null) { - HostingEnvironment = hostingEnvironment; - RootPath = HostingEnvironment.WebRootPath; + Response.StatusCode = 400; + await Response.WriteAsync("No file to upload."); + return new EmptyResult(); } - [HttpPost] - public async Task Save(IFormFile files) + try { - if (files != null) - { - try - { - string saveLocation = Path.Combine(RootPath, files.FileName); - - using FileStream fs = new(saveLocation, FileMode.Create); - await files.CopyToAsync(fs); - - Response.StatusCode = 201; - } - catch (Exception ex) - { - Response.StatusCode = 500; - await Response.WriteAsync($"Upload failed: {ex.Message}"); - } - } + string saveLocation = Path.Combine(RootPath, files.FileName); + + using FileStream fs = new(saveLocation, FileMode.Create); + await files.CopyToAsync(fs); + Response.StatusCode = 201; + } + catch (Exception ex) + { + Response.StatusCode = 500; + await Response.WriteAsync($"Upload failed: {ex.Message}"); + } + + return new EmptyResult(); + } + + [HttpPost] + public async Task SaveChunk(IFormFile files, [FromForm] string chunkMetadata) + { + if (files is null) + { + Response.StatusCode = 400; + await Response.WriteAsync("No file to upload."); return new EmptyResult(); } - [HttpPost] - public async Task SaveChunk(IFormFile files, [FromForm] string chunkMetadata) + try { - if (files != null) + DataContractJsonSerializer dcSerializer = new(typeof(ChunkMetadata)); + using MemoryStream ms = new(Encoding.UTF8.GetBytes(chunkMetadata)); + + if (dcSerializer.ReadObject(ms) is not ChunkMetadata metadata) { - try - { - DataContractJsonSerializer dcSerializer = new(typeof(ChunkMetadata)); - MemoryStream ms = new(Encoding.UTF8.GetBytes(chunkMetadata)); - - if (dcSerializer.ReadObject(ms) is not ChunkMetadata metadata) - { - throw new NullReferenceException("Chunk metadata serialization failed."); - } - - string saveLocation = Path.Combine(RootPath, metadata.FileName); - - using FileStream fs = new(saveLocation, FileMode.Append); - await files.CopyToAsync(fs); - - Response.StatusCode = 201; - } - catch (Exception ex) - { - Response.StatusCode = 500; - await Response.WriteAsync($"Upload failed: {ex.Message}"); - } + throw new NullReferenceException("Chunk metadata serialization failed."); } + string saveLocation = Path.Combine(RootPath, metadata.FileName); + + using FileStream fs = new(saveLocation, metadata.ChunkIndex == 0 ? FileMode.Create : FileMode.Append); + await files.CopyToAsync(fs); + + Response.StatusCode = 201; + } + catch (Exception ex) + { + Response.StatusCode = 500; + await Response.WriteAsync($"Upload failed: {ex.Message}"); + } + + return new EmptyResult(); + } + + [HttpPost] + public async Task Remove([FromForm] string files) + { + if (string.IsNullOrEmpty(files?.Trim())) + { + Response.StatusCode = 400; + await Response.WriteAsync("No file to delete."); return new EmptyResult(); } - [HttpPost] - public async Task Remove([FromForm] string files) + try { - if (files != null) + string fileLocation = Path.Combine(RootPath, files); + + if (System.IO.File.Exists(fileLocation)) { - try - { - string fileLocation = Path.Combine(RootPath, files); - - if (System.IO.File.Exists(fileLocation)) - { - System.IO.File.Delete(fileLocation); - } - } - catch (Exception ex) - { - Response.StatusCode = 500; - await Response.WriteAsync($"Delete failed: {ex.Message}"); - } + System.IO.File.Delete(fileLocation); } - - return new EmptyResult(); } + catch (Exception ex) + { + Response.StatusCode = 500; + await Response.WriteAsync($"Delete failed: {ex.Message}"); + } + + return new EmptyResult(); } +} - [DataContract] - public class ChunkMetadata - { - [DataMember(Name = "fileId")] - public string FileId { get; set; } = string.Empty; +[DataContract] +public class ChunkMetadata +{ + [DataMember(Name = "fileId")] + public string FileId { get; set; } = string.Empty; - [DataMember(Name = "fileName")] - public string FileName { get; set; } = string.Empty; + [DataMember(Name = "fileName")] + public string FileName { get; set; } = string.Empty; - [DataMember(Name = "fileSize")] - public long FileSize { get; set; } + [DataMember(Name = "fileSize")] + public long FileSize { get; set; } - [DataMember(Name = "contentType")] - public string ContentType { get; set; } = string.Empty; + [DataMember(Name = "contentType")] + public string ContentType { get; set; } = string.Empty; - [DataMember(Name = "chunkIndex")] - public long ChunkIndex { get; set; } + [DataMember(Name = "chunkIndex")] + public long ChunkIndex { get; set; } - [DataMember(Name = "totalChunks")] - public long TotalChunks { get; set; } - } -} \ No newline at end of file + [DataMember(Name = "totalChunks")] + public long TotalChunks { get; set; } +}