public HttpResponseMessage Upload([FromBody] UploadedFile uploadedFile) { using (var operation = this.TelClient.StartOperation <RequestTelemetry>("Upload")) { if (uploadedFile == null || string.IsNullOrWhiteSpace(uploadedFile.Url) || string.IsNullOrWhiteSpace(uploadedFile.Name)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "The file to be uploaded is empty")); } var fileShareName = WebConfigurationManager.AppSettings["fileShareName"]; // the incoming url will look like this: // "azure://<accountname>:<accesskey>@file.core.windows.net/<sharename>/<filename>" var urlSplit = uploadedFile.Url.Split('/'); if (urlSplit.Length < 2 || !urlSplit[urlSplit.Length - 2].Equals(fileShareName, StringComparison.InvariantCultureIgnoreCase) || !urlSplit[urlSplit.Length - 1].Equals(uploadedFile.Name)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Request's Url does not match request's Name or does not contain expected file share name")); } string ocmeData; var fileName = urlSplit[urlSplit.Length - 1]; try { fileName = CopyFileToBlob(fileName); ocmeData = BlobDal.DownloadBlob(fileName); operation.Telemetry.ResponseCode = HttpStatusCode.OK.ToString(); operation.Telemetry.Url = Request.RequestUri; ParseAndUpload(ocmeData); BlobDal.MoveFileToProcessed(fileName); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { BlobDal.MoveFileToPoison(fileName); return(HandleExceptions(ex, operation, Request)); } } }
public async Task <HttpResponseMessage> Upload() { var groups = UserDal.GetGroupsFromHeader(Request); if (!groups.Contains("Admin")) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, "You do not have permission to upload files.")); } using (var operation = this.TelClient.StartOperation <RequestTelemetry>("UploadFromSite")) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new MultipartMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.Contents) { string filename = null; try { operation.Telemetry.ResponseCode = HttpStatusCode.OK.ToString(); operation.Telemetry.Url = Request.RequestUri; var buffer = await file.ReadAsByteArrayAsync(); var contents = System.Text.Encoding.UTF8.GetString(buffer); filename = BlobDal.UploadFromText("ocme" + DateTime.Now.ToString(@"yyyy-MM-dd"), contents); ParseAndUpload(contents); BlobDal.MoveFileToProcessed(filename); } catch (Exception ex) { if (!string.IsNullOrEmpty(filename)) { BlobDal.MoveFileToPoison(filename); } return(HandleExceptions(ex, operation, Request)); } } return(Request.CreateResponse(HttpStatusCode.OK, "Success")); } }