Esempio n. 1
0
 /// <summary>
 /// Helper method for populating the song list with songs.
 /// </summary>
 /// <param name="info">
 /// The AlbumInfo-object the AlbumDetails-instance is based on.
 /// </param>
 private void PopulateSongList(AlbumInfo info)
 {
     foreach (var song in info.Songs)
     {
         var listItem = new ListViewItem(song.Title);
         listItem.SubItems.Add(song.Artist);
         listItem.SubItems.Add(song.Genre);
         listItem.SubItems.Add(song.Price.ToString());
         listItem.SubItems.Add(song.Duration.ToString());
         songList.Items.Add(listItem);
     }
 }
Esempio n. 2
0
        /// <author>Kenneth Søhrmann</author>
        /// <summary>
        /// Convenience method for uploading an album and its songs to the 
        /// server. 
        /// The methods blocks during upload.
        /// </summary>
        /// <param name="credentials">
        /// The credentials of the publisher who is uploading the specified album.
        /// </param>
        /// <param name="songs">
        /// A list of SongInfoUpload-objects each representing the metadata of the songs
        /// that is a part of the album.
        /// </param>
        /// <param name="albumInfo">
        /// Instance holding the metadata of the album to be uploaded.
        /// </param>
        /// <exception cref="WebException">
        /// Is thrown if the upload of one of the songs or the thumbnail failed.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Is thrown if the credentials are not authorized.
        /// </exception>
        public static void UploadAlbum(Credentials credentials, List<SongInfoUpload> songs, AlbumInfoUpload albumInfo)
        {
            // Check specified song files
            foreach (SongInfoUpload song in songs)
            {
                // If the filepath has not been stated, it is okay.
                if (string.IsNullOrEmpty(song.FilePath))
                {
                    continue;
                }

                if (!File.Exists(song.FilePath))
                {
                    throw new ArgumentException("The file, " + song.FilePath + ", does not exist.");
                }
                if (!new FileInfo(song.FilePath).Extension.Equals(".mp3"))
                {
                    throw new ArgumentException("The file, " + song.FilePath + ", does not have the supported extension, mp3.");
                }
            }

            var serviceClient = GetServiceClient();
            var accountCredentials = new AccountCredentials
            {
                UserName = credentials.UserName,
                HashedPassword = credentials.HashedPassword
            };

            try
            {
                serviceClient.ValidateCredentials(accountCredentials);
            }
            catch (Exception)
            {
                throw new ArgumentException("Invalid credentials submitted.");
            }

            var albumMedia = new AlbumInfo()
                {
                    Title = albumInfo.Title,
                    Type = MediaType.Album,
                    Genre = albumInfo.Genre,
                    Price = albumInfo.Price,
                    Publisher = albumInfo.Publisher,
                    ReleaseDate = albumInfo.ReleaseDate,

                    AlbumArtist = albumInfo.AlbumArtist,
                    Description = albumInfo.Description,
                };

            int albumMediaId = serviceClient.PublishMedia(albumMedia, accountCredentials);

            // Upload the thumbnail of the album.
            try
            {
                UploadThumbnail(albumMediaId, albumInfo, credentials);
            }
            catch (Exception e)
            {
                serviceClient.DeleteMedia(albumMediaId, accountCredentials);
                throw new WebException("Upload failed: " + e.Message);
            }

            // For database clean up if upload fails.
            var publishedSongs = new List<int>();

            // Process each song file; upload data to database, and upload song files to
            // server.
            foreach (SongInfoUpload songInfo in songs)
            {
                var songMedia = new SongInfo()
                {
                    Title = songInfo.Title,
                    Type = MediaType.Song,
                    Genre = songInfo.Genre,
                    Price = songInfo.Price,
                    Publisher = songInfo.Publisher,
                    ReleaseDate = songInfo.ReleaseDate,

                    AlbumId = albumMediaId, // Set the album media id to the one received when publishing the album metadata.
                    Artist = songInfo.Artist,
                    Duration = songInfo.Duration
                };

                // publish the metadata of the song to the server.
                int songMediaId = serviceClient.PublishMedia(songMedia, accountCredentials);

                // For rollback if upload of binary data fails.
                publishedSongs.Add(songMediaId);

                try
                {
                    if (!string.IsNullOrEmpty(songInfo.FilePath))
                    {
                        UploadMediaFile(songInfo.FilePath, songMediaId, credentials);
                    }

                    if (songInfo.Thumbnail != null)
                    {
                        UploadThumbnail(songMediaId, songInfo, credentials);
                    }
                }
                catch (Exception e)
                {
                    // Clean up server database upon upload failure.
                    foreach (int songId in publishedSongs)
                    {
                        serviceClient.DeleteMedia(songId, accountCredentials);
                    }

                    serviceClient.DeleteMedia(albumMediaId, accountCredentials);
                    throw new WebException("Upload failed: " + e.Message);
                }
            }
        }