Ejemplo n.º 1
0
/*
 * For a Post collect alle Transport- and Metadata for a file,
 * save the file to temp and store filepath and collected Data.
 */
        private void ProcessPost(HttpRequest request, string set)
        {
            DateTime dateTime = DateTime.Now;

            string headers = string.Join("|", Request.Headers.Select(x => String.Format("{0}={1}", x.Key, x.Value)).ToArray());

            string headerFingerprint = "";

            foreach (var header in Request.Headers)
            {
                if (header.Key == "User-Agent" || header.Key == "Accept-Encoding" || header.Key == "Accept")
                {
                    headerFingerprint += String.Format("{0}={1}|", header.Key, header.Value);
                }
            }

            headerFingerprint = CalculateHash(headerFingerprint);

            string ipAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();

            Dictionary <string, string> geoInfo = GetGeoInfo(ipAddress);

            geoInfo.TryGetValue("country", out string country);
            geoInfo.TryGetValue("regionName", out string regionName);
            geoInfo.TryGetValue("city", out string city);
            geoInfo.TryGetValue("lat", out string lat);
            geoInfo.TryGetValue("lon", out string lon);
            geoInfo.TryGetValue("isp", out string isp);

            var files = Request.Form.Files;

            List <FileEntryItem> fileEntrys = new List <FileEntryItem>();

            foreach (var file in files)
            {
                // full path to file in temp location
                var filePath = Path.GetTempFileName();

                if (file.Length > 0)
                {
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                }
                fileEntrys.Add(new FileEntryItem(set, file.FileName, filePath, file.Length, ipAddress, headers, headerFingerprint, dateTime, country, regionName, city, lat, lon, isp));
            }

            _context.FileEntryItems.AddRange(fileEntrys);
            _context.SaveChanges();
        }
Ejemplo n.º 2
0
        private static void WriteFileToDb(ZipInputStream fileFromMemory)
        {
            MemoryStream unzippedFileMemoryStream = new MemoryStream();

            byte[] buffer = new byte[4096];

            using (unzippedFileMemoryStream)
            {
                StreamUtils.Copy(fileFromMemory, unzippedFileMemoryStream, buffer);

                using (var sr = new StreamReader(unzippedFileMemoryStream, System.Text.Encoding.UTF8))
                {
                    unzippedFileMemoryStream.Seek(0, SeekOrigin.Begin);
                    string fileContent = sr.ReadToEnd();
                    using (var context = new FileEntryContext())
                    {
                        context.FileEntries.Add(new FileEntry()
                        {
                            Content = fileContent
                        });
                        context.SaveChanges();
                    }
                }
            }
        }