Skip to content

Commit

Permalink
Add HEAD method and MIME type for GETing audio files
Browse files Browse the repository at this point in the history
  • Loading branch information
cankaratepe23 committed Aug 27, 2024
1 parent fd7916f commit 1937e64
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
52 changes: 51 additions & 1 deletion MedicBot/Controller/AudioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using ZstdSharp.Unsafe;
using MimeTypes;

namespace MedicBot.Controller;

Expand Down Expand Up @@ -147,6 +147,9 @@ public IActionResult Get(string audioId, [FromQuery] ulong guildId)
Response.Headers.LastModified = lastUpdate.ToHttpDate();
Response.Headers.CacheControl = "no-cache";

var mimeType = MimeTypeMap.GetMimeType(track.Path[track.Path.LastIndexOf('.')..]);
Response.Headers.ContentType = mimeType;

var file = System.IO.File.OpenRead(track.Path);
return Ok(file);
}
Expand All @@ -159,4 +162,51 @@ public IActionResult Get(string audioId, [FromQuery] ulong guildId)
return BadRequest(e.Message);
}
}

[HttpHead("{audioId}")]
[Authorize(Policy = "CombinedPolicy")]
public IActionResult Head(string audioId, [FromQuery] ulong guildId)
{
try
{
var userClaim = HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier) ?? throw new InvalidCredentialException();
Log.Debug("User's ID is: {UserId}", userClaim.Value);

var track = AudioManager.FindById(audioId) ?? throw new AudioTrackNotFoundException($"No track was found with ID: {audioId}");

var lastUpdate = DateTimeOffset.FromUnixTimeSeconds(track.Id.Timestamp);
if (track.LastModifiedAt.HasValue)
{
lastUpdate = (DateTimeOffset) track.LastModifiedAt;
}

if (Request.Headers.IfModifiedSince.Count != 0)
{
var ifModifiedSinceStr = Request.Headers.IfModifiedSince[0];
if (!string.IsNullOrEmpty(ifModifiedSinceStr))
{
var ifModifiedSince = DateTimeOffset.ParseExact(ifModifiedSinceStr, "r", CultureInfo.InvariantCulture);
if (ifModifiedSince >= lastUpdate.AddTicks(-(lastUpdate.Ticks % TimeSpan.TicksPerSecond)))
{
return StatusCode(304);
}
}
}

Response.Headers.LastModified = lastUpdate.ToHttpDate();
Response.Headers.CacheControl = "no-cache";

var mimeType = MimeTypeMap.GetMimeType(track.Path[track.Path.LastIndexOf('.')..]);
Response.Headers.ContentType = mimeType;
return Ok();
}
catch (AudioTrackNotFoundException e)
{
return NotFound(e.Message);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
}
1 change: 1 addition & 0 deletions MedicBot/MedicBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="HtmlAgilityPack" Version="1.11.62" />
<PackageReference Include="Magick.NET-Q8-x64" Version="13.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.17" />
<PackageReference Include="MimeTypeMapOfficial" Version="1.0.17" />
<PackageReference Include="MongoDB.Driver" Version="2.24.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
Expand Down

0 comments on commit 1937e64

Please sign in to comment.