예제 #1
0
        public string EditSong(string form, string fileStream, string fileName, string fileType)
        {
            var result = "The song was not uploaded. Please try again. If the problem persists, please contact us at [email protected]";
            var formCollection = form.Split('&');
            var trmservice = new WebService.WCFWebServiceJson();
            var artist = trmservice.GetArtist(WebSecurity.CurrentUserId);
            var util = new Utilities();
            var localFile = Path.Combine(MusicManagerBase.LocalTempDestinationPath, fileName);

            if (!string.IsNullOrEmpty(fileStream))
            {
                byte[] bytes = Convert.FromBase64String(fileStream);
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    using (FileStream file = new FileStream(localFile, FileMode.Create, System.IO.FileAccess.Write))
                    {
                        ms.Read(bytes, 0, (int)ms.Length);
                        file.Write(bytes, 0, bytes.Length);
                        ms.Close();
                    }
                }
            }

            if (!string.IsNullOrEmpty(fileName) || !string.IsNullOrEmpty(MusicManagerBase.ReturnFormItemValue(formCollection, "MediaAssetPath")))
            {
                // Attempt to save the song
                try
                {
                    var albumCollection = new List<Album>();
                    var songGenreList = new List<Genre>();

                    foreach (var formItem in formCollection)
                    {
                        if (formItem.ToString().StartsWith("genre"))
                        {
                            songGenreList.Add(new Genre
                            {
                                GenreId = GetGenreId(formItem.ToString()),
                                GenreName = GetGenreName(formItem.ToString())
                            });
                        }
                    }

                    var albumId = 0;
                    if (!string.IsNullOrEmpty(MusicManagerBase.ReturnFormItemValue(formCollection, "AlbumId")) && Convert.ToInt32(MusicManagerBase.ReturnFormItemValue(formCollection, "AlbumId")) > 0)
                    {
                        albumId = Convert.ToInt32(MusicManagerBase.ReturnFormItemValue(formCollection, "AlbumId"));
                    }

                    var songId = 0;
                    if (!string.IsNullOrEmpty(MusicManagerBase.ReturnFormItemValue(formCollection, "SongId")) && Convert.ToInt32(MusicManagerBase.ReturnFormItemValue(formCollection, "SongId")) > 0)
                    {
                        songId = Convert.ToInt32(MusicManagerBase.ReturnFormItemValue(formCollection, "SongId"));
                    }

                    var album = trmservice.GetArtistAlbumDetails(albumId);
                    albumCollection.Add(album);

                    var mediaFileFolder = string.Empty;
                    var mediaFileLocation = string.Empty;

                    if (!string.IsNullOrEmpty(fileName))
                    {
                        mediaFileLocation = localFile;
                        mediaFileFolder = util.RemoveSpaces(artist.ArtistName) + "/" + util.RemoveSpaces(album.AlbumTitle) + "/";
                    }

                    var releaseDate = MusicManagerBase.ReturnFormItemValue(formCollection, "SongReleaseDate").ToString().Replace("%2F", "/");

                    var song = new Song()
                    {
                        AlbumCollection = albumCollection,
                        CreatedDate = DateTime.Now,
                        GenreCollection = songGenreList,
                        SongComposer = MusicManagerBase.ReturnFormItemValue(formCollection, "SongComposer"),
                        SongId = songId,
                        SongReleaseDate = Convert.ToDateTime(releaseDate),
                        SongTitle = MusicManagerBase.ReturnFormItemValue(formCollection, "SongTitle"),
                        PRS = MusicManagerBase.ReturnFormBooleanValue(formCollection, "PRS")
                    };

                    // upload and save the song
                    if (trmservice.UploadSong(song, mediaFileLocation, "mp3", mediaFileFolder))
                    {
                        result = "The song \"" + song.SongTitle + "\" has been uploaded successfully.";
                    }
                }
                catch (MembershipCreateUserException e)
                {
                    result = ErrorCodeToString(e.StatusCode);
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
            }
            else
            {
                result = "Please select a media file to upload";
            }

            return result;
        }