Example #1
0
        /// <summary>
        /// Uploads inline image attachment from source to target and returns target inline image AttachmentReference Guid.
        /// </summary>
        /// <param name="inlineImageUrl"></param>
        /// <returns></returns>
        private async Task <string> UploadInlineImageAttachmentFromSourceWorkItemToTarget(IBatchMigrationContext batchContext, string inlineImageUrl, int sourceWorkItemId, int maxAttachmentSize)
        {
            Guid   sourceGuid = MigrationHelpers.GetAttachmentUrlGuid(inlineImageUrl);
            string targetGuid = null;

            if (Guid.Empty.Equals(sourceGuid))
            {
                Logger.LogWarning(LogDestination.File, $"Unexpected format for inline image url {inlineImageUrl} for source work item {sourceWorkItemId}");
                ClientHelpers.AddFailureReasonToWorkItemMigrationState(sourceWorkItemId, FailureReason.InlineImageUrlFormatError, batchContext.WorkItemMigrationState);

                // just return the null guid since there is nothing we can do with an invalid url
                return(null);
            }

            Stream stream = null;

            try
            {
                Logger.LogTrace(LogDestination.File, $"Reading inline image {inlineImageUrl} for source work item {sourceWorkItemId} from the source account");
                stream = await WorkItemTrackingHelpers.GetAttachmentAsync(this.context.SourceClient.WorkItemTrackingHttpClient, sourceGuid);

                Logger.LogTrace(LogDestination.File, $"Completed reading inline image {inlineImageUrl} for source work item {sourceWorkItemId} from the source account");
            }
            catch (Exception e)
            {
                Logger.LogError(LogDestination.File, e, $"Unable to download inline image {inlineImageUrl} for source work item {sourceWorkItemId} from the source account");
                ClientHelpers.AddFailureReasonToWorkItemMigrationState(sourceWorkItemId, FailureReason.InlineImageDownloadError, batchContext.WorkItemMigrationState);

                // just return the null guid since there is nothing we can do if we couldn't download the inline image
                return(null);
            }

            if (stream != null)
            {
                using (MemoryStream memstream = new MemoryStream())
                {
                    bool copiedStream = false;
                    using (stream)
                    {
                        try
                        {
                            Logger.LogTrace(LogDestination.File, $"Downloading inline image {inlineImageUrl} for source work item {sourceWorkItemId} from the source account");
                            await ClientHelpers.CopyStreamAsync(stream, memstream);

                            Logger.LogTrace(LogDestination.File, $"Completed downloading inline image {inlineImageUrl} for source work item {sourceWorkItemId} from the source account");
                            copiedStream = true;
                        }
                        catch (Exception e)
                        {
                            Logger.LogError(LogDestination.File, e, $"Unable to download inline image {inlineImageUrl} for source work item {sourceWorkItemId} from the source account");
                            ClientHelpers.AddFailureReasonToWorkItemMigrationState(sourceWorkItemId, FailureReason.InlineImageDownloadError, batchContext.WorkItemMigrationState);
                        }
                    }

                    if (memstream.Length > maxAttachmentSize)
                    {
                        Logger.LogWarning(LogDestination.File, $"Inline image attachment of source work item with id {sourceWorkItemId} and url {inlineImageUrl} exceeded the maximum attachment size of {maxAttachmentSize} bytes." +
                                          $" Skipping creating the inline image attachment in target account.");
                        return(null);
                    }

                    if (copiedStream)
                    {
                        memstream.Position = 0;
                        //upload the attachment to target
                        try
                        {
                            Logger.LogTrace(LogDestination.File, $"Uploading inline image {inlineImageUrl} for source work item {sourceWorkItemId} from the source account");
                            var aRef = await WorkItemTrackingHelpers.CreateAttachmentChunkedAsync(this.context.TargetClient.WorkItemTrackingHttpClient, this.context.TargetClient.Connection, memstream, this.context.Config.AttachmentUploadChunkSize);

                            targetGuid = aRef.Id.ToString();
                            Logger.LogTrace(LogDestination.File, $"Completed uploading inline image {inlineImageUrl} for source work item {sourceWorkItemId} from the source account");
                        }
                        catch (Exception e)
                        {
                            Logger.LogError(LogDestination.File, e, $"Unable to upload inline image for source work item {sourceWorkItemId} in the target account");
                            ClientHelpers.AddFailureReasonToWorkItemMigrationState(sourceWorkItemId, FailureReason.InlineImageUploadError, batchContext.WorkItemMigrationState);
                        }
                    }
                }
            }

            return(targetGuid);
        }