private static string GetHistoryContentJson(JObject apiResponse)
        {
            string content_string;

            var content_struct = new CrmHistoryContent
            {
                @from        = apiResponse.Value <String>("from"),
                to           = apiResponse.Value <String>("to"),
                cc           = apiResponse.Value <String>("cc"),
                bcc          = apiResponse.Value <String>("bcc"),
                subject      = apiResponse.Value <String>("subject"),
                important    = apiResponse.Value <Boolean>("important"),
                chain_id     = apiResponse.Value <String>("chainId"),
                is_sended    = apiResponse.Value <Int32>("folder") == 1,
                date_created = apiResponse.Value <String>("date"),
                introduction = apiResponse.Value <String>("introduction"),
                message_id   = apiResponse.Value <Int32>("id")
            };

            var serializer = new DataContractJsonSerializer(typeof(CrmHistoryContent));

            using (var stream = new System.IO.MemoryStream())
            {
                serializer.WriteObject(stream, content_struct);
                content_string = Encoding.UTF8.GetString(stream.GetCorrectBuffer());
            }
            //JsonConvert.SerializeObject
            return(content_string);
        }
        private static string GetHistoryContentJson(MailMessageItem item)
        {
            string content_string;

            var content_struct = new CrmHistoryContent
                {
                    message_id = item.Id
                };

            var serializer = new DataContractJsonSerializer(typeof (CrmHistoryContent));
            using (var stream = new MemoryStream())
            {
                serializer.WriteObject(stream, content_struct);
                content_string = Encoding.UTF8.GetString(stream.GetCorrectBuffer());
            }
            return content_string;
        }
        private static string GetHistoryContentJson(JObject apiResponse)
        {
            string content_string;

            var content_struct = new CrmHistoryContent
                {
                    @from = apiResponse.Value<String>("from"),
                    to = apiResponse.Value<String>("to"),
                    cc = apiResponse.Value<String>("cc"),
                    bcc = apiResponse.Value<String>("bcc"),
                    subject = apiResponse.Value<String>("subject"),
                    important = apiResponse.Value<Boolean>("important"),
                    chain_id = apiResponse.Value<String>("chainId"),
                    is_sended = apiResponse.Value<Int32>("folder") == 1,
                    date_created = apiResponse.Value<String>("date"),
                    introduction = apiResponse.Value<String>("introduction"),
                    message_id = apiResponse.Value<Int32>("id")
                };

            var serializer = new DataContractJsonSerializer(typeof(CrmHistoryContent));
            using (var stream = new System.IO.MemoryStream())
            {
                serializer.WriteObject(stream, content_struct);
                content_string = Encoding.UTF8.GetString(stream.GetCorrectBuffer());
            }
            //JsonConvert.SerializeObject
            return content_string;
        }
Example #4
0
        private void UploadToCdn()
        {
            try
            {
                // one thread only
                if (Interlocked.CompareExchange(ref work, 1, 0) == 0)
                {
                    var @continue = false;
                    try
                    {
                        CdnItem item;
                        if (queue.TryDequeue(out item))
                        {
                            @continue = true;

                            var cdnpath = GetCdnPath(item.Bundle.Path);
                            var key = new Uri(cdnpath).PathAndQuery.TrimStart('/');
                            var contentMD5 = Hasher.Base64Hash(item.Response.Content, HashAlg.MD5);
                            var compressed = new MemoryStream();

                            if (ClientSettings.GZipEnabled)
                            {
                                using (var compression = new GZipStream(compressed, CompressionMode.Compress, true))
                                {
                                    new MemoryStream(Encoding.UTF8.GetBytes(item.Response.Content)).CopyTo(compression);
                                }
                                contentMD5 = Hasher.Base64Hash(compressed.GetCorrectBuffer(), HashAlg.MD5);
                            }

                            var config = new AmazonS3Config
                            {
                                RegionEndpoint = RegionEndpoint.GetBySystemName(s3region),
                                UseHttp = true
                            };
                            using (var s3 = new AmazonS3Client(s3publickey, s3privatekey, config))
                            {
                                var upload = false;
                                try
                                {
                                    var request = new GetObjectMetadataRequest
                                        {
                                        BucketName = s3bucket,
                                        Key = key
                                    };
                                    var response = s3.GetObjectMetadata(request);
                                    upload = !string.Equals(contentMD5, response.Metadata["x-amz-meta-etag"], StringComparison.InvariantCultureIgnoreCase);
                                }
                                catch (AmazonS3Exception ex)
                                {
                                    if (ex.StatusCode == HttpStatusCode.NotFound)
                                    {
                                        upload = true;
                                    }
                                    else
                                    {
                                        throw;
                                    }
                                }

                                if (upload)
                                {
                                    var request = new PutObjectRequest
                                    {
                                        BucketName = s3bucket,
                                        CannedACL = S3CannedACL.PublicRead,
                                        Key = key,
                                        ContentType = AmazonS3Util.MimeTypeFromExtension(Path.GetExtension(key).ToLowerInvariant())
                                    };

                                    if (ClientSettings.GZipEnabled)
                                    {
                                        request.InputStream = compressed;
                                        request.Headers.ContentEncoding = "gzip";
                                    }
                                    else
                                    {
                                        request.ContentBody = item.Response.Content;
                                    }

                                    var cache = TimeSpan.FromDays(365);
                                    request.Headers.CacheControl = string.Format("public, maxage={0}", (int) cache.TotalSeconds);
                                    request.Headers.Expires = DateTime.UtcNow.Add(cache);
                                    request.Headers.ContentMD5 = contentMD5;
                                    request.Headers["x-amz-meta-etag"] = contentMD5;
                                    //request.AddHeader("Last-Modified", DateTime.UtcNow.ToString("R"));

                                    s3.PutObject(request);
                                }

                                item.Bundle.CdnPath = cdnpath;
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        log.Error(err);
                    }
                    finally
                    {
                        work = 0;
                        if (@continue)
                        {
                            Action upload = () => UploadToCdn();
                            upload.BeginInvoke(null, null);
                        }
                    }
                }
            }
            catch (Exception fatal)
            {
                log.Fatal(fatal);
            }
        }