Exemplo n.º 1
0
        private async Task UploadFile(UploadFile file, string summary, string newPageContent, CancellationToken cancelToken)
        {
            var maxLagRetries = 3;

            while (true)
            {
                cancelToken.ThrowIfCancellationRequested();

                file.SetUploading();

                IUploadResponse response;
                try
                {
                    response = await _fileUploader.UpLoadAsync(
                        file.FullPath,
                        cancelToken,
                        summary,
                        newPageContent);
                }
                finally
                {
                    await _helpers.Wait(_appSettings.UploadDelay, cancelToken);
                }

                // Note: Only access response.Result once, as thgis makes testing much easier
                //       as a chain of responses can be faked. See maclag tests in UploadViewModelTests.cs
                var result = response.Result;

                if (result == ResponseCodes.Success)
                {
                    UploadFiles.Remove(file);
                }
                else if (result == ResponseCodes.Warning)
                {
                    file.SetWarning(response.Warnings.ToString());
                }
                else if (result == ResponseCodes.MaxlagThrottle)
                {
                    if (--maxLagRetries < 0)
                    {
                        throw new ServerIsBusyException();
                    }
                    await _helpers.Wait(response.RetryDelay * 1000, cancelToken);

                    continue;
                }
                else if (response.Errors.IsAny)
                {
                    if (response.Errors.IsTokenError)
                    {
                        if (_editTokenRefreshed)
                        {
                            throw new NoEditTokenException();
                        }
                        else
                        {
                            await RefreshEditToken();

                            continue;
                        }
                    }
                    else if (response.Errors.IsMutsBeLoggedInError)
                    {
                        file.SetError(response.Errors.ToString());
                        throw new MustBeLoggedInException();
                    }
                    else if (response.Errors.IsRateLimitedError)
                    {
                        file.SetDelaying(Resources.WaitingForRetry + " " + response.Errors.ToString());
                        await _helpers.Wait(_appSettings.RateLimitedBackoffPeriod, cancelToken);

                        continue;
                    }
                    else
                    {
                        file.SetError(response.Errors.ToString());
                    }
                }
                else
                {
                    file.SetError(UploadMessages.UnkownServerResponse);
                }
                return;
            }
        }