Ejemplo n.º 1
0
        public IActionResult Delete(int agentid, string dirname)
        {
            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(Unauthorized());
            }

            try
            {
                Directory.Delete("tmp/" + agentid + "/" + dirname, true);
                return(Ok(0));
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return(StatusCode(500));
            }
        }
Ejemplo n.º 2
0
        public IActionResult Get()
        {
            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(Unauthorized());
            }

            List <string> dirs = new List <string>();

            foreach (var v in Directory.EnumerateDirectories("tmp"))
            {
                dirs.Add(v.Substring(1 + v.IndexOf('/')));
            }

            return(Ok(dirs));
        }
Ejemplo n.º 3
0
        new public ActionResult File(string id, string agentid, string filename)
        {
            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["readonlyUser"])
            {
                return(Unauthorized());
            }

            try
            {
                //System.Console.WriteLine(id + " " + agentid + " " + filename);
                return(new FileStreamResult(new FileStream("tmp/" + agentid + "/" + filename, FileMode.Open),
                                            filename.EndsWith(".zip") ? "application/zip" : "image/jpeg"));
            }
            catch (Exception)
            {
                return(NotFound());
            }
        }
Ejemplo n.º 4
0
        public ActionResult Files(string agentid, string id)
        {
            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(Unauthorized());
            }

            try
            {
                createCSV(agentid, id);
                System.IO.File.Delete("/tmp/test.zip");
                System.IO.Compression.ZipFile.CreateFromDirectory("./tmp/" + agentid + "/" + id + "/", "/tmp/test.zip",
                                                                  System.IO.Compression.CompressionLevel.Optimal, true);

                return(new PhysicalFileResult("/tmp/test.zip", "application/zip"));
            }
            catch (Exception ex)
            {
                return(Ok(ex.Message));
            }

            //return Ok(agentid + " " + id);
        }
Ejemplo n.º 5
0
        public IActionResult Get(int id)
        {
            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(Unauthorized());
            }

            List <Dirs> dirs = new List <Dirs>();

            foreach (var v in Directory.EnumerateDirectories("tmp/" + id))
            {
                var files = Directory.EnumerateFiles(v);

                HashSet <string> dict = new HashSet <string>();
                foreach (var file in files)
                {
                    string key   = file.Substring(file.IndexOf('_') + 1);
                    int    index = key.LastIndexOf('/');
                    if (index != -1)
                    {
                        key = key.Substring(index + 1);
                    }

                    if (!dict.Contains(key))
                    {
                        dict.Add(key);
                    }
                }
                string s = "";
                foreach (var dic in dict)
                {
                    s += dic + ' ';
                }

                dirs.Add(
                    new Dirs()
                {
                    Name  = v.Substring(1 + v.LastIndexOf('/')),
                    Size  = files.Count().ToString(),
                    Files = s
                }
                    );
            }

            dirs.Sort(new DirsSorter());

            return(Ok(dirs));
        }
Ejemplo n.º 6
0
        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));
        }