#region S# License /****************************************************************************************** NOTICE!!! This program and source code is owned and licensed by StockSharp, LLC, www.stocksharp.com Viewing or use of this code requires your acceptance of the license agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE Removal of this comment is a violation of the license agreement. Project: StockSharp.Community.Community File: FileClient.cs Created: 2015, 11, 11, 2:32 PM Copyright 2010 by StockSharp, LLC *******************************************************************************************/ #endregion S# License namespace StockSharp.Community { using System; using System.Collections.Generic; using System.Linq; using Ecng.Collections; using Ecng.Common; using Ecng.IO; using MoreLinq; using StockSharp.Community.Messages; using StockSharp.Localization; /// /// The client for access to the service of work with files and documents. /// public class FileClient : BaseCommunityClient, IFileClient { private readonly CachedSynchronizedDictionary _cache = new CachedSynchronizedDictionary(); /// /// Initializes a new instance of the . /// public FileClient() : this("https://stocksharp.com/services/fileservice.svc".To()) { } /// /// Initializes a new instance of the . /// /// Service address. public FileClient(Uri address) : base(address, "file") { } /// public bool Compression { get; set; } = true; /// public bool CheckDownloadedHash { get; set; } /// public int PartSize { get; set; } = 40 * 1024; // 40kb /// public FileInfoMessage GetFile(long id, Action progress = null, Func cancel = null) { var data = GetFileInfo(id); Download(data, progress, cancel); return data; } /// public FileInfoMessage GetFileInfo(long id) { return _cache.SafeAdd(id, key => Invoke(f => f.GetFileInfo2(NullableSessionId ?? Guid.Empty, id))); } /// public bool Download(FileInfoMessage data, Action progress = null, Func cancel = null) { if (data == null) throw new ArgumentNullException(nameof(data)); if (data.Body != null) return true; var tuple = Invoke(f => f.BeginDownload2(SessionId, data.Id, Compression)); var body = Download(tuple.Item1, tuple.Item2, true, tuple.Item3, progress, cancel); if (body == null) return false; data.Body = body; return true; } /// public bool DownloadTemp(FileInfoMessage data, Guid operationId, Action progress = null, Func cancel = null) { if (data == null) throw new ArgumentNullException(nameof(data)); var body = Download(operationId, data.BodyLength, false, data.Hash, progress, cancel); if (body == null) return false; data.Body = body; return true; } private byte[] Download(Guid operationId, long size, bool checkHash, string hash, Action progress, Func cancel) { if (operationId == Guid.Empty) throw new ArgumentNullException(nameof(operationId)); var list = new List(); while (list.Count < size) { if (cancel?.Invoke() == true) { Invoke(f => f.FinishDownload(operationId, true)); return null; } var part = Invoke(f => f.ProcessDownload2(operationId, list.Count, PartSize)); if (Compression) part = part.DeflateFrom(); list.AddRange(part); progress?.Invoke(list.Count); } Invoke(f => f.FinishDownload(operationId, false)); var body = list.ToArray(); if (checkHash && CheckDownloadedHash && !hash.IsEmpty()) { var calc = body.Hash(); if (!hash.CompareIgnoreCase(calc)) throw new InvalidOperationException(LocalizedStrings.FileHashNotMatchKey.Put(hash, calc)); } return body; } /// public void Update(FileInfoMessage data, Action progress = null, Func cancel = null) { if (data == null) throw new ArgumentNullException(nameof(data)); if (data.Id == 0) throw new ArgumentException(nameof(data)); var hash = data.Body.Hash(); var operationId = Invoke(f => f.BeginUploadExisting2(SessionId, data.Id, Compression, hash)); Upload(operationId, data, progress, cancel); } /// public FileInfoMessage Upload(string fileName, byte[] body, bool isPublic, Action progress = null, Func cancel = null) { if (fileName.IsEmpty()) throw new ArgumentNullException(nameof(fileName)); var hash = body.Hash(); var operationId = Invoke(f => f.BeginUpload2(SessionId, fileName, isPublic, Compression, hash)); var data = new FileInfoMessage { FileName = fileName, Body = body, BodyLength = body.LongLength, IsPublic = isPublic, CreationDate = DateTime.UtcNow, Hash = hash, }; var id = Upload(operationId, data, progress, cancel); return id == null ? null : data; } /// public Guid? UploadTemp(string fileName, byte[] body, Action progress = null, Func cancel = null) { if (fileName.IsEmpty()) throw new ArgumentNullException(nameof(fileName)); var hash = body.Hash(); var operationId = Invoke(f => f.BeginUploadTemp(SessionId, fileName, Compression, hash)); var data = new FileInfoMessage { FileName = fileName, Body = body, BodyLength = body.LongLength, CreationDate = DateTime.UtcNow, Hash = hash, }; var id = Upload(operationId, data, progress, cancel); if (id == null) return null; return operationId; } private long? Upload(Guid operationId, FileInfoMessage file, Action progress, Func cancel) { if (file == null) throw new ArgumentNullException(nameof(file)); var sentCount = 0L; var body = file.Body; if (Compression) body = body.DeflateTo(); foreach (var part in body.Batch(PartSize)) { if (cancel?.Invoke() == true) { Invoke(f => f.FinishUpload(operationId, true)); return null; } var arr = part.ToArray(); ValidateError(Invoke(f => f.ProcessUpload(operationId, arr))); sentCount += arr.Length; progress?.Invoke(sentCount); } var id = Invoke(f => f.FinishUpload(operationId, false)); if (id < 0) ValidateError((byte)-id); // temp upload if (id == 0) return 0; if (file.Id == 0) file.Id = id; _cache.TryAdd(id, file); return id; } /// public long GetUploadLimit() { return Invoke(f => f.GetUploadLimit(SessionId)); } /// public string Share(long id) { return Invoke(f => f.Share(SessionId, id)); } /// public void UnShare(long id) { Invoke(f => f.UnShare(SessionId, id)); } private static void ValidateError(byte errorCode) { ((ErrorCodes)errorCode).ThrowIfError(); } } }