private static long ToId(DirectoryId folderId)
        {
            if (!folderId.Value.StartsWith("d", StringComparison.Ordinal))
                throw new FormatException(string.Format(CultureInfo.InvariantCulture, Resources.InvalidFolderId, folderId));

            return long.Parse(folderId.Value.Substring(1), NumberStyles.Number);
        }
Example #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 file = new GoogleFile()
            {
                Name = name, MimeType = MIME_TYPE_FILE, Parents = new[] { parent.Value }
            };
            var insert = context.Service.Files.Create(file, content, MIME_TYPE_FILE).AsFile();

            if (progress != null)
            {
                insert.ProgressChanged += p => progress.Report(new ProgressValue((int)p.BytesSent, (int)content.Length));
            }
            var upload = await AsyncFunc.RetryAsync <IUploadProgress, GoogleApiException>(async() => await insert.UploadAsync(), RETRIES);

            var item = insert.ResponseBody;

            return(new FileInfoContract(item.Id, item.Name, new DateTimeOffset(item.CreatedTime.Value), new DateTimeOffset(item.ModifiedTime.Value), item.Size.Value, item.Md5Checksum));
        }
Example #3
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 item           = default(Item);
            var requestBuilder = context.Client.Drive.Items[parent.Value].ItemWithPath(name);

            if (content.Length <= LargeFileThreshold)
            {
                var stream = progress != null ? new ProgressStream(content, progress) : content;
                var retryPolicyWithAction = Policy.Handle <ServiceException>().WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                                                                 (ex, ts) => content.Seek(0, SeekOrigin.Begin));
                item = await retryPolicyWithAction.ExecuteAsync(() => requestBuilder.Content.Request().PutAsync <Item>(stream));
            }
            else
            {
                var session = await requestBuilder.CreateSession().Request().PostAsync();

                var provider = new ChunkedUploadProvider(session, context.Client, content);

                item = await ChunkedUploadAsync(provider, progress);
            }

            return(new FileInfoContract(item.Id, item.Name, item.CreatedDateTime ?? DateTimeOffset.FromFileTime(0), item.LastModifiedDateTime ?? DateTimeOffset.FromFileTime(0), (FileSize)(item.Size ?? -1), item.File.Hashes.Sha1Hash.ToLowerInvariant()));
        }
Example #4
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"]));
        }
        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());
        }
Example #6
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"]));
        }
        public IEnumerable <FileSystemInfoContract> GetChildItem(RootName root, DirectoryId parent)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }

            var effectivePath = GetFullPath(root, parent.Value);

            var directory = new DirectoryInfo(effectivePath);

            if (directory.Exists)
            {
                return(directory.EnumerateDirectories().Select(d => new DirectoryInfoContract(GetRelativePath(root, d.FullName), d.Name, d.CreationTime, d.LastWriteTime)).Cast <FileSystemInfoContract>().Concat(
                           directory.EnumerateFiles().Select(f => new FileInfoContract(GetRelativePath(root, f.FullName), f.Name, f.CreationTime, f.LastWriteTime, f.Length, null)).Cast <FileSystemInfoContract>()));
            }
            else
            {
                return new FileSystemInfoContract[] { }
            };
        }
Example #8
0
        public IEnumerable <FileSystemInfoContract> GetChildItem(RootName root, DirectoryId parent)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (string.IsNullOrEmpty(rootPath))
            {
                throw new InvalidOperationException($"{nameof(rootPath)} not initialized".ToString(CultureInfo.CurrentCulture));
            }

            var effectivePath = GetFullPath(rootPath, parent.Value);

            var directory = new DirectoryInfo(effectivePath);

            if (directory.Exists)
            {
                return(directory.EnumerateDirectories().Select(d => new DirectoryInfoContract(GetRelativePath(rootPath, d.FullName), d.Name, d.CreationTime, d.LastWriteTime)).Cast <FileSystemInfoContract>().Concat(
                           directory.EnumerateFiles().Select(f => new FileInfoContract(GetRelativePath(rootPath, f.FullName), f.Name, f.CreationTime, f.LastWriteTime, f.Length, null)).Cast <FileSystemInfoContract>()));
            }
            else
            {
                return(Array.Empty <FileSystemInfoContract>());
            }
        }
        public Mock <IAsyncCloudGateway> InitializeGetChildItemsAsyncWithFilter(RootName rootName, string path, string filter, params IEnumerable <FileSystemInfoContract>[] pathChildItems)
        {
            var gatewayMock = new Mock <IAsyncCloudGateway>(MockBehavior.Strict);

            gatewayMock.SetupSequence(g => g.GetRootAsync(rootName, FileSystemFixture.ApiKey))
            .ReturnsAsync(FileSystemFixture.Root)
            .ThrowsAsync(new InvalidOperationException("Redundant call to GetRoot()"));
            gatewayMock.SetupSequence(g => g.GetDriveAsync(rootName, FileSystemFixture.ApiKey))
            .ReturnsAsync(FileSystemFixture.Drive)
            .Throws(new InvalidOperationException("Redundant call to GetDrive()"));

            if (path != null)
            {
                path = path + Path.DirectorySeparatorChar;
                for (int i = 0, j = 0; i >= 0; i = path.IndexOf(Path.DirectorySeparatorChar, Math.Min(path.Length, i + 2)), ++j)
                {
                    var currentPath = new DirectoryId(path.Substring(0, Math.Max(1, i)));

                    bool applyFilter     = j == pathChildItems.Length - 1;
                    var  effectiveFilter = applyFilter && filter != null ? new Regex("^" + filter.Replace("*", ".*") + "$") : null;
                    gatewayMock.SetupSequence(g => g.GetChildItemAsync(rootName, currentPath))
                    .ReturnsAsync(applyFilter ? pathChildItems[j].Where(f => effectiveFilter == null || effectiveFilter.IsMatch(f.Name)) : pathChildItems[j])
                    .ThrowsAsync(new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Redundant access to {0}", currentPath)));
                }
            }

            return(gatewayMock);
        }
        public Mock<ICloudGateway> InitializeGetChildItemsWithFilter(RootName rootName, string path, string filter, params IEnumerable<FileSystemInfoContract>[] pathChildItems)
        {
            var gatewayMock = new Mock<ICloudGateway>(MockBehavior.Strict);
            gatewayMock.SetupSequence(g => g.GetRoot(rootName, FileSystemFixture.ApiKey))
                .Returns(FileSystemFixture.Root)
                .Throws(new InvalidOperationException(@"Redundant call to GetRoot()"));
            gatewayMock.SetupSequence(g => g.GetDrive(rootName, FileSystemFixture.ApiKey))
                .Returns(FileSystemFixture.Drive)
                .Throws(new InvalidOperationException(@"Redundant call to GetDrive()"));

            if (path != null) {
                path = path + Path.DirectorySeparatorChar;
                for (int i = 0, j = 0; i >= 0; i = path.IndexOf(Path.DirectorySeparatorChar, Math.Min(path.Length, i + 2)), ++j) {
                    var currentPath = new DirectoryId(path.Substring(0, Math.Max(1, i)));

                    bool applyFilter = j == pathChildItems.Length - 1;
                    var effectiveFilter = applyFilter && filter != null ? new Regex("^" + filter.Replace("*", ".*") + "$") : null;
                    gatewayMock.SetupSequence(g => g.GetChildItem(rootName, currentPath))
                        .Returns(applyFilter ? pathChildItems[j].Where(f => effectiveFilter == null || effectiveFilter.IsMatch(f.Name)) : pathChildItems[j])
                        .Throws(new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, @"Redundant access to {0}", currentPath)));
                }
            }

            return gatewayMock;
        }
Example #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 item           = default(Item);
            var requestBuilder = context.Client.Drive.Items[parent.Value].ItemWithPath(name);

            if (content.Length <= LARGE_FILE_THRESHOLD)
            {
                var stream = progress != null ? new ProgressStream(content, progress) : content;
                item = await AsyncFunc.RetryAsync <Item, ServiceException>(async() => await requestBuilder.Content.Request().PutAsync <Item>(stream), RETRIES);
            }
            else
            {
                var session = await requestBuilder.CreateSession().Request().PostAsync();

                var provider = new ChunkedUploadProvider(session, context.Client, content);

                item = await ChunkedUploadAsync(provider, progress, RETRIES);
            }

            return(new FileInfoContract(item.Id, item.Name, item.CreatedDateTime ?? DateTimeOffset.FromFileTime(0), item.LastModifiedDateTime ?? DateTimeOffset.FromFileTime(0), item.Size ?? -1, item.File.Hashes.Sha1Hash.ToLowerInvariant()));
        }
Example #12
0
        public DirectoryInfoContract NewDirectoryItem(RootName root, DirectoryId parent, string name)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (string.IsNullOrEmpty(rootPath))
            {
                throw new InvalidOperationException($"{nameof(rootPath)} not initialized".ToString(CultureInfo.CurrentCulture));
            }

            var effectivePath = GetFullPath(rootPath, Path.Combine(parent.Value, name));

            var directory = new DirectoryInfo(effectivePath);

            if (directory.Exists)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.DuplicatePath, effectivePath));
            }

            directory.Create();

            return(new DirectoryInfoContract(GetRelativePath(rootPath, directory.FullName), directory.Name, directory.CreationTime, directory.LastWriteTime));
        }
Example #13
0
        private void DoTestRenderer(PathTable pathTable, PipFragmentRenderer renderer, string expectedHash)
        {
            // StringId
            var strValue = "my string";

            XAssert.AreEqual(strValue, renderer.Render(PipFragment.FromString(strValue, pathTable.StringTable)));

            var pathStr   = A("t", "file1.txt");
            var path      = AbsolutePath.Create(pathTable, pathStr);
            var srcFile   = FileArtifact.CreateSourceFile(path);
            var outFile   = FileArtifact.CreateOutputFile(srcFile);
            var rwFile    = outFile.CreateNextWrittenVersion();
            var rw2File   = rwFile.CreateNextWrittenVersion();
            var opaqueDir = DirectoryArtifact.CreateDirectoryArtifactForTesting(path, 0);
            var sharedDir = new DirectoryArtifact(path, 1, isSharedOpaque: true);

            // AbsolutePath
            XAssert.AreEqual(pathStr, renderer.Render(PipFragment.FromAbsolutePathForTesting(path)));
            XAssert.AreEqual(pathStr, renderer.Render(PipFragment.FromAbsolutePathForTesting(srcFile)));
            XAssert.AreEqual(pathStr, renderer.Render(PipFragment.FromAbsolutePathForTesting(outFile)));
            XAssert.AreEqual(pathStr, renderer.Render(PipFragment.FromAbsolutePathForTesting(rwFile)));
            XAssert.AreEqual(pathStr, renderer.Render(PipFragment.FromAbsolutePathForTesting(rw2File)));

            // VsoHash
            XAssert.AreEqual(expectedHash, renderer.Render(PipFragment.VsoHashFromFileForTesting(srcFile)));
            XAssert.AreEqual(expectedHash, renderer.Render(PipFragment.VsoHashFromFileForTesting(outFile)));
            XAssert.AreEqual(expectedHash, renderer.Render(PipFragment.VsoHashFromFileForTesting(rwFile)));
            XAssert.AreEqual(expectedHash, renderer.Render(PipFragment.VsoHashFromFileForTesting(rw2File)));

            XAssert.AreEqual(DirectoryId.ToString(opaqueDir), renderer.Render(PipFragment.DirectoryIdForTesting(opaqueDir)));
            XAssert.AreEqual(DirectoryId.ToString(sharedDir), renderer.Render(PipFragment.DirectoryIdForTesting(sharedDir)));
        }
        public FileInfoContract NewFileItem(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            var effectivePath = GetFullPath(root, Path.Combine(parent.Value, name));

            var file = new FileInfo(effectivePath);

            if (file.Exists)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resource.DuplicatePath, parent.Value));
            }

            using (var fileStream = file.Create()) {
                if (content != null)
                {
                    content.CopyTo(fileStream);
                }
            }

            file.Refresh();
            return(new FileInfoContract(GetRelativePath(root, file.FullName), file.Name, file.CreationTime, file.LastWriteTime, file.Length, null));
        }
Example #15
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 file = new GoogleFile()
            {
                Title = name, MimeType = MIME_TYPE_FILE, Parents = new[] { new ParentReference()
                                                                           {
                                                                               Id = parent.Value
                                                                           } }
            };
            var insert = context.Service.Files.Insert(file, content, MIME_TYPE_FILE);

            if (progress != null)
            {
                insert.ProgressChanged += p => progress.Report(new ProgressValue((int)p.BytesSent, (int)content.Length));
            }
            var retryPolicyWithAction = Policy.Handle <GoogleApiException>().WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                                                               (ex, ts) => content.Seek(0, SeekOrigin.Begin));
            var upload = await retryPolicyWithAction.ExecuteAsync(() => insert.UploadAsync());

            var item = insert.ResponseBody;

            return(new FileInfoContract(item.Id, item.Title, new DateTimeOffset(item.CreatedDate.Value), new DateTimeOffset(item.ModifiedDate.Value), (FileSize)item.FileSize.Value, item.Md5Checksum));
        }
Example #16
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 config = await context.Agent.Upload.GetUploadConfiguration(WebUtility.UrlEncode(name), content.Length, parent.Value, MediaFireActionOnDuplicate.Skip);

            var mediaFireProgress = progress != null ? new Progress <MediaFireOperationProgress>(p => progress.Report(new ProgressValue((int)p.CurrentSize, (int)p.TotalSize))) : null;
            var upload            = await context.Agent.Upload.Simple(config, content, mediaFireProgress);

            while (!(upload.IsComplete && upload.IsSuccess))
            {
                await Task.Delay(100);

                upload = await context.Agent.Upload.PollUpload(upload.Key);
            }
            var item = await context.Agent.GetAsync <MediaFireGetFileInfoResponse>(MediaFireApiFileMethods.GetInfo, new Dictionary <string, object>() {
                [MediaFireApiParameters.QuickKey] = upload.QuickKey
            });

            return(new FileInfoContract(item.FileInfo.QuickKey, WebUtility.UrlDecode(item.FileInfo.Name), item.FileInfo.Created, item.FileInfo.Created, item.FileInfo.Size, item.FileInfo.Hash));
        }
 public bool Equals(DirectoryInfoEx other)
 {
     return(other != null &&
            DirectoryDbId.Equals(other.DirectoryDbId) &&
            DirectoryId.Equals(other.DirectoryId) &&
            EqualityComparer <DirectoryInfo> .Default.Equals(DirectoryInfo, other.DirectoryInfo) &&
            EqualityComparer <IEnumerable <Exception> > .Default.Equals(Exceptions, other.Exceptions));
 }
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var item = await AsyncFunc.Retry <FileSystem, ServerException>(async() => await context.Client.FileSystemManager.UploadNewFileStreamAsync(parent.Value, name, new ProgressStream(content, progress), true), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.DateLastSynced, FileSystemExtensions.Later(item.DateLastSynced, item.ModifiedTime), item.Size, null));
        }
        public FileInfoContract NewFileItem(RootName root, DirectoryId parent,
                                            string name, System.IO.Stream content, IProgress <ProgressValue> progress)
        {
            var contract = _localState.NewFileItem(root, parent, name, content, progress);

            _contentCache[contract.Id] = content.ReadFully();
            return(contract);
        }
Example #20
0
        private static long ToId(DirectoryId folderId)
        {
            if (!folderId.Value.StartsWith("d", StringComparison.Ordinal))
            {
                throw new FormatException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.InvalidFolderId, folderId));
            }

            return(long.Parse(folderId.Value.Substring(1), NumberStyles.Number));
        }
        public FileSystemInfoLocator(FileSystemInfoContract fileSystem)
        {
            if (fileSystem == null)
                throw new ArgumentNullException(nameof(fileSystem));

            Id = fileSystem.Id;
            Name = fileSystem.Name;
            ParentId = (fileSystem as DirectoryInfoContract)?.Parent.Id ?? (fileSystem as FileInfoContract)?.Directory.Id;
        }
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var tokenSource = new CancellationTokenSource();
            var item        = await AsyncFunc.Retry <pCloudFile, pCloudException>(async() => await context.Client.UploadFileAsync(new ProgressStream(content, progress), ToId(parent), name, tokenSource.Token), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.Created, item.Modified, item.Size, null));
        }
Example #23
0
            public FileInfoContract SetupNewFile(DirectoryId parentId, string fileName)
            {
                var file = new FileInfoContract($"{parentId.Value.TrimEnd('\\')}\\{fileName}".ToString(CultureInfo.CurrentCulture), fileName, "2016-02-01 12:00:00".ToDateTime(), "2016-02-01 12:00:00".ToDateTime(), FileSize.Empty, null);

                _drive
                .Setup(drive => drive.NewFileItem(It.Is <DirectoryInfoContract>(parent => parent.Id == parentId), fileName, It.Is <Stream>(s => s.Length == 0)))
                .Returns(file)
                .Verifiable();
                return(file);
            }
Example #24
0
        private GetSealedDirectoryContentCommand ReceiveGetSealedDirectoryContentCommandAndCheckItMatchesDirectoryId(string payload, string expectedDirectoryId)
        {
            var cmd = global::BuildXL.Ipc.ExternalApi.Commands.Command.Deserialize(payload);

            XAssert.AreEqual(typeof(GetSealedDirectoryContentCommand), cmd.GetType());
            var getSealedDirectoryCmd = (GetSealedDirectoryContentCommand)cmd;

            XAssert.AreEqual(expectedDirectoryId, DirectoryId.ToString(getSealedDirectoryCmd.Directory));
            return(getSealedDirectoryCmd);
        }
        public IEnumerable <FileSystemInfoContract> GetChildItem(RootName root, DirectoryId parent)
        {
            var context = RequireContext(root);

            var nodes      = context.Client.GetNodes();
            var parentItem = nodes.Single(n => n.Id == parent.Value);
            var items      = context.Client.GetNodes(parentItem);

            return(items.Select(i => i.ToFileSystemInfoContract()));
        }
        public DirectoryInfoContract NewDirectoryItem(RootName root, DirectoryId parent, string name)
        {
            var context = RequireContext(root);

            var nodes      = context.Client.GetNodes();
            var parentItem = nodes.Single(n => n.Id == parent.Value);
            var item       = context.Client.CreateFolder(name, parentItem);

            return(new DirectoryInfoContract(item.Id, item.Name, DateTimeOffset.MinValue, item.LastModificationDate));
        }
        public FileInfoContract NewFileItem(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            var context = RequireContext(root);

            var nodes      = context.Client.GetNodes();
            var parentItem = nodes.Single(n => n.Id == parent.Value);
            var item       = context.Client.Upload(new ProgressStream(content, progress), name, parentItem);

            return(new FileInfoContract(item.Id, item.Name, DateTimeOffset.MinValue, item.LastModificationDate, item.Size, null));
        }
Example #28
0
            public DirectoryInfoContract SetupNewDirectory(string parentName, string directoryName)
            {
                var parentId  = new DirectoryId(parentName);
                var directory = new DirectoryInfoContract($"{parentId.Value}{directoryName}\\".ToString(CultureInfo.CurrentCulture), directoryName, "2016-01-01 12:00:00".ToDateTime(), "2016-01-01 12:00:00".ToDateTime());

                _drive
                .Setup(drive => drive.NewDirectoryItem(It.Is <DirectoryInfoContract>(parent => parent.Id == parentId), directoryName))
                .Returns(directory)
                .Verifiable();
                return(directory);
            }
        /// <summary>
        /// Initializes a new instance of the <see cref="FileSystemInfoLocator"/> class.
        /// </summary>
        /// <param name="fileSystem">The file system info object.</param>
        /// <exception cref="ArgumentNullException">The filesystem is <c>null</c>.</exception>
        public FileSystemInfoLocator(FileSystemInfoContract fileSystem)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            Id       = fileSystem.Id;
            Name     = fileSystem.Name;
            ParentId = (fileSystem as DirectoryInfoContract)?.Parent.Id ?? (fileSystem as FileInfoContract)?.Directory.Id;
        }
Example #30
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 item = await AsyncFunc.RetryAsync <Item, OneDriveException>(async() => await context.Client.Drive.Items[parent.Value].ItemWithPath(name).Content.Request().PutAsync <Item>(content), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.CreatedDateTime ?? DateTimeOffset.FromFileTime(0), item.LastModifiedDateTime ?? DateTimeOffset.FromFileTime(0), item.Size ?? -1, item.File.Hashes.Sha1Hash.ToLowerInvariant()));
        }
Example #31
0
        public void TestValidDirectoryId(int pathId, uint partialSealId, bool isSharedOpaque)
        {
            // skip invalid input
            if (partialSealId == 0 && isSharedOpaque)
            {
                return;
            }

            var dir  = new DirectoryArtifact(new AbsolutePath(pathId), partialSealId, isSharedOpaque);
            var dir2 = DirectoryId.Parse(DirectoryId.ToString(dir));

            XAssert.AreEqual(dir, dir2);
        }
Example #32
0
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var itemReference = ODConnection.ItemReferenceForItemId(parent.Value, context.Drive.Id);
            var uploadOptions = progress != null ? new ItemUploadOptions()
            {
                ProgressReporter = (complete, transfered, total) => progress.Report(new ProgressValue(complete, (int)transfered, (int)total))
            } : ItemUploadOptions.Default;
            var item = await AsyncFunc.Retry <ODItem, ODException>(async() => await context.Connection.PutNewFileToParentItemAsync(itemReference, name, content, uploadOptions), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.CreatedDateTime, item.LastModifiedDateTime, item.Size, item.File.Hashes.Sha1.ToLowerInvariant()));
        }
Example #33
0
        public async Task GivenDirectoryEntry_WhenRoundTrip_Success()
        {
            DirectoryClient client = TestApplication.GetDirectoryClient();

            var documentId = new DocumentId("test/unit-tests/entry1");

            var query = new QueryParameter()
            {
                Filter    = "test/unit-tests",
                Recursive = false,
            };

            IReadOnlyList <DatalakePathItem> search = (await client.Search(query).ReadNext()).Records;

            if (search.Any(x => x.Name == (string)documentId))
            {
                await client.Delete(documentId);
            }

            var entry = new DirectoryEntryBuilder()
                        .SetDirectoryId(documentId)
                        .SetClassType("test")
                        .AddProperty(new EntryProperty {
                Name = "property1", Value = "value1"
            })
                        .Build();

            await client.Set(entry);

            search = (await client.Search(query).ReadNext()).Records;
            search.Any(x => x.Name == (string)documentId).Should().BeTrue();

            DirectoryEntry?readEntry = await client.Get(documentId);

            readEntry.Should().NotBeNull();

            readEntry !.DirectoryId.Should().Be(entry.DirectoryId);
            readEntry.ClassType.Should().Be(entry.ClassType);
            readEntry.ETag.Should().NotBeNull();
            readEntry.Properties.Count.Should().Be(1);

            readEntry.Properties.Values.First().Action(x =>
            {
                (x == entry.Properties.Values.First()).Should().BeTrue();
            });

            await client.Delete(documentId);

            search = (await client.Search(query).ReadNext()).Records;
            search.Any(x => x.Name == (string)documentId).Should().BeFalse();
        }
        public IEnumerable<FileSystemInfoContract> GetChildItem(RootName root, DirectoryId parent)
        {
            if (root == null)
                throw new ArgumentNullException(nameof(root));
            if (parent == null)
                throw new ArgumentNullException(nameof(parent));

            var effectivePath = GetFullPath(root, parent.Value);

            var directory = new DirectoryInfo(effectivePath);
            if (directory.Exists)
                return directory.EnumerateDirectories().Select(d => new DirectoryInfoContract(GetRelativePath(root, d.FullName), d.Name, d.CreationTime, d.LastWriteTime)).Cast<FileSystemInfoContract>().Concat(
                    directory.EnumerateFiles().Select(f => new FileInfoContract(GetRelativePath(root, f.FullName), f.Name, f.CreationTime, f.LastWriteTime, f.Length, null)).Cast<FileSystemInfoContract>());
            else
                return new FileSystemInfoContract[] { };
        }
        public async Task<DirectoryInfoContract> NewDirectoryItemAsync(RootName root, DirectoryId parent, string name)
        {
            var context = await RequireContext(root);

            var item = await AsyncFunc.Retry<Folder, pCloudException>(async () => await context.Client.CreateFolderAsync(ToId(parent), name), RETRIES);

            return new DirectoryInfoContract(item.Id, item.Name, item.Created, item.Modified);
        }
        public FileInfoContract NewFileItem(RootName root, DirectoryId parent, string name, Stream content, IProgress<ProgressValue> progress)
        {
            var context = RequireContext(root);

            var nodes = context.Client.GetNodes();
            var parentItem = nodes.Single(n => n.Id == parent.Value);
            var item = context.Client.Upload(new ProgressStream(content, progress), name, parentItem);

            return new FileInfoContract(item.Id, item.Name, DateTimeOffset.MinValue, item.LastModificationDate, item.Size, null);
        }
        public FileSystemInfoContract MoveItem(RootName root, FileSystemId source, string moveName, DirectoryId destination)
        {

            var context = RequireContext(root);

            var nodes = context.Client.GetNodes();
            var sourceItem = nodes.Single(n => n.Id == source.Value);
            if (!string.IsNullOrEmpty(moveName) && moveName != sourceItem.Name)
                throw new NotSupportedException(Resources.RenamingOfFilesNotSupported);
            var destinationParentItem = nodes.Single(n => n.Id == destination.Value);
            var item = context.Client.Move(sourceItem, destinationParentItem);

            return item.ToFileSystemInfoContract();
        }
        public IEnumerable<FileSystemInfoContract> GetChildItem(RootName root, DirectoryId parent)
        {
            var context = RequireContext(root);

            var nodes = context.Client.GetNodes();
            var parentItem = nodes.Single(n => n.Id == parent.Value);
            var items = context.Client.GetNodes(parentItem);

            return items.Select(i => i.ToFileSystemInfoContract());
        }
 public Task<FileSystemInfoContract> CopyItemAsync(RootName root, FileSystemId source, string copyName, DirectoryId destination, bool recurse)
 {
     return Task.FromException<FileSystemInfoContract>(new NotSupportedException(Resources.CopyingOfFilesOrDirectoriesNotSupported));
 }
        public async Task<DirectoryInfoContract> NewDirectoryItemAsync(RootName root, DirectoryId parent, string name)
        {
            var context = await RequireContext(root);

            var item = await AsyncFunc.Retry<FileSystem, ServerException>(async () => await context.Client.FileSystemManager.CreateNewFolderAsync(parent.Value, name, false), RETRIES);

            return new DirectoryInfoContract(item.Id, item.Name, item.DateLastSynced, FileSystemExtensions.Later(item.DateLastSynced, item.ModifiedTime));
        }
        public FileSystemInfoContract MoveItem(RootName root, FileSystemId source, string moveName, DirectoryId destination)
        {
            if (root == null)
                throw new ArgumentNullException(nameof(root));
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (string.IsNullOrEmpty(moveName))
                throw new ArgumentNullException(nameof(moveName));
            if (destination == null)
                throw new ArgumentNullException(nameof(destination));

            var effectivePath = GetFullPath(root, source.Value);
            var destinationPath = destination.Value;
            if (Path.IsPathRooted(destinationPath))
                destinationPath = destinationPath.Remove(0, Path.GetPathRoot(destinationPath).Length);
            var effectiveMovePath = GetFullPath(root, Path.Combine(destinationPath, moveName));

            var directory = new DirectoryInfo(effectivePath);
            if (directory.Exists) {
                directory.MoveTo(effectiveMovePath);
                return new DirectoryInfoContract(GetRelativePath(root, directory.FullName), directory.Name, directory.CreationTime, directory.LastWriteTime);
            }

            var file = new FileInfo(effectivePath);
            if (file.Exists) {
                file.MoveTo(effectiveMovePath);
                return new FileInfoContract(GetRelativePath(root, file.FullName), file.Name, file.CreationTime, file.LastWriteTime, file.Length, null);
            }

            throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resource.PathNotFound, source.Value));
        }
        public async Task<IEnumerable<FileSystemInfoContract>> GetChildItemAsync(RootName root, DirectoryId parent)
        {
            var context = await RequireContext(root);

            var item = await AsyncFunc.Retry<ListedFolder, pCloudException>(async () => await context.Client.ListFolderAsync(ToId(parent)), RETRIES);
            var items = item.Contents;

            return items.Select(i => i.ToFileSystemInfoContract());
        }
        public async Task<FileSystemInfoContract> CopyItemAsync(RootName root, FileSystemId source, string copyName, DirectoryId destination, bool recurse)
        {
            var fileSource = source as FileId;
            if (fileSource == null)
                 throw new NotSupportedException(Resources.CopyingOfDirectoriesNotSupported);

            var context = await RequireContext(root);

            var item = await AsyncFunc.Retry<pCloudFile, pCloudException>(async () => await context.Client.CopyFileAsync(ToId(fileSource), ToId(destination), copyName), RETRIES);

            return new FileInfoContract(item.Id, item.Name, item.Created, item.Modified, item.Size, null);
        }
        public async Task<FileSystemInfoContract> MoveItemAsync(RootName root, FileSystemId source, string moveName, DirectoryId destination, Func<FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContext(root);

            var directorySource = source as DirectoryId;
            if (directorySource != null) {
                var item = await context.Client.RenameFolderAsync(ToId(directorySource), ToId(destination), moveName);

                return new DirectoryInfoContract(item.Id, item.Name, item.Created, item.Modified);
            }

            var fileSource = source as FileId;
            if (fileSource != null) {
                var item = await context.Client.RenameFileAsync(ToId(fileSource), ToId(destination), moveName);

                return new FileInfoContract(item.Id, item.Name, item.Created, item.Modified, item.Size, null);
            }

            throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resources.ItemTypeNotSupported, source.GetType().Name));
        }
 internal DirectoryInfoContract SetupNewDirectory(string parentName, string directoryName)
 {
     var parentId = new DirectoryId(parentName);
     var directory = new DirectoryInfoContract($"{parentId.Value}{directoryName}\\", directoryName, ToDateTime("2016-01-01 12:00:00"), ToDateTime("2016-01-01 12:00:00"));
     Drive
         .Setup(drive => drive.NewDirectoryItem(It.Is<DirectoryInfoContract>(parent => parent.Id == parentId), directoryName))
         .Returns(directory);
     return directory;
 }
        public async Task<IEnumerable<FileSystemInfoContract>> GetChildItemAsync(RootName root, DirectoryId parent)
        {
            var context = await RequireContext(root);

            var childReferences = await AsyncFunc.Retry<ChildList, GoogleApiException>(async () => await context.Service.Children.List(parent.Value).ExecuteAsync(), RETRIES);
            var items = childReferences.Items.Select(async c => await AsyncFunc.Retry<GoogleFile, GoogleApiException>(async () => await context.Service.Files.Get(c.Id).ExecuteAsync(), RETRIES)).ToArray();
            Task.WaitAll(items);

            return items.Select(i => i.Result.ToFileSystemInfoContract());
        }
 internal FileInfoContract SetupNewFile(DirectoryId parentId, string fileName)
 {
     var file = new FileInfoContract($"{parentId.Value.TrimEnd('\\')}\\{fileName}", fileName, ToDateTime("2016-02-01 12:00:00"), ToDateTime("2016-02-01 12:00:00"), 0, null);
     Drive
         .Setup(drive => drive.NewFileItem(It.Is<DirectoryInfoContract>(parent => parent.Id == parentId), fileName, It.Is<Stream>(s => s.Length == 0)))
         .Returns(file);
     return file;
 }
        public async Task<FileSystemInfoContract> CopyItemAsync(RootName root, FileSystemId source, string copyName, DirectoryId destination, bool recurse)
        {
            var context = await RequireContext(root);

            var copy = new GoogleFile() { Title = copyName };
            if (destination != null)
                copy.Parents = new[] { new ParentReference() { Id = destination.Value } };
            var item = await AsyncFunc.Retry<GoogleFile, GoogleApiException>(async () => await context.Service.Files.Copy(copy, source.Value).ExecuteAsync(), RETRIES);

            return item.ToFileSystemInfoContract();
        }
        public async Task<FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress<ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var item = await AsyncFunc.Retry<FileSystem, ServerException>(async () => await context.Client.FileSystemManager.UploadNewFileStreamAsync(parent.Value, name, new ProgressStream(content, progress), true), RETRIES);

            return new FileInfoContract(item.Id, item.Name, item.DateLastSynced, FileSystemExtensions.Later(item.DateLastSynced, item.ModifiedTime), item.Size, null);
        }
        public async Task<FileSystemInfoContract> MoveItemAsync(RootName root, FileSystemId source, string moveName, DirectoryId destination, Func<FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContext(root);

            var move = new GoogleFile() { Parents = new[] { new ParentReference() { Id = destination.Value } }, Title = moveName };
            var patch = context.Service.Files.Patch(move, source.Value);
            var item = await AsyncFunc.Retry<GoogleFile, GoogleApiException>(async () => await patch.ExecuteAsync(), RETRIES);

            return item.ToFileSystemInfoContract();
        }
        public async Task<FileSystemInfoContract> MoveItemAsync(RootName root, FileSystemId source, string moveName, DirectoryId destination, Func<FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContext(root);

            if (string.IsNullOrEmpty(moveName))
                moveName = locatorResolver().Name;
            var success = await AsyncFunc.Retry<bool, ServerException>(async () => await context.Client.FileSystemManager.MoveFileAsync(source.Value, destination.Value, moveName, false), RETRIES);
            if (!success)
                throw new ApplicationException(string.Format(CultureInfo.CurrentCulture, Resources.MoveFailed, source.Value, destination.Value, moveName?.Insert(0, @"/") ?? string.Empty));

            var movedItemPath = destination.Value.Substring(0, destination.Value.LastIndexOf('/')) + @"/" + moveName;
            var item = await AsyncFunc.Retry<FileSystem, ServerException>(async () => await context.Client.FileSystemManager.GetFileSystemInformationAsync(movedItemPath), RETRIES);

            return item.ToFileSystemInfoContract();
        }
        public async Task<DirectoryInfoContract> NewDirectoryItemAsync(RootName root, DirectoryId parent, string name)
        {
            var context = await RequireContext(root);

            var file = new GoogleFile() { Title = name, MimeType = MIME_TYPE_DIRECTORY, Parents = new[] { new ParentReference() { Id = parent.Value } } };
            var item = await AsyncFunc.Retry<GoogleFile, GoogleApiException>(async () => await context.Service.Files.Insert(file).ExecuteAsync(), RETRIES);

            return new DirectoryInfoContract(item.Id, item.Title, new DateTimeOffset(item.CreatedDate.Value), new DateTimeOffset(item.ModifiedDate.Value));
        }
        public async Task<IEnumerable<FileSystemInfoContract>> GetChildItemAsync(RootName root, DirectoryId parent)
        {
            var context = await RequireContext(root);

            var items = await AsyncFunc.Retry<FileSystem, ServerException>(async () => await context.Client.FileSystemManager.GetFileSystemInformationAsync(parent.Value), RETRIES);

            return items.Children.Select(i => i.ToFileSystemInfoContract());
        }
        public async Task<FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress<ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var file = new GoogleFile() { Title = name, MimeType = MIME_TYPE_FILE, Parents = new[] { new ParentReference() { Id = parent.Value } } };
            var insert = context.Service.Files.Insert(file, content, MIME_TYPE_FILE);
            insert.ProgressChanged += p => progress.Report(new ProgressValue((int)p.BytesSent, (int)content.Length));
            var upload = await AsyncFunc.Retry<IUploadProgress, GoogleApiException>(async () => await insert.UploadAsync(), RETRIES);
            var item = insert.ResponseBody;

            return new FileInfoContract(item.Id, item.Title, new DateTimeOffset(item.CreatedDate.Value), new DateTimeOffset(item.ModifiedDate.Value), item.FileSize.Value, item.Md5Checksum);
        }
 public FileSystemInfoContract CopyItem(RootName root, FileSystemId source, string copyName, DirectoryId destination, bool recurse)
 {
     throw new NotSupportedException(Resources.CopyingOfFilesNotSupported);
 }
        public FileInfoContract NewFileItem(RootName root, DirectoryId parent, string name, Stream content, IProgress<ProgressValue> progress)
        {
            if (root == null)
                throw new ArgumentNullException(nameof(root));
            if (parent == null)
                throw new ArgumentNullException(nameof(parent));
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException(nameof(name));

            var effectivePath = GetFullPath(root, Path.Combine(parent.Value, name));

            var file = new FileInfo(effectivePath);
            if (file.Exists)
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resource.DuplicatePath, parent.Value));

            using (var fileStream = file.Create()) {
                if (content != null)
                    content.CopyTo(fileStream);
            }

            file.Refresh();
            return new FileInfoContract(GetRelativePath(root, file.FullName), file.Name, file.CreationTime, file.LastWriteTime, file.Length, null);
        }
        public DirectoryInfoContract NewDirectoryItem(RootName root, DirectoryId parent, string name)
        {
            var context = RequireContext(root);

            var nodes = context.Client.GetNodes();
            var parentItem = nodes.Single(n => n.Id == parent.Value);
            var item = context.Client.CreateFolder(name, parentItem);

            return new DirectoryInfoContract(item.Id, item.Name, DateTimeOffset.MinValue, item.LastModificationDate);
        }
        public DirectoryInfoContract NewDirectoryItem(RootName root, DirectoryId parent, string name)
        {
            if (root == null)
                throw new ArgumentNullException(nameof(root));
            if (parent == null)
                throw new ArgumentNullException(nameof(parent));
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException(nameof(name));

            var effectivePath = GetFullPath(root, Path.Combine(parent.Value, name));

            var directory = new DirectoryInfo(effectivePath);
            if (directory.Exists)
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resource.DuplicatePath, effectivePath));

            directory.Create();

            return new DirectoryInfoContract(GetRelativePath(root, directory.FullName), directory.Name, directory.CreationTime, directory.LastWriteTime);
        }
        public async Task<FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress<ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var request = new BoxFileRequest() { Name = name, Parent = new BoxRequestEntity() { Id = parent.Value } };
            var item = await AsyncFunc.Retry<BoxFile, BoxException>(async () => await context.Client.FilesManager.UploadAsync(request, new ProgressStream(content, progress), boxFileFields), RETRIES);

            return new FileInfoContract(item.Id, item.Name, item.CreatedAt.Value, item.ModifiedAt.Value, item.Size.Value, item.Sha1.ToLowerInvariant());
        }
        public async Task<FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress<ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var tokenSource = new CancellationTokenSource();
            var item = await AsyncFunc.Retry<pCloudFile, pCloudException>(async () => await context.Client.UploadFileAsync(new ProgressStream(content, progress), ToId(parent), name, tokenSource.Token), RETRIES);

            return new FileInfoContract(item.Id, item.Name, item.Created, item.Modified, item.Size, null);
        }