Ejemplo n.º 1
0
        public async Task <ActionResult> Delete(int?id, string errorMessage = null)
        {
            // validate song id
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // fetch song from the database
            Song song = await _databaseGateway.Songs.GetAsync(id.Value);

            if (song == null)
            {
                return(HttpNotFound());
            }

            // check authorization
            if (!IsUserAuthorized(song, AuthorizationActivity.Delete))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized));
            }

            // build a DTO view model of the song for the view to render
            SongViewModel songViewModel = new SongViewModel(song);

            // add edit authorization data
            songViewModel.IsUserAuthorizedToDelete = true;
            songViewModel.IsUserAuthorizedToEdit   = IsUserAuthorized(song, AuthorizationActivity.Update);

            // try reading the chords & midi files and adding their content to the view model
            string chordsFilePath = await GetSongPath(song.Id, SongFileType.ChordProgressionFile);

            string midiFilePath = await GetSongPath(song.Id, SongFileType.MidiOriginalFile);

            try
            {
                songViewModel.ChordProgression = System.IO.File.ReadAllText(chordsFilePath);
                songViewModel.MidiData         = CompositionContext.ReadMidiFile(midiFilePath);
            }
            catch (Exception ex)
            {
                // failed to read chord/midi data. log the error
                HomeController.WriteErrorToLog(HttpContext, ex.Message);
            }
            finally
            {
                // release unmanaged open resources
                songViewModel.MidiData?.Stream?.Dispose();
            }

            // if an error message is present, save it for the view to render
            ViewBag.ErrorMessage = errorMessage;

            // pass the view model to the view to render
            return(View(songViewModel));
        }
Ejemplo n.º 2
0
 public static bool IsMidiFileValid(HttpPostedFileBase midiFileHandler, out IMidiFile midiFile, out string errorMessage)
 {
     errorMessage = null;
     try
     {   // fetch midi content from stream
         midiFile = CompositionContext.ReadMidiFile(midiFileHandler.InputStream);
         return(true);
     }
     catch (Exception ex)
     {
         midiFile     = null;
         errorMessage = ex.Message;
         return(false);
     }
 }