public async Task <HttpResponseMessage> AddPackages(string sessionGuid) { if (!SessionManager.SessionExists(sessionGuid)) { // Session doesn't exist. return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid session.")); } try { // Does the request contain multipart/form-data? if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } // Get the api key from the header. string apiKey = Request.Headers.GetValues("x-api-key").FirstOrDefault(); // Get the api user. APIUser apiUser = APIUserManager.FindAndPrepare(apiKey); // Receive files. MultipartMemoryStreamProvider provider = await Request.Content.ReadAsMultipartAsync(); foreach (HttpContent file in provider.Contents) { EventLogManager.Log("httpcontent filename: ", EventLogSeverity.Warning, "hard coded", null); //EventLogManager.Log("httpcontent filename: ", EventLogSeverity.Warning, file.Headers.ContentDisposition.FileName.ToString(), null); //string filename = file.Headers.ContentDisposition.FileName.Replace("\"", ""); string filename = "DonorsTrust_Install.zip"; using (MemoryStream ms = new MemoryStream(await file.ReadAsByteArrayAsync())) { EventLogManager.Log("MemoryStream ms length: ", EventLogSeverity.Warning, ms.Length.ToString(), null); EventLogManager.Log("apiUser.EncryptionKey: ", EventLogSeverity.Warning, apiUser.EncryptionKey.ToString(), null); using (Stream ds = Crypto.Decrypt(ms, apiUser.EncryptionKey)) { SessionManager.AddPackage(sessionGuid, ds, filename); } } } } catch (Exception ex) { EventLogManager.Log("REMOTE_EXCEPTION", EventLogSeverity.Warning, null, ex); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } return(Request.CreateResponse(HttpStatusCode.Created)); }
public async Task <HttpResponseMessage> AddPackage(string guid) { if (!SessionManager.SessionExists(guid)) { // Session doesn't exist. return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid session.")); } try { // Does the request contain multipart/form-data? if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } // Receive files. MultipartMemoryStreamProvider provider = await Request.Content.ReadAsMultipartAsync(); // TODO: Add filtering so that non .zip archives are not added. foreach (HttpContent file in provider.Contents) { string filename = file.Headers.ContentDisposition.FileName.Replace("\"", ""); using (MemoryStream ms = new MemoryStream(await file.ReadAsByteArrayAsync())) { SessionManager.AddPackage(guid, ms, filename); } } } catch (Exception ex) { EventLogManager.Log("SESSION_EXCEPTION", EventLogSeverity.Warning, null, ex); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } return(Request.CreateResponse(HttpStatusCode.Created)); }