Example #1
0
        public static bool IsChordsFileValid(HttpPostedFileBase chordsFileHandler, SongFileType fileType, out IList <IBar> bars, out string errorMessage)
        {
            bars         = null;
            errorMessage = null;

            try
            {
                // build a stream reader for reading the chord progression
                StreamReader streamReader = new StreamReader(chordsFileHandler.InputStream);
                bars = CompositionContext.ReadChordsFromFile(streamReader);

                // reset stream origin seek to beginning
                chordsFileHandler.InputStream.Position = 0;

                // no errors found while parsing the file
                return(true);
            }
            catch (Exception ex)
            {
                // if this is a chord file validation, return any found errors if such exist
                if (fileType == SongFileType.ChordProgressionFile)
                {
                    errorMessage = ex.Message;
                    return(false);
                }

                // if this is a check for midi file, no need to fail on chords
                return(true);
            }
        }
Example #2
0
        /// <summary>
        /// Downloads the requested song file from the server to the client.
        /// </summary>
        /// <param name="id"> The song identification number. </param>
        /// <param name="songFileType"> File type - chords file, original midi file or playback file. </param>
        /// <returns> The requested file for the given song. </returns>
        public async Task <ActionResult> DownloadFile(int?id, SongFileType songFileType)
        {
            // assure valid input params
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

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

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

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

            // build path for the requested file resource on the file server
            string filePath = await GetSongPath(song.Id, songFileType);

            // read file contents
            byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

            // return file content to the client
            string fileName = Path.GetFileName(filePath);

            return(File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName));
        }
Example #3
0
        public static bool IsFileMetadataValid(HttpPostedFileBase fileHandler, SongFileType fileType, out string errorMessage)
        {
            // initialize
            errorMessage = null;

            // validate that the file is not empty
            if (fileHandler.ContentLength == 0)
            {
                errorMessage = "File Cannot be Empty";
                return(false);
            }

            // validate that the file is not too big
            if (fileHandler.ContentLength > FileMaxLength)
            {
                errorMessage = $"File Size {fileHandler.ContentLength} " +
                               $"Exceeds Max Length of {FileMaxLength} Bytes";
                return(false);
            }

            // validate that the file is from the correct MIME type
            string[] validMimeTypes;
            if (fileType == SongFileType.ChordProgressionFile) // Chords file
            {
                validMimeTypes = new[] { "text/plain" }
            }
            ;
            else // MIDI file
            {
                validMimeTypes = new[] { "audio/midi", "audio/x-midi", "audio/mid", "audio/x-mid" }
            };

            if (!validMimeTypes.Contains(fileHandler.ContentType))
            {
                string validTypesToken = string.Empty;
                foreach (string mimeType in validMimeTypes)
                {
                    validTypesToken += mimeType + " ";
                }
                errorMessage =
                    $"File MIME Type {fileHandler.ContentType} Is Not Valid." +
                    $"Expected Format Type are one of the following: {validTypesToken}";
                return(false);
            }

            // if we got this far then hopefully everything is okay
            return(true);
        }
Example #4
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            _songViewModel = validationContext.ObjectInstance as SongViewModel;

            _midiFileHandler   = _songViewModel?.MidiFileHandler;
            _chordsFileHandler = _songViewModel?.ChordsFileHandler;
            _melodyTrackIndex  = _songViewModel?.MelodyTrackIndex;


            // nothing to check if no file is uploaded for current file handler property
            if (value == null)
            {
                return(ValidationResult.Success);
            }
            else
            {
                _fileHandler = value as HttpPostedFileBase;
            }

            // set the subjected validated file type according to the subject file handler
            if (_fileHandler == _midiFileHandler)
            {
                _songFileType = SongFileType.MidiOriginalFile;
            }
            else
            {
                _songFileType = SongFileType.ChordProgressionFile;
            }

            // validate file metadata (size, mime type, etc)
            if (!FileUploadValidation.IsFileMetadataValid(_fileHandler, _songFileType, out _errorMessage))
            {
                return(new ValidationResult(_errorMessage));
            }

            // fetch chords data from new chords file or from existing db record
            if (_chordsFileHandler != null)
            {
                if (!IsChordsFileValid(_chordsFileHandler, _songFileType, out _bars, out _errorMessage))
                {
                    return(new ValidationResult(_errorMessage));
                }
            }

            // validations for MIDI progression file
            if (_songFileType == SongFileType.MidiOriginalFile)
            {
                if (!FileUploadValidation.IsMidiFileValid(_midiFileHandler, out _midi, out _errorMessage))
                {
                    return(new ValidationResult(_errorMessage));
                }
            }

            // aditional validation for midi file and midi vs chords file
            if (_fileHandler == _midiFileHandler)
            {
                // validate melody track index is not out of bounds
                if (!CompositionContext.IsMelodyTrackIndexValid((int?)_melodyTrackIndex, _midi, out _errorMessage))
                {
                    return(new ValidationResult(_errorMessage));
                }

                // validate that bars in CHORD progression are compatible with MIDI file
                if (!CompositionContext.AreBarsCompatible(_bars, _midi, out _errorMessage))
                {
                    return(new ValidationResult(_errorMessage));
                }
            }

            // if we got this far then hopefully everything is okay
            return(ValidationResult.Success);
        }
 protected bool Equals(SongFileType other)
 {
     return(string.Equals(Name, other.Name) && string.Equals(Extension, other.Extension));
 }