示例#1
0
        public async Task <ActionResult> Create(SongViewModel songViewModel)
        {
            // if model is invalid return back the view again
            if (!ModelState.IsValid)
            {
                return(View(songViewModel));
            }

            // get current timestamp
            DateTime timestamp = DateTime.Now;

            // Create a new song instance based on the form data from the view model
            Song song = new Song
            {
                Created          = timestamp,
                Modified         = timestamp,
                Title            = songViewModel.Title,
                Artist           = songViewModel.Artist,
                MidiFileName     = songViewModel.MidiFileHandler.FileName,
                ChordsFileName   = songViewModel.ChordsFileHandler.FileName,
                MelodyTrackIndex = songViewModel.MelodyTrackIndex,
                IsPublic         = songViewModel.IsPublic && User.IsInRole(RoleName.Admin),
                UserId           = this.User.Identity.GetUserId(),
            };

            // set name for the playback file to be created based on the midi file
            song.SetPlaybackName();

            // save the new song in the database
            _databaseGateway.Songs.Add(song);
            await _databaseGateway.CommitChangesAsync();

            // save the files of the new song on the file server
            IMidiFile playbackFile = null;

            try
            {
                // create a new directory for the new song
                string directoryPath = await GetSongPath(song.Id);

                Directory.CreateDirectory(directoryPath);

                // save the midi file in the new directory
                string midiFileFullPath = directoryPath + song.MidiFileName;
                songViewModel.MidiFileHandler.SaveAs(midiFileFullPath);

                // save the midi playback file in the new directory
                string midiPlaybackFullPath = directoryPath + song.MidiPlaybackFileName;
                playbackFile = CompositionContext.CreateMidiPlayback(songViewModel.MidiFileHandler.InputStream, song.MelodyTrackIndex);
                playbackFile.SaveFile(outputPath: midiPlaybackFullPath, pathIncludesFileName: true);

                // save the chord progression file in the new directory
                string chordsFilefullPath = directoryPath + song.ChordsFileName;
                songViewModel.ChordsFileHandler.SaveAs(chordsFilefullPath);
            }
            catch (Exception ex)
            {
                // in case of failure, rollback DB changes and log error message
                _databaseGateway.Songs.Remove(song);
                await _databaseGateway.CommitChangesAsync();

                HomeController.WriteErrorToLog(HttpContext, ex.Message);
            }
            finally
            {
                // release open unmanaged resources
                songViewModel.ChordsFileHandler?.InputStream?.Dispose();
                songViewModel.MidiFileHandler?.InputStream?.Dispose();
                playbackFile?.Stream?.Dispose();
            }

            // If creation was successful, redirect to new song details page
            string successMessage = $"The Song '{song.Title}' by '{song.Artist}' was successfully uploaded.";

            return(RedirectToAction(nameof(Details), new { Id = song.Id, message = successMessage }));
        }