// POST api/File public HttpResponseMessage PostFile(File file) { var db = ServicesContext.Current; if (file != null) { db.Files.Add(file); db.SaveChanges(); return new HttpResponseMessage(HttpStatusCode.Created); } else { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("File id does not match") }); } }
/** * Handle uploaded files * IEnumerable<File> */ public Task<HttpResponseMessage> PostFiles() { logger.Debug("starting to process incoming files."); if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = System.Web.HttpContext.Current.Server.MapPath("~/uploads"); string rootUrl = Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.AbsolutePath, String.Empty); logger.Debug("saving files to location: " + root); logger.Debug(" and the root url = " + rootUrl); var provider = new MultipartFormDataStreamProvider(root); User me = AuthorizationManager.getCurrentUser(); var db = ServicesContext.Current; var task = Request.Content.ReadAsMultipartAsync(provider). ContinueWith<HttpResponseMessage>(o => { if (o.IsFaulted || o.IsCanceled) { logger.Debug("Error: " + o.Exception.Message); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, o.Exception)); } //Look up our project Int32 ProjectId = Convert.ToInt32(provider.FormData.Get("ProjectId")); logger.Debug("And we think the projectid === " + ProjectId); Project project = db.Projects.Find(ProjectId); if (project == null) throw new Exception("Project ID not found: " + ProjectId); //TODO: collaborators? //security check :: you can only edit your own projects if (project.Owner.Id != me.Id) { throw new Exception("NotAuthorized: You can only edit projects you own."); } //Now iterate through the files that just came in List<File> files = new List<File>(); foreach (MultipartFileData file in provider.FileData) { logger.Debug("Filename = " + file.LocalFileName); logger.Debug("Orig = " + file.Headers.ContentDisposition.FileName); logger.Debug("Name? = " + file.Headers.ContentDisposition.Name); //var fileIndex = getFileIndex(file.Headers.ContentDisposition.Name); //"uploadedfile0" -> 0 var fileIndex = "0"; logger.Debug("Fileindex = " + fileIndex); var filename = file.Headers.ContentDisposition.FileName; filename = filename.Replace("\"", string.Empty); if (!String.IsNullOrEmpty(filename)) { try { var newFileName = relocateProjectFile( file.LocalFileName, ProjectId, filename); var info = new System.IO.FileInfo(newFileName); File newFile = new File(); newFile.Title = provider.FormData.Get("Title_" + fileIndex); //"Title_1, etc. logger.Debug("Title = " + newFile.Title); newFile.Description = provider.FormData.Get("Description_" + fileIndex); //"Description_1, etc. logger.Debug("Desc = " + newFile.Description); newFile.Name = info.Name;//.Headers.ContentDisposition.FileName; newFile.Link = rootUrl + "/services/uploads/" + ProjectId + "/" + info.Name; //file.LocalFileName; newFile.Size = (info.Length / 1024).ToString(); //file.Headers.ContentLength.ToString(); newFile.FileTypeId = FileType.getFileTypeFromFilename(info); newFile.UserId = me.Id; logger.Debug(" Adding file " + newFile.Name + " at " + newFile.Link); files.Add(newFile); } catch (Exception e) { logger.Debug("Error: " + e.ToString()); } } } //Add files to database for this project. if (files.Count() > 0) { logger.Debug("woot -- we have file objects to save"); foreach (var file in files) { project.Files.Add(file); } db.Entry(project).State = EntityState.Modified; db.SaveChanges(); } logger.Debug("Done saving files."); //TODO: actual error/success message handling string result = "{message: 'Success'}"; HttpResponseMessage resp = new HttpResponseMessage(System.Net.HttpStatusCode.OK); resp.Content = new System.Net.Http.StringContent(result, System.Text.Encoding.UTF8, "text/plain"); return resp; }); return task; }
// PUT api/File/5 public HttpResponseMessage PutFile(File file) { var db = ServicesContext.Current; if (file == null) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("File not valid") }); } db.Entry(file).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { throw new HttpResponseException(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest, Content = new StringContent("Database error.") }); } HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Created); //response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = file.Id })); return response; }
// PUT api/file/5 public HttpResponseMessage Put(File file) { return _fileRepository.PutFile(file); }