Добавлены права на воспроизведение.
This commit is contained in:
@@ -13,15 +13,18 @@ public class AudioController : ControllerBase
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly YandexMusicService _yandexService;
|
||||
private readonly SharedPlaylistService _sharedService;
|
||||
private readonly JwtService _jwtService;
|
||||
|
||||
public AudioController(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
YandexMusicService yandexService,
|
||||
SharedPlaylistService sharedService,
|
||||
JwtService jwtService)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_yandexService = yandexService;
|
||||
_sharedService = sharedService;
|
||||
_jwtService = jwtService;
|
||||
}
|
||||
|
||||
@@ -29,17 +32,18 @@ public class AudioController : ControllerBase
|
||||
/// Потоковое воспроизведение трека из Яндекс.Музыки.
|
||||
/// </summary>
|
||||
/// <param name="trackId">ID трека (например, "21696942").</param>
|
||||
/// <param name="access_token">gwt пользователя</param>
|
||||
/// <param name="shared_id">ID расшаренного плейлиста</param>
|
||||
[HttpGet("track/{trackId}")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> StreamTrack(string trackId, [FromQuery] string? access_token = null)
|
||||
public async Task<IActionResult> StreamTrack(string trackId, [FromQuery] string? access_token = null, [FromQuery] string? shared_id = null)
|
||||
{
|
||||
var user = await GetUserFromToken(access_token);
|
||||
if (user == null)
|
||||
return Unauthorized();
|
||||
if (user == null) user = await GetUserFromSharedPlaylistId(shared_id);
|
||||
if (user == null) return Unauthorized();
|
||||
|
||||
var streamUrl = await _yandexService.GetTrackFileUrlAsync(user, trackId);
|
||||
if (string.IsNullOrEmpty(streamUrl))
|
||||
return NotFound();
|
||||
if (string.IsNullOrEmpty(streamUrl)) return NotFound();
|
||||
|
||||
var httpClient = new HttpClient();
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, streamUrl);
|
||||
@@ -56,11 +60,11 @@ public class AudioController : ControllerBase
|
||||
Response.ContentType = response.Content.Headers.ContentType?.ToString() ?? "audio/mpeg";
|
||||
|
||||
if (response.Content.Headers.Contains("Content-Range"))
|
||||
Response.Headers.Add("Content-Range", response.Content.Headers.ContentRange?.ToString());
|
||||
Response.Headers.Append("Content-Range", response.Content.Headers.ContentRange?.ToString());
|
||||
if (response.Headers.Contains("Accept-Ranges"))
|
||||
Response.Headers.Add("Accept-Ranges", response.Headers.AcceptRanges?.ToString());
|
||||
Response.Headers.Append("Accept-Ranges", response.Headers.AcceptRanges?.ToString());
|
||||
if (response.Content.Headers.Contains("Content-Length"))
|
||||
Response.Headers.Add("Content-Length", response.Content.Headers.ContentLength?.ToString());
|
||||
Response.Headers.Append("Content-Length", response.Content.Headers.ContentLength?.ToString());
|
||||
|
||||
await response.Content.CopyToAsync(Response.Body);
|
||||
return new EmptyResult();
|
||||
@@ -68,17 +72,28 @@ public class AudioController : ControllerBase
|
||||
|
||||
private async Task<ApplicationUser?> GetUserFromToken(string? token)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token))
|
||||
return null;
|
||||
if (string.IsNullOrEmpty(token)) return null;
|
||||
|
||||
var principal = _jwtService.ValidateToken(token);
|
||||
if (principal == null)
|
||||
return null;
|
||||
if (principal == null) return null;
|
||||
|
||||
var userId = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (string.IsNullOrEmpty(userId))
|
||||
return null;
|
||||
if (string.IsNullOrEmpty(userId)) return null;
|
||||
|
||||
return await _userManager.FindByIdAsync(userId);
|
||||
}
|
||||
|
||||
private async Task<ApplicationUser?> GetUserFromSharedPlaylistId(string? sharedId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sharedId)) return null;
|
||||
|
||||
var playlist = await _sharedService.GetEntityByTokenAsync(sharedId);
|
||||
|
||||
if (playlist == null) return null;
|
||||
|
||||
if (!await _sharedService.CanPlayEveryoneAsync(playlist)) return null;
|
||||
|
||||
return await _userManager.FindByIdAsync(playlist.CreatorUserId.ToString());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user