Exemplo n.º 1
0
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            if (content.Length == 0)
            {
                return(new ProxyFileInfoContract(name));
            }

            var context = await RequireContextAsync(root);

            var objectId = parent.GetObjectId(name);
            var length   = content.Length;

            var item = default(SwiftResponse);

            if (length <= LARGE_FILE_THRESHOLD)
            {
                var stream = progress != null ? new ProgressStream(content, progress) : content;
                item = await AsyncFunc.RetryAsync <SwiftResponse, Exception>(async() => await context.Client.PutObject(context.Container, objectId, stream), RETRIES);

                if (!item.IsSuccess)
                {
                    throw new ApplicationException(item.Reason);
                }
            }
            else
            {
                item = await AsyncFunc.RetryAsync <SwiftResponse, Exception>(async() => await ChunkedUpload(context, objectId, content, progress), RETRIES);
            }

            var creationTime = DateTime.Parse(item.Headers["Date"]);

            return(new FileInfoContract(objectId, name, creationTime, creationTime, length, item.Headers["ETag"]));
        }
Exemplo n.º 2
0
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            if (content.Length == 0)
            {
                return(new ProxyFileInfoContract(name));
            }

            var context = await RequireContextAsync(root);

            var objectId = parent.GetObjectId(name);
            var length   = content.Length;

            var item = default(SwiftResponse);
            var retryPolicyWithAction = Policy.Handle <Exception>().WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                                                      (ex, ts) => content.Seek(0, SeekOrigin.Begin));

            if (length <= LargeFileThreshold)
            {
                var stream = progress != null ? new ProgressStream(content, progress) : content;
                item = await retryPolicyWithAction.ExecuteAsync(() => context.Client.PutObject(context.Container, objectId, stream));

                if (!item.IsSuccess)
                {
                    throw new ApplicationException(item.Reason);
                }
            }
            else
            {
                item = await retryPolicyWithAction.ExecuteAsync(() => ChunkedUpload(context, objectId, content, progress));
            }

            var creationTime = DateTime.Parse(item.Headers["Date"]);

            return(new FileInfoContract(objectId, name, creationTime, creationTime, (FileSize)length, item.Headers["ETag"]));
        }
Exemplo n.º 3
0
        public async Task <DirectoryInfoContract> NewDirectoryItemAsync(RootName root, DirectoryId parent, string name)
        {
            var context = await RequireContextAsync(root);

            var objectId = parent.GetObjectId(name);

            var item = await context.Client.PutPseudoDirectory(context.Container, objectId);

            if (!item.IsSuccess)
            {
                throw new ApplicationException(item.Reason);
            }

            var creationTime = DateTime.Parse(item.Headers["Date"]);

            return(new DirectoryInfoContract(objectId, name, creationTime, creationTime));
        }
Exemplo n.º 4
0
        public async Task <FileSystemInfoContract> CopyItemAsync(RootName root, FileSystemId source, string copyName, DirectoryId destination, bool recurse)
        {
            var context = await RequireContextAsync(root);

            if (source is DirectoryId)
            {
                throw new NotSupportedException(Properties.Resources.CopyingOfDirectoriesNotSupported);
            }

            var targetName = !string.IsNullOrEmpty(copyName) ? copyName : source.GetName();
            var targetId   = destination.GetObjectId(targetName);

            await context.Client.CopyObject(context.Container, source.Value, context.Container, targetId);

            var item = await context.Client.HeadObject(context.Container, targetId);

            var creationTime = DateTime.Parse(item.Headers["Date"]);

            return(new FileInfoContract(targetId, targetName, creationTime, creationTime, item.ContentLength, item.Headers["ETag"]));
        }