Esempio n. 1
0
        public static BlobStream ReadStreamResponse(this WebRequest request)
        {
            var regx = new Regex("filename=\"(?<fn>[^\"]+)\"");

            using (var response = request.GetResponse())
            {
                var match = regx.Match(response.Headers["content-disposition"]);
                var bs    = new BlobStream(match.Groups["fn"].Value, response.Headers["Content-Type"]);
                using (var stream = response.GetResponseStream())
                {
                    stream.CopyTo(bs);
                }
                return(bs);
            }
        }
Esempio n. 2
0
        static void Import(string token, string importdir, List <KeyValuePair <string, byte[]> > files)
        {
            string apiUrl = config["apiUrl"];

            if (Directory.Exists(importdir))
            {
                files.Clear();
                foreach (var filepath in Directory.GetFiles(importdir, "*.csetw", SearchOption.AllDirectories))
                {
                    var buffer = File.ReadAllBytes(filepath);
                    files.Add(new KeyValuePair <string, byte[]>(Path.GetFileName(filepath), buffer));
                }
            }

            var req = new WebRequestOptions
            {
                UriString = $"{apiUrl}{Urls.import}",
                Headers   = new Dictionary <string, string> {
                    { "Authorization", token }
                }
            };

            foreach (var file in files)
            {
                var filename = file.Key;
                var buffer   = file.Value;
                using (var bs = new BlobStream(filename, "application/octet-stream"))
                {
                    bs.Write(buffer, 0, buffer.Length);
                    var content = new Dictionary <string, object> {
                        { "file", bs }
                    };
                    using (var data = MimeMultipartStream.FromContent(content))
                    {
                        try
                        {
                            var resp = req.Post(new HttpPostPayload
                            {
                                ContentType = data.ContentType,
                                Data        = data.Buffer
                            });
                            var log = $"{filename} | {resp}";
                            Console.WriteLine(log);
                            Log.Information(log);
                        }
                        catch (WebException ex)
                        {
                            using (var stream = ex.Response.GetResponseStream())
                            {
                                using (var reader = new StreamReader(stream))
                                {
                                    var json = reader.ReadToEnd();
                                    var err  = JObject.Parse(json);
                                    var log  = $"{filename} | \"Failed to import assessment\"";
                                    Console.WriteLine(log);
                                    Log.Error(ex, log);
                                    Log.Error("Server-Message: {0}", err.Value <string>("Message"));
                                    Log.Error("Server-ExceptionType: {0}", err.Value <string>("ExceptionType"));
                                    Log.Error("Server-ExceptionMessage: {0}", err.Value <string>("ExceptionMessage"));
                                    Log.Error("Server-StackTrace: {0}", err.Value <string>("StackTrace"));
                                }
                            }
                        }
                    }
                }
            }
        }