#26 API methods to download mp3 and xml files

This commit is contained in:
Hamid Reza Mohammadi 2020-11-04 14:56:59 +03:30
parent 2fa2777acf
commit 6dc209a0b1
2 changed files with 95 additions and 0 deletions

View File

@ -12,6 +12,7 @@ using RSecurityBackend.Models.Generic;
using RSecurityBackend.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
@ -69,6 +70,78 @@ namespace RMuseum.Controllers
return Ok(res.Result.Items);
}
/// <summary>
/// get the corresponding mp3 file for the narration
/// </summary>
/// <remarks>
/// it could be protected (Authorized), but I guess I would have problems with available client components support,
/// so I preferred it to be anonymous, as it does not harm anybody I guess
/// </remarks>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("file/{id}.mp3")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(FileStreamResult))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> GetMp3File(int id)
{
RServiceResult<PoemNarrationViewModel> narration =
await _audioService.Get(id);
if (!string.IsNullOrEmpty(narration.ExceptionString))
return BadRequest(narration.ExceptionString);
if (narration.Result == null)
return NotFound();
Response.GetTypedHeaders().LastModified = narration.Result.UploadDate;//TODO: Add a FileLastUpdated field to narrations to indicate the last time the mp3/xml files have been updated
var requestHeaders = Request.GetTypedHeaders();
if (requestHeaders.IfModifiedSince.HasValue &&
requestHeaders.IfModifiedSince.Value >= narration.Result.UploadDate)
{
return StatusCode(StatusCodes.Status304NotModified);
}
return new FileStreamResult(new FileStream(narration.Result.LocalMp3FilePath, FileMode.Open, FileAccess.Read), "audio/mpeg");
}
/// <summary>
/// get the corresponding xml file for the narration
/// </summary>
/// <remarks>
/// it could be protected (Authorized), but I guess I would have problems with available client components support,
/// so I preferred it to be anonymous, as it does not harm anybody I guess
/// </remarks>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("file/{id}.xml")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(FileStreamResult))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> GetXMLFile(int id)
{
RServiceResult<PoemNarrationViewModel> narration =
await _audioService.Get(id);
if (!string.IsNullOrEmpty(narration.ExceptionString))
return BadRequest(narration.ExceptionString);
if (narration.Result == null)
return NotFound();
Response.GetTypedHeaders().LastModified = narration.Result.UploadDate;//TODO: Add a FileLastUpdated field to narrations to indicate the last time the mp3/xml files have been updated
var requestHeaders = Request.GetTypedHeaders();
if (requestHeaders.IfModifiedSince.HasValue &&
requestHeaders.IfModifiedSince.Value >= narration.Result.UploadDate)
{
return StatusCode(StatusCodes.Status304NotModified);
}
return new FileStreamResult(new FileStream(narration.Result.LocalXmlFilePath, FileMode.Open, FileAccess.Read), "text/xml");
}
/// <summary>
/// updates narration metadata
/// </summary>

View File

@ -635,6 +635,28 @@
<param name="status">default: -1, unfiltered</param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.AudioNarrationController.GetMp3File(System.Int32)">
<summary>
get the corresponding mp3 file for the narration
</summary>
<remarks>
it could be protected (Authorized), but I guess I would have problems with available client components support,
so I preferred it to be anonymous, as it does not harm anybody I guess
</remarks>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.AudioNarrationController.GetXMLFile(System.Int32)">
<summary>
get the corresponding xml file for the narration
</summary>
<remarks>
it could be protected (Authorized), but I guess I would have problems with available client components support,
so I preferred it to be anonymous, as it does not harm anybody I guess
</remarks>
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.AudioNarrationController.UpdatePoemNarration(System.Int32,RMuseum.Models.GanjoorAudio.ViewModels.PoemNarrationUpdateViewModel)">
<summary>
updates narration metadata