public IHttpActionResult PostBrainWaveFile(BrainWaveFileUpload brainWaveFileUpload)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (brainWaveFileUpload.FileType == null)
            {
                brainWaveFileUpload.FileType = "";
            }

            if (!brainWaveFileUpload.FileType.StartsWith(".") && brainWaveFileUpload.FileType != String.Empty)
            {
                brainWaveFileUpload.FileType = "." + brainWaveFileUpload.FileType;
            }

            BrainWaveFile oldBrainWaveFile = _db.Files.Find(brainWaveFileUpload.Id);

            if (brainWaveFileUpload.Id != oldBrainWaveFile.Id)
            {
                return BadRequest();
            }

            File.Delete(GetFilePath(oldBrainWaveFile));

            oldBrainWaveFile.FileType = brainWaveFileUpload.FileType.ToLower();
            oldBrainWaveFile.AuthorId = brainWaveFileUpload.AuthorId;
            oldBrainWaveFile.AuthorName = brainWaveFileUpload.AuthorName;
            oldBrainWaveFile.DisplayName = brainWaveFileUpload.DisplayName;
            oldBrainWaveFile.Description = brainWaveFileUpload.Description;
            oldBrainWaveFile.DateUploaded = DateTime.Now;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BrainWaveFileExists(oldBrainWaveFile.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            var fileContents = brainWaveFileUpload.Body.ToCharArray();
            byte[] fileBytes = new byte[fileContents.Length];

            for (int i = 0;i < fileContents.Length;i++)
            {
                fileBytes[i] = (byte)fileContents[i];
            }

            var uploadFileInfo = new FileInfo(GetFilePath(oldBrainWaveFile));
            uploadFileInfo.Directory.Create();
            var uploadedFile = uploadFileInfo.Create();
            uploadedFile.Write(fileBytes, 0, fileBytes.Length);
            uploadedFile.Close();

            return CreatedAtRoute("DefaultApi", new { id = oldBrainWaveFile.Id }, oldBrainWaveFile);
        }
        public IHttpActionResult PutBrainWaveFile(BrainWaveFileUpload brainWaveFileUpload)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var brainWaveFile = new BrainWaveFile
            {
                FileType = brainWaveFileUpload.FileType.ToLower(),
                AuthorId = brainWaveFileUpload.AuthorId,
                AuthorName = brainWaveFileUpload.AuthorName,
                DisplayName = brainWaveFileUpload.DisplayName,
                Description = brainWaveFileUpload.Description,
                DateUploaded = DateTime.Now
            };

            if (brainWaveFile.FileType == null)
            {
                brainWaveFile.FileType = "";
            }

            if (!brainWaveFile.FileType.StartsWith(".") && brainWaveFile.FileType != String.Empty)
            {
                brainWaveFile.FileType = "." + brainWaveFile.FileType;
            }

            brainWaveFile = _db.Files.Add(brainWaveFile);

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (BrainWaveFileExists(brainWaveFile.Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            var fileContents = brainWaveFileUpload.Body.ToCharArray();
            byte[] fileBytes = new byte[fileContents.Length];

            for (int i = 0; i < fileContents.Length; i++)
            {
                fileBytes[i] = (byte)fileContents[i];
            }

            var uploadFileInfo = new FileInfo("Files/" + brainWaveFile.Id + brainWaveFile.FileType);
            uploadFileInfo.Directory.Create();
            var uploadedFile = uploadFileInfo.Create();
            uploadedFile.Write(fileBytes, 0, fileBytes.Length);
            uploadedFile.Close();

            return CreatedAtRoute("DefaultApi", new { id = brainWaveFile.Id }, brainWaveFile);
        }