public async Task <System.Net.Http.HttpResponseMessage> PutFile(int id, string resource) { string username = ""; if (HttpContext != null && HttpContext.Request != null) { username = ProductsController.getUsername(HttpContext.Request.Headers["Authorization"]); } if (string.IsNullOrEmpty(username)) { username = ProductsController.getUsername2(HttpContext.Request.Headers["Cookie"]); } if (username != Startup.Configuration["adminUser"]) { return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); } int index = resource.IndexOf('/'); string profileName = resource.Substring(index + 1); string filename = String.Format("log-{0}.txt", profileName); if (Request != null && Request.Query.ContainsKey("filename")) { filename = Request.Query["filename"].ToString(); } bool append = false; if (Request != null && Request.Query.ContainsKey("append")) { append = Request.Query["append"].ToString() == "true"; } ProductsRepo repo = new ProductsRepo(); if (repo.GetOne(id) == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } bool history = repo.GetOne(id).UploadHistory; if (filename.EndsWith(".zip")) { history = false; } try { string body = ""; using (var reader = new StreamReader(Request.Body)) { body = await reader.ReadToEndAsync(); } StringReader sr = new StringReader(body); System.Net.Mail.MailMessage mm = Amende.Snorre.MailMessageMimeParser.ParseMessage(sr); string datedir = DateTime.Now.ToString("MMddyyyy"); System.IO.Directory.CreateDirectory("tmp/" + id); if (history) { System.IO.Directory.CreateDirectory("tmp/" + id + "/" + datedir); } Console.WriteLine("count=" + mm.Attachments.Count); if (mm.Attachments.Count > 0) { using (var fileStream = System.IO.File.Open(String.Format(@"{0}/{1}/{2}", "tmp", id, filename), append ? FileMode.Append : FileMode.Create)) { using (MemoryStream ms = new MemoryStream()) { mm.Attachments[0].ContentStream.CopyTo(ms); ms.Position = 0; ms.CopyTo(fileStream); if (append || !checkHash(ms, id, filename)) { history = false; //System.Console.WriteLine("dup hash for " + filename); } } } if (history) { System.IO.File.Copy(String.Format(@"{0}/{1}/{2}", "tmp", id, filename), String.Format(@"{0}/{1}/{2}/{3}_{4}", "tmp", id, datedir, DateTime.Now.Ticks, filename)); } System.IO.Directory.CreateDirectory("wwwroot/WebApp/logs/" + id); System.IO.File.WriteAllText("wwwroot/WebApp/logs/" + id + "/" + filename + "_date.txt", DateTime.Now.ToString()); } else { using (StreamWriter sw = System.IO.File.CreateText("tmp/" + id + "/" + filename)) { sw.Write(mm.Body); } } } catch (Exception ex) { Console.WriteLine("exception" + ex.Message); } return(new HttpResponseMessage(HttpStatusCode.Created)); }
public IActionResult Put(int id, [FromBody] Product p) { bool update = false; if (Request != null && Request.Query.ContainsKey("update")) { update = Request.Query["update"].ToString() == "true"; } bool uploadhist = false; if (Request != null && Request.Query.ContainsKey("uploadhist")) { uploadhist = Request.Query["uploadhist"].ToString() == "true"; } string username = ""; if (HttpContext != null && HttpContext.Request != null) { username = getUsername(HttpContext.Request.Headers["Authorization"]); } if (string.IsNullOrEmpty(username)) { username = getUsername2(HttpContext.Request.Headers["Cookie"]); } if (username == Startup.Configuration["readonlyUser"] && (string.IsNullOrEmpty(p.Command) || p.Command != "getProperties") ) { return(Unauthorized()); } if (!string.IsNullOrEmpty(p.Command)) { System.Console.WriteLine(p.Command); } ProductsRepo repo = new ProductsRepo(); if (repo.GetOne(id) == null) { return(NotFound(p)); } string ret = "good"; repo.SetOne(id, p, HttpContext.Connection.RemoteIpAddress.ToString()); Product product = repo.GetOne(id); //TODO: use set method instead of updating product if (update) { bool doSave = p.Version != product.Version; if (string.IsNullOrEmpty(p.Status)) { product.Percent = p.Percent; product.IP = p.IP; } product.Version = p.Version; product.OS = p.OS; product.Profile = p.Profile; if (doSave) { repo.Save(); } } else if (!string.IsNullOrEmpty(p.Command)) { //increment the cmd number if (string.IsNullOrEmpty(product.Command) || !product.Command.Contains(":")) { product.Command = "1:" + p.Command; } else { int i = 1 + System.Convert.ToInt32(product.Command.Substring(0, product.Command.IndexOf(':'))); product.Command = i.ToString() + ":" + p.Command; } ret = product.Command; //don't update lastReport time if received command from UI return(Ok(product)); } else if (!string.IsNullOrEmpty(p.Response)) { product.Response = p.Response; } else if (!string.IsNullOrEmpty(p.Output)) { product.setOutput(p.Output); } else if (!string.IsNullOrEmpty(p.Desc)) { product.Desc = p.Desc; repo.Save(); } else if (!string.IsNullOrEmpty(p.Name)) { if (username != Startup.Configuration["adminUser"]) { return(Unauthorized()); } product.Name = p.Name; writeIds(); } else if (uploadhist) { Console.WriteLine("UploadHistory " + p.UploadHistory); product.UploadHistory = p.UploadHistory; //repo.Save(); } else { product.Percent = p.Percent; } product.IP2 = HttpContext.Connection.RemoteIpAddress.ToString(); product.LastReport = DateTime.Now; string ip = GetHeaderValueAs <string>("X-Forwarded-For"); //Console.WriteLine("forwarded from "+ip); if (!string.IsNullOrEmpty(ip)) { product.IP2 = ip; } repo.AddOne(0, new Product() { Id = 0, Name = "" }); Product product0 = repo.GetOne(0); if (product0 != null) { product0.LastReport = DateTime.Now; } //must return existing product NOT incoming product, so that we're returning latest command return(Ok(product)); }