Exemplo n.º 1
0
        private OnlineItem UploadFile(UploadStatus status, OnlineItem folder, string filePath)
        {
            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                long length = stream.Length;

                var child = OnlinePathBuilder.CreateChild(folder, Path.GetFileName(filePath));
                var task  = _client.PutFile(child.FullPath, stream, new PutFileParameters {
                    CancellationToken = _cancellationToken
                });
                do
                {
                    status.FileProgress = (float)((double)stream.Position / length);
                } while (!task.IsFaulted && !task.IsCanceled && !task.IsCompleted && !task.Wait(100));

                return(child);
            }
        }
Exemplo n.º 2
0
        public async Task <OnlineItem> CreateFolderAsync(string relativePath, OnlineItem parent = null)
        {
            // The default relative path is just nothing.
            if (string.IsNullOrEmpty(relativePath))
            {
                throw new ArgumentException("The relative path cannot be null or empty.", nameof(relativePath));
            }
            if (relativePath.StartsWith("/") || relativePath.EndsWith("/"))
            {
                throw new ArgumentException("The relative path cannot start or end with a slash.", nameof(relativePath));
            }

            // Create a new root if it does not exist (this will be the /Share/Guid/ folder.
            if (parent == null)
            {
                parent           = new OnlineItem();
                parent.LocalPath = string.Empty;
                parent.FullPath  = OnlinePathBuilder.ConvertPathToFullUri(_connectionSettings.StorageUri, null);
            }

            OnlineItem currentChild = parent;

            var pathParts = relativePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string pathPart in pathParts)
            {
                currentChild = OnlinePathBuilder.CreateChild(currentChild, pathPart);

                // Create the folder
                _cancellationToken.ThrowIfCancellationRequested();

                var createResponse = await _client.Mkcol(currentChild.FullPath, new MkColParameters { CancellationToken = _cancellationToken }).ConfigureAwait(false);

                if (createResponse.StatusCode != 201 && createResponse.StatusCode != 405) // 405 if maybe a tweak to not check if it exists?
                {
                    throw new Exception($"The folder is not created, code={createResponse.StatusCode}, description={createResponse.Description}");
                }
            }

            return(OnlinePathBuilder.CreateChild(parent, relativePath));
        }