Example #1
0
        public override async Task ExecuteAsync(IOperationExecutionContext context)
        {
            var credentials = this.GetCredentials(context as ICredentialResolutionContext);
            var fileOps     = await context.Agent.GetServiceAsync <IFileOperationsExecuter>();

            var sourceDirectory = context.ResolvePath(this.SourceDirectory);

            if (!await fileOps.DirectoryExistsAsync(sourceDirectory))
            {
                this.LogWarning($"Source directory {sourceDirectory} does not exist; nothing to upload.");
                return;
            }

            var files = (await fileOps.GetFileSystemInfosAsync(sourceDirectory, new MaskingContext(this.Includes, this.Excludes)))
                        .OfType <SlimFileInfo>()
                        .ToList();

            if (files.Count == 0)
            {
                this.LogWarning($"No files match the specified masks in {sourceDirectory}; nothing to upload.");
                return;
            }

            var prefix = string.Empty;

            if (!string.IsNullOrEmpty(this.KeyPrefix))
            {
                prefix = this.KeyPrefix.Trim('/') + "/";
            }

            Interlocked.Exchange(ref this.totalUploadBytes, files.Sum(f => f.Size));

            using var s3 = this.CreateClient(credentials);
            foreach (var file in files)
            {
                var keyName = prefix + file.FullName.Substring(sourceDirectory.Length).Replace(Path.DirectorySeparatorChar, '/').Trim('/');
                this.LogInformation($"Transferring {file.FullName} to {keyName} ({AH.FormatSize(file.Size)})...");
                using var fileStream = await fileOps.OpenFileAsync(file.FullName, FileMode.Open, FileAccess.Read);

                if (file.Size < this.PartSize * 2)
                {
                    await this.UploadSmallFileAsync(s3, fileStream, keyName, context);
                }
                else
                {
                    await this.MultipartUploadAsync(s3, fileStream, keyName, context);
                }
            }
        }
        public override OperationProgress GetProgress()
        {
            long total  = Interlocked.Read(ref this.totalBytes);
            long copied = Interlocked.Read(ref this.bytesCopied);

            if (total == 0)
            {
                return(null);
            }

            return(new OperationProgress(
                       (int)(100.0 * copied / total),
                       AH.FormatSize(total - copied) + " remaining"
                       ));
        }
Example #3
0
        public override OperationProgress GetProgress()
        {
            long total    = Interlocked.Read(ref this.totalUploadBytes);
            long uploaded = Interlocked.Read(ref this.uploadedBytes);

            if (total == 0)
            {
                return(null);
            }

            long remaining = Math.Max(total - uploaded, 0);

            if (remaining > 0)
            {
                return(new OperationProgress((int)(100.0 * uploaded / total), AH.FormatSize(remaining) + " remaining"));
            }
            else
            {
                return(new OperationProgress(100));
            }
        }
        public override async Task ExecuteAsync(IOperationExecutionContext context)
        {
            this.progress = null;

            var sourceDirectory = context.ResolvePath(this.SourceDirectory);

            var fileOps = await context.Agent.GetServiceAsync <IFileOperationsExecuter>().ConfigureAwait(false);

            var files = await fileOps.GetFileSystemInfosAsync(sourceDirectory, new MaskingContext(this.Includes, this.Excludes)).ConfigureAwait(false);

            if (files.Count == 0)
            {
                this.LogWarning("No files matched.");
                return;
            }

            var github = new GitHubClient(this.ApiUrl, this.UserName, this.Password, this.OrganizationName);

            var ownerName = AH.CoalesceString(this.OrganizationName, this.UserName);

            foreach (var info in files)
            {
                var file = info as SlimFileInfo;
                if (file == null)
                {
                    this.LogWarning($"Not a file: {info.FullName}");
                    continue;
                }

                using (var stream = await fileOps.OpenFileAsync(file.FullName, FileMode.Open, FileAccess.Read).ConfigureAwait(false))
                {
                    this.LogDebug($"Uploading {file.Name} ({AH.FormatSize(file.Size)})");
                    await github.UploadReleaseAssetAsync(ownerName, this.RepositoryName, this.Tag, file.Name, this.ContentType, new PositionStream(stream, file.Size), pos => this.progress = new OperationProgress((int)(100 * pos / file.Size), $"Uploading {file.Name} ({AH.FormatSize(pos)} / {AH.FormatSize(file.Size)})"), context.CancellationToken).ConfigureAwait(false);

                    this.progress = null;
                }
            }
        }
 public override OperationProgress GetProgress()
 {
     if (this.TotalSize == 0)
     {
         return(null);
     }
     return(new OperationProgress((int)(100 * this.CurrentPosition / this.TotalSize), AH.FormatSize(this.TotalSize - this.CurrentPosition) + " remaining"));
 }