예제 #1
0
        public async Task <FileSystemInfoContract> MoveItemAsync(RootName root, FileSystemId source, string moveName, DirectoryId destination, Func <FileSystemInfoLocator> locatorResolver)
        {
            // Emulate MoveItem through CopyItem + RemoveItem

            var context = await RequireContextAsync(root);

            var sourceObjectId      = new StorageObjectId(source.Value);
            var destinationObjectId = new StorageObjectId(destination.Value);
            var directorySource     = source as DirectoryId;

            if (directorySource != null)
            {
                moveName += Path.AltDirectorySeparatorChar;
            }

            var item = await retryPolicy.ExecuteAsync(() => context.Client.CopyObjectAsync(sourceObjectId.Bucket, sourceObjectId.Path, destinationObjectId.Bucket, $"{destinationObjectId.Path}{moveName}"));

            if (directorySource != null)
            {
                var subDestination = new DirectoryId(item.Id);
                foreach (var subItem in await GetChildItemAsync(root, directorySource))
                {
                    await retryPolicy.ExecuteAsync(() => MoveItemAsync(root, subItem.Id, subItem.Name, subDestination, locatorResolver));
                }
            }

            await retryPolicy.ExecuteAsync(() => context.Client.DeleteObjectAsync(sourceObjectId.Bucket, sourceObjectId.Path));

            return(item.ToFileSystemInfoContract());
        }
예제 #2
0
        public async Task <DirectoryInfoContract> NewDirectoryItemAsync(RootName root, DirectoryId parent, string name)
        {
            var context = await RequireContextAsync(root);

            var parentObjectId = new StorageObjectId(parent.Value);
            var item           = await retryPolicy.ExecuteAsync(() => context.Client.UploadObjectAsync(parentObjectId.Bucket, $"{parentObjectId.Path}{name}/", null, new MemoryStream()));

            return(new DirectoryInfoContract(item.Id, item.Name.Substring(parentObjectId.Path.Length), new DateTimeOffset(item.TimeCreated.Value), new DateTimeOffset(item.Updated.Value)));
        }
예제 #3
0
        public async Task <bool> ClearContentAsync(RootName root, FileId target, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContextAsync(root);

            var targetObjectId = new StorageObjectId(target.Value);
            await retryPolicy.ExecuteAsync(() => context.Client.UploadObjectAsync(targetObjectId.Bucket, targetObjectId.Path, MIME_TYPE_FILE, new MemoryStream()));

            return(true);
        }
예제 #4
0
        public async Task <Stream> GetContentAsync(RootName root, FileId source)
        {
            var context = await RequireContextAsync(root);

            var sourceObjectId = new StorageObjectId(source.Value);
            var stream         = new MemoryStream();
            await context.Client.DownloadObjectAsync(sourceObjectId.Bucket, sourceObjectId.Path, stream);

            stream.Seek(0, SeekOrigin.Begin);
            return(stream);
        }
예제 #5
0
        public async Task <bool> SetContentAsync(RootName root, FileId target, Stream content, IProgress <ProgressValue> progress, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContextAsync(root);

            var targetObjectId        = new StorageObjectId(target.Value);
            var uploadProgress        = progress != null ? new UploadProgress(progress, (int)content.Length) : null;
            var retryPolicyWithAction = Policy.Handle <GoogleApiException>().WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                                                               (ex, ts) => content.Seek(0, SeekOrigin.Begin));
            await retryPolicyWithAction.ExecuteAsync(() => context.Client.UploadObjectAsync(targetObjectId.Bucket, targetObjectId.Path, MIME_TYPE_FILE, content, progress: uploadProgress));

            return(true);
        }
예제 #6
0
        public async Task <bool> SetContentAsync(RootName root, FileId target, Stream content, IProgress <ProgressValue> progress, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContextAsync(root);

            var targetObjectId = new StorageObjectId(target.Value);

            var uploadProgress = progress != null ? new UploadProgress(progress, (int)content.Length) : null;

            await context.Client.UploadObjectAsync(targetObjectId.Bucket, targetObjectId.Path, MIME_TYPE_FILE, content, progress : uploadProgress);

            return(true);
        }
예제 #7
0
        public async Task <IEnumerable <FileSystemInfoContract> > GetChildItemAsync(RootName root, DirectoryId parent)
        {
            var context = await RequireContextAsync(root);

            var parentObjectId = new StorageObjectId(parent.Value);
            var items          = await retryPolicy.ExecuteAsync(() => context.Client.ListObjectsAsync(parentObjectId.Bucket, parentObjectId.Path).Where(i =>
            {
                var childName = i.Name.Substring(parentObjectId.Path.Length).TrimEnd(Path.AltDirectorySeparatorChar);
                return(!string.IsNullOrEmpty(childName) && !childName.Contains(Path.AltDirectorySeparatorChar));
            }).ToList());

            return(items.Select(i => i.ToFileSystemInfoContract()));
        }
예제 #8
0
        public async Task <Stream> GetContentAsync(RootName root, FileId source)
        {
            var context = await RequireContextAsync(root);

            var sourceObjectId        = new StorageObjectId(source.Value);
            var stream                = new ProducerConsumerStream();
            var retryPolicyWithAction = Policy.Handle <GoogleApiException>().WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                                                               (ex, ts) => stream.Reset());
            await retryPolicyWithAction.ExecuteAsync(() => context.Client.DownloadObjectAsync(sourceObjectId.Bucket, sourceObjectId.Path, stream));

            stream.Flush();

            return(stream);
        }
예제 #9
0
        public async Task <FileSystemInfoContract> CopyItemAsync(RootName root, FileSystemId source, string copyName, DirectoryId destination, bool recurse)
        {
            if (source is DirectoryId)
            {
                throw new NotSupportedException(Properties.Resources.CopyingOfDirectoriesNotSupported);
            }

            var context = await RequireContextAsync(root);

            var sourceObjectId      = new StorageObjectId(source.Value);
            var destinationObjectId = new StorageObjectId(destination.Value);
            var item = await retryPolicy.ExecuteAsync(() => context.Client.CopyObjectAsync(sourceObjectId.Bucket, sourceObjectId.Path, destinationObjectId.Bucket, $"{destinationObjectId.Path}{copyName}"));

            return(item.ToFileSystemInfoContract());
        }
예제 #10
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 parentObjectId        = new StorageObjectId(parent.Value);
            var uploadProgress        = progress != null ? new UploadProgress(progress, (int)content.Length) : null;
            var retryPolicyWithAction = Policy.Handle <GoogleApiException>().WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                                                               (ex, ts) => content.Seek(0, SeekOrigin.Begin));
            var item = await retryPolicyWithAction.ExecuteAsync(() => context.Client.UploadObjectAsync(parentObjectId.Bucket, $"{parentObjectId.Path}{name}", MIME_TYPE_FILE, content, progress: uploadProgress));

            return(new FileInfoContract(item.Id, item.Name.Substring(parentObjectId.Path.Length), new DateTimeOffset(item.TimeCreated.Value), new DateTimeOffset(item.Updated.Value), (FileSize)(long)item.Size.Value, item.Md5Hash));
        }
예제 #11
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 parentObjectId = new StorageObjectId(parent.Value);

            var uploadProgress = progress != null ? new UploadProgress(progress, (int)content.Length) : null;

            var item = await context.Client.UploadObjectAsync(parentObjectId.Bucket, $"{parentObjectId.Path}{name}", MIME_TYPE_FILE, content, progress : uploadProgress);

            return(new FileInfoContract(item.Id, item.Name.Substring(parentObjectId.Path.Length), new DateTimeOffset(item.TimeCreated.Value), new DateTimeOffset(item.Updated.Value), (long)item.Size.Value, item.Md5Hash));
        }
예제 #12
0
        public async Task <bool> RemoveItemAsync(RootName root, FileSystemId target, bool recurse)
        {
            var context = await RequireContextAsync(root);

            var targetObjectId = new StorageObjectId(target.Value);

            if (recurse)
            {
                foreach (var childItem in await GetChildItemAsync(root, (DirectoryId)target))
                {
                    await retryPolicy.ExecuteAsync(() => RemoveItemAsync(root, childItem.Id, childItem is DirectoryInfoContract));
                }
            }

            await retryPolicy.ExecuteAsync(() => context.Client.DeleteObjectAsync(targetObjectId.Bucket, targetObjectId.Path));

            return(true);
        }