Merge pull request #9 from ganjoor/audioman
Audio Narrations: basic codes for uploading files
This commit is contained in:
commit
e0cd4e097a
@ -86,9 +86,15 @@ namespace RMuseum.Controllers
|
||||
RServiceResult<UploadSession> resSession = await _audioService.InitiateNewUploadSession(loggedOnUserId);
|
||||
if (!string.IsNullOrEmpty(resSession.ExceptionString))
|
||||
return BadRequest(resSession.ExceptionString);
|
||||
List<UploadSessionFile> files = new List<UploadSessionFile>();
|
||||
foreach(IFormFile file in Request.Form.Files)
|
||||
{
|
||||
|
||||
RServiceResult<UploadSessionFile> 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));
|
||||
}
|
||||
|
||||
@ -43,6 +43,16 @@ namespace RMuseum.Models.UploadSession
|
||||
/// </summary>
|
||||
public string FilePath { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Process Result
|
||||
/// </summary>
|
||||
public bool ProcessResult { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Process Result Message
|
||||
/// </summary>
|
||||
public string ProcessResultMsg { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -2809,6 +2809,16 @@
|
||||
File Path
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:RMuseum.Models.UploadSession.UploadSessionFile.ProcessResult">
|
||||
<summary>
|
||||
Process Result
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:RMuseum.Models.UploadSession.UploadSessionFile.ProcessResultMsg">
|
||||
<summary>
|
||||
Process Result Message
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:RMuseum.Models.UploadSession.UploadSessionProcessStatus">
|
||||
<summary>
|
||||
Upload Session Process Status
|
||||
@ -3387,6 +3397,13 @@
|
||||
<param name="userId"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IAudioNarrationService.SaveUploadedFile(Microsoft.AspNetCore.Http.IFormFile)">
|
||||
<summary>
|
||||
Save uploaded file
|
||||
</summary>
|
||||
<param name="uploadedFile"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.IAudioNarrationService.GetUploadSession(System.Guid)">
|
||||
<summary>
|
||||
Get Upload Session (including files)
|
||||
@ -4129,6 +4146,13 @@
|
||||
<param name="userId"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.AudioNarrationService.SaveUploadedFile(Microsoft.AspNetCore.Http.IFormFile)">
|
||||
<summary>
|
||||
Save uploaded file
|
||||
</summary>
|
||||
<param name="uploadedFile"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.AudioNarrationService.GetUploadSession(System.Guid)">
|
||||
<summary>
|
||||
Get Upload Session (including files)
|
||||
|
||||
@ -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
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<UploadSession>> InitiateNewUploadSession(Guid userId);
|
||||
|
||||
/// <summary>
|
||||
/// Save uploaded file
|
||||
/// </summary>
|
||||
/// <param name="uploadedFile"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<UploadSessionFile>> SaveUploadedFile(IFormFile uploadedFile);
|
||||
|
||||
/// <summary>
|
||||
/// Get Upload Session (including files)
|
||||
/// </summary>
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save uploaded file
|
||||
/// </summary>
|
||||
/// <param name="uploadedFile"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<UploadSessionFile>> 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<UploadSessionFile>(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<UploadSessionFile>(file);
|
||||
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
return new RServiceResult<UploadSessionFile>(null, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Upload Session (including files)
|
||||
/// </summary>
|
||||
|
||||
@ -39,5 +39,8 @@
|
||||
"Password": "",
|
||||
"Database": "adab"
|
||||
},
|
||||
"AudioUploadService": {
|
||||
"StoragePath": "C:\\audio"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user