// GET /api/music public IEnumerable <Models.MusicModel> Get() { Cache cache = HttpContext.Current.Cache; List <Models.MusicModel> list = (List <Models.MusicModel>)cache["songlist"]; if (list == null) { list = new List <Models.MusicModel>(); foreach (string filepath in System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("~/Content/Music"))) { TagLib.File file = TagLib.File.Create(filepath); Models.MusicModel song = new Models.MusicModel(); var s = file.Tag.AlbumArtists.ToList(); s.AddRange(file.Tag.Performers.ToList()); song.Artist = string.Join(", ", s.ToArray()); //song.Artist = string.Join(", ", file.Tag.Performers, file.Tag.AlbumArtists); song.MusicId = Guid.Parse(file.Name.Split('\\').Last().Replace(".mp3", "")); song.Length = file.Length; song.Track = file.Tag.Track; song.Title = file.Tag.Title; song.Genres = string.Join(", ", file.Tag.Genres); song.Album = file.Tag.Album; song.Url = "/api/music/" + file.Name.Split('\\').Last().Replace(".mp3", ""); list.Add(song); file.Dispose(); } cache.Insert("songlist", list, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30)); } return(list); }
// GET /api/music public IEnumerable<Models.MusicModel> Get() { Cache cache = HttpContext.Current.Cache; List<Models.MusicModel> list = (List<Models.MusicModel>)cache["songlist"]; if (list == null) { list = new List<Models.MusicModel>(); foreach (string filepath in System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("~/Content/Music"))) { TagLib.File file = TagLib.File.Create(filepath); Models.MusicModel song = new Models.MusicModel(); var s = file.Tag.AlbumArtists.ToList(); s.AddRange(file.Tag.Performers.ToList()); song.Artist = string.Join(", ", s.ToArray()); //song.Artist = string.Join(", ", file.Tag.Performers, file.Tag.AlbumArtists); song.MusicId = Guid.Parse(file.Name.Split('\\').Last().Replace(".mp3", "")); song.Length = file.Length; song.Track = file.Tag.Track; song.Title = file.Tag.Title; song.Genres = string.Join(", ", file.Tag.Genres); song.Album = file.Tag.Album; song.Url = "/api/music/" + file.Name.Split('\\').Last().Replace(".mp3", ""); list.Add(song); file.Dispose(); } cache.Insert("songlist", list, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30)); } return list; }
// PUT /api/music/5 public HttpResponseMessage Put(Models.MusicModel data) { HttpResponseMessage theresponse = new HttpResponseMessage(); string filePath = HttpContext.Current.Server.MapPath("~/Content/Music") + "\\" + data.MusicId.ToString() + ".mp3"; if (System.IO.File.Exists(filePath)) { TagLib.File file = TagLib.File.Create(filePath); if (!string.IsNullOrWhiteSpace(data.Title)) { file.Tag.Title = data.Title; } if (!string.IsNullOrWhiteSpace(data.Artist)) { file.Tag.Performers = data.Artist.Split(','); } if (!string.IsNullOrWhiteSpace(data.Album)) { file.Tag.Album = data.Album; } if (!string.IsNullOrWhiteSpace(data.Genres)) { file.Tag.Genres = data.Genres.Split(','); } file.Tag.Track = data.Track; file.Save(); theresponse.StatusCode = System.Net.HttpStatusCode.OK; //Hreinsum cacheið HttpContext.Current.Cache.Remove("songlist"); } else { theresponse.StatusCode = System.Net.HttpStatusCode.NotFound; } return(theresponse); }