public HttpResponseMessage Upload() { HttpResponseMessage result = null; var httpRequest = HttpContext.Current.Request; string username = httpRequest.Params.Get("username"); string token = httpRequest.Params.Get("token"); if (!ValidPermission(username, token)) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Permission Denied")); } if (httpRequest.ContentLength > 5002000) // >= 5MB + req { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Maximum size is 5MB")); } if (httpRequest.Files.Count > 0) { try { var postedFile = httpRequest.Files[0]; IImageInfo img = WebManager.GetImageInfoV2(postedFile); img.Path = physicalPath + "\\" + img.FileName; if (!File.Exists(img.Path)) { img.Save(); result = Request.CreateResponse(HttpStatusCode.Created, "Created Successfull"); LogAction(username, token, ActionType.Create, img.Path); } else { result = Request.CreateResponse(HttpStatusCode.OK, "Existed image"); } } catch (Exception e) { result = Request.CreateResponse(HttpStatusCode.InternalServerError, e.ToString()); } } else { result = Request.CreateResponse(HttpStatusCode.BadRequest, "No Content"); } return(result); }
public HttpResponseMessage Update(string fileName) { HttpResponseMessage result = null; var httpRequest = HttpContext.Current.Request; string username = httpRequest.Params.Get("username"); string token = httpRequest.Params.Get("token"); if (!ValidPermission(username, token)) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Permission Denied")); } if (httpRequest.ContentLength > 5000000) // >= 5MB { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } bool isExist = false; string fileDir = ""; fileDir = physicalPath + "\\" + fileName; if (File.Exists(fileDir)) { isExist = true; File.Delete(fileDir); if (httpRequest.Files.Count > 0) { try { var postedFile = httpRequest.Files[0]; var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName); //postedFile.SaveAs(filePath); //docfiles.Add(filePath); IImageInfo img = WebManager.GetImageInfoV2(postedFile); img.Save(physicalPath + "/" + img.FileName); } catch (Exception e) { result = Request.CreateResponse(HttpStatusCode.InternalServerError, e.ToString()); } } else { result = Request.CreateResponse(HttpStatusCode.BadRequest); } } if (!isExist) { result = Request.CreateResponse(HttpStatusCode.OK, "Not exist"); } else { LogAction(username, token, ActionType.Update, fileDir); result = Request.CreateResponse(HttpStatusCode.Created, "Updated successfully"); } return(result); }