From 1e713603660d1552917f7fe41fbc09dc8fa9f850 Mon Sep 17 00:00:00 2001 From: lukeIam Date: Tue, 9 Dec 2014 17:58:42 +0100 Subject: [PATCH] Added mp3 streaming for songs and a player to the app (experimental) --- ServerLib/CServer.cs | 2 ++ ServerLib/CWebservice.cs | 31 +++++++++++++++++++++++-- ServerLib/DataStructs.cs | 2 ++ ServerLib/ICWebservice.cs | 5 ++++ Vocaluxe/Base/Server/CVocaluxeServer.cs | 6 +++++ Vocaluxe/Website/index.html | 1 + Vocaluxe/Website/js/main.js | 7 ++++++ 7 files changed, 52 insertions(+), 2 deletions(-) diff --git a/ServerLib/CServer.cs b/ServerLib/CServer.cs index b085e2c1..f9bf489d 100644 --- a/ServerLib/CServer.cs +++ b/ServerLib/CServer.cs @@ -59,6 +59,8 @@ namespace ServerLib public static GetAllSongsDelegate GetAllSongs { internal get; set; } public static GetCurrentSongIdDelegate GetCurrentSongId { internal get; set; } + + public static GetMp3PathDelegate GetMp3Path { internal get; set; } #endregion #region playlist diff --git a/ServerLib/CWebservice.cs b/ServerLib/CWebservice.cs index fc385e89..42ed7c70 100644 --- a/ServerLib/CWebservice.cs +++ b/ServerLib/CWebservice.cs @@ -37,7 +37,8 @@ namespace ServerLib { sessionKey = Guid.Parse(sessionHeader); } - catch (Exception) {} + catch (Exception) + { } return sessionKey; } @@ -144,7 +145,7 @@ namespace ServerLib public SProfileData[] GetProfileList() { if (CServer.GetProfileList == null) - return new SProfileData[] {}; + return new SProfileData[] { }; return CServer.GetProfileList(); } #endregion @@ -308,6 +309,32 @@ namespace ServerLib { return CServer.GetAllSongs(); } + + public Stream GetMp3File(int songId) + { + if (WebOperationContext.Current != null) + { + WebOperationContext.Current.OutgoingResponse.ContentType = "audio/mpeg"; + WebOperationContext.Current.OutgoingResponse.LastModified = DateTime.UtcNow; + WebOperationContext.Current.OutgoingResponse.Headers.Add( + HttpResponseHeader.Expires, + DateTime.UtcNow.AddYears(1).ToString("r")); + } + + + String path = CServer.GetMp3Path(songId); + path = path.Replace("..", ""); + + + if (!File.Exists(path) || !(path.EndsWith(".mp3", StringComparison.InvariantCulture) || path.EndsWith(".ogg", StringComparison.InvariantCulture))) + { + if (WebOperationContext.Current != null) + WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound; + return null; + } + + return File.OpenRead(path); + } #endregion #region playlist diff --git a/ServerLib/DataStructs.cs b/ServerLib/DataStructs.cs index 11942ac9..591b5f98 100644 --- a/ServerLib/DataStructs.cs +++ b/ServerLib/DataStructs.cs @@ -111,6 +111,8 @@ namespace ServerLib public delegate int GetCurrentSongIdDelegate(); + public delegate string GetMp3PathDelegate(int songId); + [DataContract] public struct SSongInfo { diff --git a/ServerLib/ICWebservice.cs b/ServerLib/ICWebservice.cs index 1324c2d9..d13eb95d 100644 --- a/ServerLib/ICWebservice.cs +++ b/ServerLib/ICWebservice.cs @@ -127,6 +127,11 @@ namespace ServerLib ResponseFormat = WebMessageFormat.Json, UriTemplate = "/getAllSongs")] SSongInfo[] GetAllSongs(); + + [OperationContract, WebInvoke(Method = "GET", + ResponseFormat = WebMessageFormat.Json, + UriTemplate = "/getMp3?songId={songId}")] + Stream GetMp3File(int songId); #endregion #region playlist diff --git a/Vocaluxe/Base/Server/CVocaluxeServer.cs b/Vocaluxe/Base/Server/CVocaluxeServer.cs index 878bbc37..252c0ef7 100644 --- a/Vocaluxe/Base/Server/CVocaluxeServer.cs +++ b/Vocaluxe/Base/Server/CVocaluxeServer.cs @@ -73,6 +73,7 @@ namespace Vocaluxe.Base.Server CServer.GetSong = _GetSong; CServer.GetAllSongs = _GetAllSongs; CServer.GetCurrentSongId = _GetCurrentSongId; + CServer.GetMp3Path = _GetMp3Path; CServer.ValidatePassword = _ValidatePassword; CServer.GetUserRole = _GetUserRole; CServer.SetUserRole = _SetUserRole; @@ -463,6 +464,11 @@ namespace Vocaluxe.Base.Server return (from s in songs select _GetSongInfo(s, false)).ToArray(); } + private static string _GetMp3Path(int songId) + { + CSong song = CSongs.GetSong(songId); + return song.GetMP3(); + } private static int _GetCurrentSongId() { diff --git a/Vocaluxe/Website/index.html b/Vocaluxe/Website/index.html index 3ef1620b..c6731d36 100644 --- a/Vocaluxe/Website/index.html +++ b/Vocaluxe/Website/index.html @@ -231,6 +231,7 @@