Ejemplo n.º 1
0
        public async Task ChunkedFileUploadTask()
        {
            var site    = await SiteAsync;
            var file    = Utility.GetDemoImage("1");
            var chunked = new ChunkedUploadSource(site, file.ContentStream)
            {
                DefaultChunkSize = 1024 * 4
            };

            do
            {
                var result = await chunked.StashNextChunkAsync();

                Assert.NotEqual(UploadResultCode.Warning, result.ResultCode);
                if (result.ResultCode == UploadResultCode.Success)
                {
                    // As of 2019-10, this does not hold.
                    // Assert.True(result.FileRevision.IsAnonymous);
                    Assert.Equal(file.Sha1, result.FileRevision.Sha1, StringComparer.OrdinalIgnoreCase);
                }
            } while (!chunked.IsStashed);
            try
            {
                // This is to attempt to prevent the following error:
                // backend-fail-alreadyexists: The file "mwstore://local-swift-eqiad/local-public/archive/9/95/20191116051316!Test_image.jpg" already exists.
                await site.UploadAsync($"Test image {Utility.RandomTitleString()}.jpg", chunked, file.Description, true);
            }
            catch (OperationFailedException ex) when(ex.ErrorCode == "fileexists-no-change")
            {
                // We cannot suppress this error by setting ignoreWarnings = true.
                WriteOutput(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public async Task ChunkedFileUploadTask()
        {
            var site    = await SiteAsync;
            var file    = Utility.GetDemoImage("1");
            var chunked = new ChunkedUploadSource(site, file.ContentStream)
            {
                DefaultChunkSize = 1024 * 4
            };

            do
            {
                var result = await chunked.StashNextChunkAsync();

                Assert.NotEqual(UploadResultCode.Warning, result.ResultCode);
                if (result.ResultCode == UploadResultCode.Success)
                {
                    Assert.True(result.FileRevision.IsAnonymous);
                    Assert.Equal(file.Sha1, result.FileRevision.Sha1, StringComparer.OrdinalIgnoreCase);
                }
            } while (!chunked.IsStashed);
            try
            {
                await site.UploadAsync("Test image.jpg", chunked, file.Description, true);
            }
            catch (OperationFailedException ex) when(ex.ErrorCode == "fileexists-no-change")
            {
                // We cannot suppress this error by setting ignoreWarnings = true.
                Output.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        protected override async Task ProcessRecordAsync(CancellationToken cancellationToken)
        {
            using (var fs = new FileStream(File.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 1024 * 4,
                                           FileOptions.Asynchronous | FileOptions.SequentialScan))
            {
                var titleLink = WikiLink.Parse(WikiSite, Title, BuiltInNamespaces.File);
                WikiUploadSource uploadSource;
                if (Chunked)
                {
                    if (!ShouldProcess($"{File}", "Chunked stash"))
                    {
                        return;
                    }
                    var src = new ChunkedUploadSource(WikiSite, fs, File.Name)
                    {
                        DefaultChunkSize = 512 * 1024
                    };
                    var progress = new ProgressRecord(0, $"Stash {File}", null);
                    WriteProgress(progress);
                    do
                    {
                        var r = await src.StashNextChunkAsync(cancellationToken);

                        if (r.ResultCode == UploadResultCode.Warning)
                        {
                            WriteWarning(r.Warnings.ToString());
                        }
                        progress.PercentComplete = (int)(100.0 * src.UploadedSize / src.TotalSize);
                    } while (!src.IsStashed);
                    progress.RecordType = ProgressRecordType.Completed;
                    uploadSource        = src;
                }
                else
                {
                    uploadSource = new StreamUploadSource(fs);
                }
                if (!ShouldProcess($"{File} -> {titleLink}", "Upload"))
                {
                    return;
                }
                var result = await WikiSite.UploadAsync(Title, uploadSource, Comment, Force, Utility.ParseAutoWatchBehavior(Watch), cancellationToken);

                if (result.ResultCode == UploadResultCode.Warning)
                {
                    WriteWarning(result.Warnings.ToString());
                }
                WriteObject(result);
            }
        }