From 1a6789ea2ce933e7f1a3495b05aa27a0bfa84ec6 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Tue, 1 Sep 2020 19:49:38 +0430 Subject: [PATCH] Audio Narrations: basic codes for uploading files --- .../Controllers/AudioNarrationController.cs | 8 ++- .../Models/UploadSession/UploadSessionFile.cs | 10 +++ RMuseum/RMuseum.xml | 24 +++++++ RMuseum/Services/IAudioNarrationService.cs | 10 ++- .../Implementation/AudioNarrationService.cs | 64 ++++++++++++++++++- RMuseum/appsettings.json | 3 + 6 files changed, 116 insertions(+), 3 deletions(-) diff --git a/RMuseum/Controllers/AudioNarrationController.cs b/RMuseum/Controllers/AudioNarrationController.cs index c7e0b35c..364c628a 100644 --- a/RMuseum/Controllers/AudioNarrationController.cs +++ b/RMuseum/Controllers/AudioNarrationController.cs @@ -86,9 +86,15 @@ namespace RMuseum.Controllers RServiceResult resSession = await _audioService.InitiateNewUploadSession(loggedOnUserId); if (!string.IsNullOrEmpty(resSession.ExceptionString)) return BadRequest(resSession.ExceptionString); + List files = new List(); foreach(IFormFile file in Request.Form.Files) { - + RServiceResult rsFileResult = await _audioService.SaveUploadedFile(file); + if(!string.IsNullOrEmpty(rsFileResult.ExceptionString)) + { + return BadRequest(rsFileResult.ExceptionString); + } + files.Add(rsFileResult.Result); } return Ok(await _audioService.GetUploadSession(resSession.Result.Id)); } diff --git a/RMuseum/Models/UploadSession/UploadSessionFile.cs b/RMuseum/Models/UploadSession/UploadSessionFile.cs index 18dfd1a4..ec364429 100644 --- a/RMuseum/Models/UploadSession/UploadSessionFile.cs +++ b/RMuseum/Models/UploadSession/UploadSessionFile.cs @@ -43,6 +43,16 @@ namespace RMuseum.Models.UploadSession /// public string FilePath { get; set; } + + /// + /// Process Result + /// + public bool ProcessResult { get; set; } + + /// + /// Process Result Message + /// + public string ProcessResultMsg { get; set; } } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index b4268be1..02274623 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -2809,6 +2809,16 @@ File Path + + + Process Result + + + + + Process Result Message + + Upload Session Process Status @@ -3387,6 +3397,13 @@ + + + Save uploaded file + + + + Get Upload Session (including files) @@ -4129,6 +4146,13 @@ + + + Save uploaded file + + + + Get Upload Session (including files) diff --git a/RMuseum/Services/IAudioNarrationService.cs b/RMuseum/Services/IAudioNarrationService.cs index 954ef2ac..8f250c5e 100644 --- a/RMuseum/Services/IAudioNarrationService.cs +++ b/RMuseum/Services/IAudioNarrationService.cs @@ -1,4 +1,5 @@ -using RMuseum.Models.GanjoorAudio; +using Microsoft.AspNetCore.Http; +using RMuseum.Models.GanjoorAudio; using RMuseum.Models.GanjoorAudio.ViewModels; using RMuseum.Models.UploadSession; using RSecurityBackend.Models.Generic; @@ -35,6 +36,13 @@ namespace RMuseum.Services /// Task> InitiateNewUploadSession(Guid userId); + /// + /// Save uploaded file + /// + /// + /// + Task> SaveUploadedFile(IFormFile uploadedFile); + /// /// Get Upload Session (including files) /// diff --git a/RMuseum/Services/Implementation/AudioNarrationService.cs b/RMuseum/Services/Implementation/AudioNarrationService.cs index e62ac1d1..27958368 100644 --- a/RMuseum/Services/Implementation/AudioNarrationService.cs +++ b/RMuseum/Services/Implementation/AudioNarrationService.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MySql.Data.MySqlClient; using RMuseum.DbContext; @@ -166,6 +167,67 @@ namespace RMuseum.Services.Implementation } } + /// + /// Save uploaded file + /// + /// + /// + public async Task> SaveUploadedFile(IFormFile uploadedFile) + { + try + { + UploadSessionFile file = new UploadSessionFile() + { + ContentDisposition = uploadedFile.ContentDisposition, + ContentType = uploadedFile.ContentType, + FileName = uploadedFile.FileName, + Length = uploadedFile.Length, + Name = uploadedFile.Name, + ProcessResult = true, + ProcessResultMsg = "" + }; + + string ext = Path.GetExtension(file.FileName).ToLower(); + if(ext != ".mp3" && ext != ".xml" && ext != ".zip") + { + file.ProcessResult = false; + file.ProcessResultMsg = "تنها فایلهای با پسوند mp3، xml و zip قابل قبول هستند."; + } + else + { + if (!Directory.Exists(Configuration.GetSection("AudioUploadService")["StoragePath"])) + { + try + { + Directory.CreateDirectory(Configuration.GetSection("AudioUploadService")["StoragePath"]); + } + catch + { + return new RServiceResult(null, $"ProcessImage: create dir failed {Configuration.GetSection("AudioUploadService")["StoragePath"]}"); + } + } + + string filePath = Path.Combine(Configuration.GetSection("AudioUploadService")["StoragePath"], file.FileName); + while(File.Exists(filePath)) + { + filePath = Path.Combine(Configuration.GetSection("AudioUploadService")["StoragePath"], Guid.NewGuid().ToString() + ext); + } + using (FileStream fsMain = new FileStream(filePath, FileMode.Create)) + { + await uploadedFile.CopyToAsync(fsMain); + } + file.FilePath = filePath; + } + + return new RServiceResult(file); + + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// Get Upload Session (including files) /// diff --git a/RMuseum/appsettings.json b/RMuseum/appsettings.json index 4d648158..a591e796 100644 --- a/RMuseum/appsettings.json +++ b/RMuseum/appsettings.json @@ -39,5 +39,8 @@ "Password": "", "Database": "adab" }, + "AudioUploadService": { + "StoragePath": "C:\\audio" + }, "AllowedHosts": "*" }