private async Task <SelectelClient> GetClient()
        {
            var client = new SelectelClient();

            await client.AuthorizeAsync(_authUser, _authPwd);

            return(client);
        }
Пример #2
0
        private SelectelClient GetClient()
        {
            var client = new SelectelClient();

            client.AuthorizeAsync(_authUser, _authPwd).Wait();

            return(client);
        }
Пример #3
0
        private async Task <SelectelClient> Authorize()
        {
            var configSection = _configuration.GetSection("SelectelStorage");
            var apiKey        = configSection["ApiKey"];
            var user          = configSection["User"];

            var client = new SelectelClient();

            await client.AuthorizeAsync(user, apiKey);

            return(client);
        }
        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 path    = key.Remove(0, _container.Length + 1);

                            var content     = Encoding.UTF8.GetBytes(item.Response.Content);
                            var inputStream = new MemoryStream();

                            if (ClientSettings.GZipEnabled)
                            {
                                using (var zip = new GZipStream(inputStream, CompressionMode.Compress, true))
                                {
                                    zip.Write(content, 0, content.Length);
                                    zip.Flush();
                                }
                            }
                            else
                            {
                                inputStream.Write(content, 0, content.Length);
                            }

                            inputStream.Position = 0;

                            bool upload = true;

                            var client = new SelectelClient();

                            client.AuthorizeAsync(_authUser, _authPwd).Wait();

                            var etag = SelectelSharp.Common.Helpers.CalculateSHA1(item.Response.Content);

                            var fileInfo = client.GetContainerFilesAsync(_container, 1, null, path, null, null).Result;

                            if (fileInfo != null && fileInfo.Any())
                            {
                                upload = fileInfo.Single().Hash != etag;
                            }

                            if (upload)
                            {
                                var contentType = String.Empty;

                                var mime = string.IsNullOrEmpty(contentType) ? MimeMapping.GetMimeMapping(Path.GetFileName(key))
                             : contentType;

                                var customHeaders = new Dictionary <string, object>();

                                if (ClientSettings.GZipEnabled)
                                {
                                    customHeaders.Add("Content-Encoding", "gzip");
                                }

                                var cache = TimeSpan.FromDays(365);

                                customHeaders.Add("Cache-Control", String.Format("public, maxage={0}", (int)cache.TotalSeconds));
                                customHeaders.Add("Expires", DateTime.UtcNow.Add(cache));

                                client.UploadFileAsync(_container, path, true, true, inputStream, etag, null, mime, null, null, customHeaders).Wait();
                            }
                            else
                            {
                                inputStream.Close();
                            }

                            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);
            }
        }