roadie/RoadieApi/Controllers/SubsonicController.cs

385 lines
17 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Roadie.Api.Services;
using Roadie.Library.Caching;
2018-11-22 07:48:32 -06:00
using Roadie.Library.Extensions;
using Roadie.Library.Identity;
using Roadie.Library.Models.ThirdPartyApi.Subsonic;
2018-11-19 22:47:12 -06:00
using System.Net;
using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
//[Produces("application/json")]
[Route("subsonic/rest")]
[ApiController]
//[Authorize]
public class SubsonicController : EntityControllerBase
{
2018-11-20 08:36:07 -06:00
private IPlayActivityService PlayActivityService { get; }
2018-11-21 00:34:53 -06:00
private ISubsonicService SubsonicService { get; }
2018-11-20 08:36:07 -06:00
private ITrackService TrackService { get; }
2018-11-20 08:36:07 -06:00
public SubsonicController(ISubsonicService subsonicService, ITrackService trackService, IPlayActivityService playActivityService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager<ApplicationUser> userManager)
: base(cacheManager, configuration, userManager)
{
2018-11-21 00:34:53 -06:00
this.Logger = logger.CreateLogger("RoadieApi.Controllers.SubsonicController");
this.SubsonicService = subsonicService;
2018-11-20 08:36:07 -06:00
this.TrackService = trackService;
this.PlayActivityService = playActivityService;
}
2018-11-22 07:48:32 -06:00
[HttpGet("getAlbum.view")]
[HttpPost("getAlbum.view")]
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetAlbum([FromQuery]Request request)
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetAlbum(request, null);
return this.BuildResponse(request, result, "album");
}
2018-11-22 07:48:32 -06:00
[HttpGet("getAlbum.view")]
[HttpPost("getAlbum.view")]
2018-11-21 12:19:38 -06:00
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetSong([FromQuery]Request request, string id)
2018-11-21 12:19:38 -06:00
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetAlbum(request, null);
return this.BuildResponse(request, result, "song");
2018-11-21 12:19:38 -06:00
}
2018-11-22 07:48:32 -06:00
[HttpGet("getArtist.view")]
[HttpPost("getArtist.view")]
2018-11-21 12:19:38 -06:00
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetArtist([FromQuery]Request request)
2018-11-21 12:19:38 -06:00
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetArtist(request, null);
return this.BuildResponse(request, result, "artist");
2018-11-21 12:19:38 -06:00
}
2018-11-22 07:48:32 -06:00
[HttpGet("getAlbumInfo.view")]
[HttpPost("getAlbumInfo.view")]
2018-11-21 12:19:38 -06:00
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetAlbumInfo([FromQuery]Request request)
2018-11-21 12:19:38 -06:00
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetAlbumInfo(request, null, AlbumInfoVersion.One);
return this.BuildResponse(request, result, "albumInfo");
2018-11-21 12:19:38 -06:00
}
2018-11-22 07:48:32 -06:00
[HttpGet("getAlbumInfo2.view")]
[HttpPost("getAlbumInfo2.view")]
2018-11-21 12:19:38 -06:00
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetAlbumInfo2([FromQuery]Request request)
2018-11-21 12:19:38 -06:00
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetAlbumInfo(request, null, AlbumInfoVersion.Two);
return this.BuildResponse(request, result, "albumInfo");
2018-11-21 12:19:38 -06:00
}
2018-11-22 07:48:32 -06:00
[HttpGet("getVideos.view")]
[HttpPost("getVideos.view")]
2018-11-21 12:19:38 -06:00
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public IActionResult GetVideos([FromQuery]Request request)
2018-11-21 12:19:38 -06:00
{
2018-11-22 07:48:32 -06:00
var result = this.SubsonicService.GetVideos(request);
return this.BuildResponse(request, result, "videos");
2018-11-21 12:19:38 -06:00
}
2018-11-22 07:48:32 -06:00
[HttpGet("getLyrics.view")]
[HttpPost("getLyrics.view")]
2018-11-21 12:19:38 -06:00
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public IActionResult GetLyrics([FromQuery]Request request, string artist, string title)
2018-11-21 12:19:38 -06:00
{
2018-11-22 07:48:32 -06:00
var result = this.SubsonicService.GetLyrics(request, artist, title);
return this.BuildResponse(request, result, "lyrics ");
}
[HttpGet("getAlbumList.view")]
[HttpPost("getAlbumList.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetAlbumList([FromQuery]Request request)
{
var result = await this.SubsonicService.GetAlbumList(request, null, AlbumListVersions.One);
return this.BuildResponse(request, result, "albumList");
2018-11-21 12:19:38 -06:00
}
2018-11-21 00:34:53 -06:00
[HttpGet("getAlbumList2.view")]
[HttpPost("getAlbumList2.view")]
[ProducesResponseType(200)]
2018-11-21 00:34:53 -06:00
public async Task<IActionResult> GetAlbumList2([FromQuery]Request request)
{
var result = await this.SubsonicService.GetAlbumList(request, null, AlbumListVersions.Two);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result, "albumList");
2018-11-21 00:34:53 -06:00
}
[HttpGet("getArtistInfo.view")]
[HttpPost("getArtistInfo.view")]
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetArtistInfo([FromQuery]Request request, int? count, bool? includeNotPresent)
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetArtistInfo(request, count, includeNotPresent ?? false, ArtistInfoVersion.One);
return this.BuildResponse(request, result, "artistInfo");
}
[HttpGet("getArtistInfo2.view")]
[HttpPost("getArtistInfo2.view")]
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetArtistInfo2([FromQuery]Request request, int? count, bool? includeNotPresent)
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetArtistInfo(request, count, includeNotPresent ?? false, ArtistInfoVersion.Two);
return this.BuildResponse(request, result, "artistInfo2");
}
[HttpGet("getArtists.view")]
[HttpPost("getArtists.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetArtists([FromQuery]Request request)
{
var result = await this.SubsonicService.GetArtists(request, null);
return this.BuildResponse(request, result, "artists");
}
[HttpGet("getAvatar.view")]
[HttpPost("getAvatar.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetAvatar([FromQuery]Request request, string username)
{
//[HttpGet("user/{id}/{width:int?}/{height:int?}")]
var user = await this.UserManager.FindByNameAsync(username);
return Redirect($"/images/user/{ user.RoadieId }/{this.RoadieSettings.ThumbnailImageSize.Width}/{this.RoadieSettings.ThumbnailImageSize.Height}");
}
2018-11-21 00:34:53 -06:00
[HttpGet("getCoverArt.view")]
[HttpPost("getCoverArt.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetCoverArt([FromQuery]Request request, int? size)
{
var result = await this.SubsonicService.GetCoverArt(request, size);
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
this.Logger.LogWarning($"GetCoverArt Failed For [{ JsonConvert.SerializeObject(request) }]");
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return File(fileContents: result.Data.Bytes,
contentType: result.ContentType,
fileDownloadName: $"{ result.Data.Caption ?? request.id.ToString()}.jpg",
lastModified: result.LastModified,
entityTag: result.ETag);
}
[HttpGet("getGenres.view")]
[HttpPost("getGenres.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetGenres([FromQuery]Request request)
{
var result = await this.SubsonicService.GetGenres(request);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result, "genres");
2018-11-21 00:34:53 -06:00
}
[HttpGet("getIndexes.view")]
[HttpPost("getIndexes.view")]
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetIndexes([FromQuery]Request request, long? ifModifiedSince = null)
{
var result = await this.SubsonicService.GetIndexes(request, null, ifModifiedSince);
return this.BuildResponse(request, result, "indexes");
}
[HttpGet("getLicense.view")]
[HttpPost("getLicense.view")]
[ProducesResponseType(200)]
public IActionResult GetLicense([FromQuery]Request request)
2018-11-21 00:34:53 -06:00
{
2018-11-22 07:48:32 -06:00
var result = this.SubsonicService.GetLicense(request);
return this.BuildResponse(request, result, "license");
}
2018-11-19 22:47:12 -06:00
[HttpGet("getMusicDirectory.view")]
2018-11-21 00:34:53 -06:00
[HttpPost("getMusicDirectory.view")]
2018-11-19 22:47:12 -06:00
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetMusicDirectory([FromQuery]Request request)
2018-11-19 22:47:12 -06:00
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetMusicDirectory(request, null);
return this.BuildResponse(request, result, "directory");
2018-11-19 22:47:12 -06:00
}
2018-11-21 00:34:53 -06:00
[HttpGet("getMusicFolders.view")]
[HttpPost("getMusicFolders.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetMusicFolders([FromQuery]Request request)
{
var result = await this.SubsonicService.GetMusicFolders(request);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result, "musicFolders");
2018-11-21 00:34:53 -06:00
}
2018-11-19 22:47:12 -06:00
2018-11-21 12:19:38 -06:00
[HttpGet("getPlaylist.view")]
[HttpPost("getPlaylist.view")]
[ProducesResponseType(200)]
2018-11-22 07:48:32 -06:00
public async Task<IActionResult> GetPlaylist([FromQuery]Request request)
2018-11-21 12:19:38 -06:00
{
2018-11-22 07:48:32 -06:00
var result = await this.SubsonicService.GetPlaylist(request, null);
return this.BuildResponse(request, result, "playlist");
2018-11-21 12:19:38 -06:00
}
[HttpGet("getPlaylists.view")]
2018-11-21 00:34:53 -06:00
[HttpPost("getPlaylists.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetPlaylists([FromQuery]Request request, string username)
{
var result = await this.SubsonicService.GetPlaylists(request, null, username);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result, "playlists");
}
2018-11-19 22:47:12 -06:00
[HttpGet("getPodcasts.view")]
2018-11-21 00:34:53 -06:00
[HttpPost("getPodcasts.view")]
2018-11-19 22:47:12 -06:00
[ProducesResponseType(200)]
public async Task<IActionResult> GetPodcasts([FromQuery]Request request, bool includeEpisodes)
{
var result = await this.SubsonicService.GetPodcasts(request);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result, "podcasts");
}
[HttpGet("getRandomSongs.view")]
[HttpPost("getRandomSongs.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetRandomSongs([FromQuery]Request request)
{
var result = await this.SubsonicService.GetRandomSongs(request, null);
return this.BuildResponse(request, result, "randomSongs");
}
[HttpGet("getStarred.view")]
[HttpPost("getStarred.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetStarred([FromQuery]Request request)
{
var result = await this.SubsonicService.GetStarred(request, null, StarredVersion.One);
return this.BuildResponse(request, result, "starred");
}
[HttpGet("getStarred2.view")]
[HttpPost("getStarred2.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetStarred2([FromQuery]Request request)
{
var result = await this.SubsonicService.GetStarred(request, null, StarredVersion.Two);
return this.BuildResponse(request, result, "starred");
}
[HttpGet("getUser.view")]
[HttpPost("getUser.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> GetUser([FromQuery]Request request, string username)
{
var result = await this.SubsonicService.GetUser(request, username);
return this.BuildResponse(request, result, "user");
2018-11-19 22:47:12 -06:00
}
2018-11-21 00:34:53 -06:00
[HttpGet("ping.view")]
[HttpPost("ping.view")]
2018-11-20 08:36:07 -06:00
[ProducesResponseType(200)]
2018-11-21 00:34:53 -06:00
public IActionResult Ping([FromQuery]Request request)
2018-11-20 08:36:07 -06:00
{
2018-11-22 07:48:32 -06:00
if (request.IsJSONRequest)
2018-11-20 08:36:07 -06:00
{
2018-11-21 00:34:53 -06:00
var result = this.SubsonicService.Ping(request);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result);
2018-11-20 08:36:07 -06:00
}
2018-11-21 00:34:53 -06:00
return Content("<subsonic-response xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://subsonic.org/restapi\" status=\"ok\" version=\"1.16.0\" />", "application/xml");
2018-11-20 08:36:07 -06:00
}
2018-11-21 00:34:53 -06:00
/// <summary>
/// Returns albums, artists and songs matching the given search criteria. Supports paging through the result.
/// </summary>
2018-11-21 12:19:38 -06:00
[HttpGet("search.view")]
[HttpPost("search.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> Search([FromQuery]Request request)
{
var result = await this.SubsonicService.Search(request, null, SearchVersion.One);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result, "searchResult");
2018-11-21 12:19:38 -06:00
}
2018-11-21 00:34:53 -06:00
[HttpGet("search2.view")]
[HttpPost("search2.view")]
2018-11-22 07:48:32 -06:00
[ProducesResponseType(200)]
2018-11-21 00:34:53 -06:00
public async Task<IActionResult> Search2([FromQuery]Request request)
{
2018-11-21 12:19:38 -06:00
var result = await this.SubsonicService.Search(request, null, SearchVersion.Two);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result, "searchResult2");
}
2018-11-21 12:19:38 -06:00
[HttpGet("search3.view")]
[HttpPost("search3.view")]
[ProducesResponseType(200)]
public async Task<IActionResult> Search3([FromQuery]Request request)
{
var result = await this.SubsonicService.Search(request, null, SearchVersion.Three);
2018-11-22 07:48:32 -06:00
return this.BuildResponse(request, result, "searchResult3");
2018-11-21 12:19:38 -06:00
}
2018-11-21 00:34:53 -06:00
[HttpGet("stream.view")]
[HttpPost("stream.view")]
[ProducesResponseType(200)]
public async Task<FileStreamResult> StreamTrack([FromQuery]Request request)
{
var trackId = request.TrackId;
if (trackId == null)
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}
return await base.StreamTrack(trackId.Value, this.TrackService, this.PlayActivityService);
}
2018-11-20 08:36:07 -06:00
#region Response Builder Methods
private string BuildJsonResult(Response response, string responseType)
{
2018-11-22 07:48:32 -06:00
var status = response?.status.ToString();
var version = response?.version ?? Roadie.Api.Services.SubsonicService.SubsonicVersion;
if (responseType == null)
{
2018-11-22 07:48:32 -06:00
return "{ \"subsonic-response\": { \"status\":\"" + status + "\", \"version\": \"" + version + "\" }}";
}
return "{ \"subsonic-response\": { \"status\":\"" + status + "\", \"version\": \"" + version + "\", \"" + responseType + "\":" + response != null ? JsonConvert.SerializeObject(response.Item) : string.Empty + "}}";
}
private IActionResult SendError(Request request, SubsonicOperationResult<Response> response = null, string responseType = null)
{
var version = response?.Data?.version ?? Roadie.Api.Services.SubsonicService.SubsonicVersion;
string errorDescription = response?.ErrorCode?.DescriptionAttr();
int? errorCode = (int?)response?.ErrorCode;
if (request.IsJSONRequest)
{
this.Response.ContentType = "application/json";
return Content("{ \"subsonic-response\": { \"status\":\"failed\", \"version\": \"" + version + "\", \"error\":{\"code\":\"" + errorCode + "\",\"message\":\"" + errorDescription + "\"}}}");
}
2018-11-22 07:48:32 -06:00
this.Response.ContentType = "application/xml";
return Content($"<?xml version=\"1.0\" encoding=\"UTF-8\"?><subsonic-response xmlns=\"http://subsonic.org/restapi\" status=\"failed\" version=\"{ version }\"><error code=\"{ errorCode }\" message=\"{ errorDescription }\"/></subsonic-response>");
}
2018-11-22 07:48:32 -06:00
private IActionResult BuildResponse(Request request, SubsonicOperationResult<Response> response = null, string responseType = null)
{
2018-11-21 00:34:53 -06:00
var acceptHeader = this.Request.Headers["Accept"];
2018-11-22 07:48:32 -06:00
this.Logger.LogTrace($"Subsonic Request: Method [{ this.Request.Method }], Accept Header [{ acceptHeader }], Path [{ this.Request.Path }], Query String [{ this.Request.QueryString }], Response Error Code [{ response.ErrorCode }], Request [{ JsonConvert.SerializeObject(request) }] ResponseType [{ responseType }]");
if (response.ErrorCode.HasValue)
{
return this.SendError(request, response, responseType);
}
if (request.IsJSONRequest)
{
this.Response.ContentType = "application/json";
2018-11-22 07:48:32 -06:00
return Content(this.BuildJsonResult(response.Data, responseType));
}
2018-11-21 00:34:53 -06:00
this.Response.ContentType = "application/xml";
return Ok(response);
}
#endregion Response Builder Methods
}
2018-11-15 21:37:00 -06:00
}