Example #1
0
        /// <inheritdoc />
        protected override async Task ProcessRecordAsync(CancellationToken cancellationToken)
        {
            using (var fs = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read, 1024 * 4,
                                           FileOptions.Asynchronous | FileOptions.SequentialScan))
            {
                var uploadSource = new StreamUploadSource(fs);
                var result       = await WikiSite.UploadAsync(Title, uploadSource, Comment, Force, Utility.ParseAutoWatchBehavior(Watch), cancellationToken);

                WriteObject(result);
            }
        }
        /// <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);
            }
        }
Example #3
0
        private async Task UploadFileAsync(string filename, string localName)
        {
            if (Client == null)
            {
                throw new InvalidOperationException("Wiki client has not been initialized!  Call Initialize() first.");
            }

            if (!File.Exists(filename))
            {
                Console.WriteLine($"{filename} does not exist!  Cannot upload.");
                return;
            }

            Console.WriteLine($"Uploading file to {localName}...");

            using (var s = File.OpenRead(filename))
            {
                var source           = new StreamUploadSource(s);
                var suppressWarnings = false;
                try
                {
                    var result = await Site.UploadAsync($"File:{localName}", source, "Mass upload by ArtificerBot.", suppressWarnings);

                    if (result.ResultCode == UploadResultCode.Warning)
                    {
                        if (result.Warnings.TitleExists && result.Warnings.DuplicateVersions.Count == 0)
                        {
                            //source = new StreamUploadSource(s);
                            s.Seek(0, SeekOrigin.Begin);
                            result = await Site.UploadAsync($"File:{localName}", source, "Mass upload by ArtificerBot.", true);
                        }
                        Console.WriteLine(result.Warnings.ToString());
                    }
                    Console.WriteLine("Done.");
                }
                catch (OperationFailedException ex)
                {
                    // Since MW 1.31, if you are uploading the exactly same content to the same title with ignoreWarnings set to true, you will reveive this exception with ErrorCode set to fileexists-no-change.
                    // See https://gerrit.wikimedia.org/r/378702.
                    Console.WriteLine(ex.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while uploading!");
                    Console.WriteLine(ex.ToString());
                }
            }
        }