示例#1
0
        /// <inheritdoc />
        public Task CopyImageStoreContentAsync(
            ImageStoreCopyDescription imageStoreCopyDescription,
            long?serverTimeout = 60,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            imageStoreCopyDescription.ThrowIfNull(nameof(imageStoreCopyDescription));
            serverTimeout?.ThrowIfOutOfInclusiveRange("serverTimeout", 1, 4294967295);
            var requestId   = Guid.NewGuid().ToString();
            var url         = "ImageStore/$/Copy";
            var queryParams = new List <string>();

            // Append to queryParams if not null.
            serverTimeout?.AddToQueryParameters(queryParams, $"timeout={serverTimeout}");
            queryParams.Add("api-version=6.0");
            url += "?" + string.Join("&", queryParams);

            string content;

            using (var sw = new StringWriter())
            {
                ImageStoreCopyDescriptionConverter.Serialize(new JsonTextWriter(sw), imageStoreCopyDescription);
                content = sw.ToString();
            }

            HttpRequestMessage RequestFunc()
            {
                var request = new HttpRequestMessage()
                {
                    Method  = HttpMethod.Post,
                    Content = new StringContent(content, Encoding.UTF8),
                };

                request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
                return(request);
            }

            return(this.httpClient.SendAsync(RequestFunc, url, requestId, cancellationToken));
        }
        /// <inheritdoc />
        public async Task CopyImageStoreContentAsync(
            ImageStoreCopyDescription imageStoreCopyDescription,
            long?serverTimeout = 60,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            imageStoreCopyDescription.ThrowIfNull(nameof(imageStoreCopyDescription));

            await this.LoadImageStoreConnectionString();

            if (this.isLocalStore)
            {
                var source      = Path.Combine(this.imageStorePath, imageStoreCopyDescription.RemoteSource);
                var destination = Path.Combine(this.imageStorePath, imageStoreCopyDescription.RemoteDestination);

                FileAttributes attr = File.GetAttributes(source);
                if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    var files = Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories)
                                .Select(file => new System.IO.FileInfo(file));

                    foreach (var file in files)
                    {
                        var targetPath = Path.Combine(this.imageStorePath, Path.Combine(destination, file.FullName.Substring(source.Length + 1)));
                        if (!Directory.Exists(Path.GetDirectoryName(targetPath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
                        }

                        File.Copy(file.FullName, targetPath, true);
                    }
                }
                else
                {
                    File.Copy(source, destination, true);
                }
            }
            else
            {
                serverTimeout?.ThrowIfOutOfInclusiveRange("serverTimeout", 1, 4294967295);
                var requestId   = Guid.NewGuid().ToString();
                var url         = "ImageStore/$/Copy";
                var queryParams = new List <string>();

                // Append to queryParams if not null.
                serverTimeout?.AddToQueryParameters(queryParams, $"timeout={serverTimeout}");
                queryParams.Add("api-version=6.0");
                url += "?" + string.Join("&", queryParams);

                string content;
                using (var sw = new StringWriter())
                {
                    ImageStoreCopyDescriptionConverter.Serialize(new JsonTextWriter(sw), imageStoreCopyDescription);
                    content = sw.ToString();
                }

                HttpRequestMessage RequestFunc()
                {
                    var request = new HttpRequestMessage()
                    {
                        Method  = HttpMethod.Post,
                        Content = new StringContent(content, Encoding.UTF8),
                    };

                    request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
                    return(request);
                }

                await this.httpClient.SendAsync(RequestFunc, url, requestId, cancellationToken);
            }
        }