Example #1
0
        /// <inheritdoc />
        public bool Download(FileInfoMessage data, Action <long, long> progress = null, Func <bool> 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);
        }
Example #2
0
        /// <inheritdoc />
        public Guid?UploadTemp(string fileName, byte[] body, Action <long, long> progress = null, Func <bool> 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);
        }
Example #3
0
        /// <inheritdoc />
        public FileInfoMessage Upload(string fileName, byte[] body, bool isPublic, Action <long> progress = null, Func <bool> cancel = null)
        {
            if (fileName.IsEmpty())
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            var hash = GetHash(body);

            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);
        }
Example #4
0
        private long?Upload(Guid operationId, FileInfoMessage file, Action <long, long> progress, Func <bool> 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, body.Length);
            }

            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);
        }
Example #5
0
        /// <inheritdoc />
        public bool DownloadTemp(FileInfoMessage data, Guid operationId, Action <long, long> progress = null, Func <bool> 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);
        }
Example #6
0
        /// <inheritdoc />
        public void Update(FileInfoMessage data, Action <long, long> progress = null, Func <bool> 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);
        }