Esempio n. 1
0
        public void Add_Value()
        {
            var expected = 1025;
            var left     = new ByteSize(1000);
            var right    = new ByteSize(25);

            //Act
            var updated = left.Add(right);
            var actual  = updated.Bytes;

            //Assert
            actual.Should().Be(expected);
        }
Esempio n. 2
0
        /// <summary>
        /// Downloads the specified files to a specified output directory
        /// </summary>
        /// <param name="inputSources">The files to download</param>
        /// <param name="isCompressed">True if the download is a compressed file, otherwise false</param>
        /// <param name="outputDir">The output directory to download to</param>
        /// <param name="isGame">Indicates if the download is for a game. If false it is assumed to be a generic patch.</param>
        /// <returns>True if the download succeeded, otherwise false</returns>
        public async Task <bool> DownloadAsync(IList <Uri> inputSources, bool isCompressed, FileSystemPath outputDir, bool isGame = false)
        {
            try
            {
                RL.Logger?.LogInformationSource($"A download is starting...");

                // Make sure there are input sources to download
                if (!inputSources.Any())
                {
                    RL.Logger?.LogInformationSource($"Download failed due to there not being any input sources");

                    await Services.MessageUI.DisplayMessageAsync(Resources.Download_NoFilesFound, MessageType.Error);

                    return(false);
                }

                if (Data.HandleDownloadsManually)
                {
                    var result = await RCPServices.UI.DisplayMessageAsync(Resources.Download_ManualInstructions, Resources.Download_ManualHeader, MessageType.Information, true, new DialogMessageActionViewModel[]
                    {
                        // Download files
                        new DialogMessageActionViewModel
                        {
                            DisplayText       = Resources.Download_ManualDownload,
                            ShouldCloseDialog = false,
                            OnHandled         = () =>
                            {
                                foreach (var u in inputSources)
                                {
                                    OpenUrl(u.AbsoluteUri);
                                }
                            }
                        },
                        // Open destination folder
                        new DialogMessageActionViewModel
                        {
                            DisplayText       = Resources.Download_ManualOpenDestination,
                            ShouldCloseDialog = false,
                            OnHandled         = () =>
                            {
                                Directory.CreateDirectory(outputDir);
                                Task.Run(async() => await RCPServices.File.OpenExplorerLocationAsync(outputDir));
                            }
                        },
                    });

                    if (!result)
                    {
                        return(false);
                    }

                    RL.Logger?.LogInformationSource($"Manual download finished");

                    // Return the result
                    return(true);
                }
                else
                {
                    // Allow user to confirm
                    try
                    {
                        ByteSize size = ByteSize.FromBytes(0);
                        foreach (var item in inputSources)
                        {
                            var webRequest = WebRequest.Create(item);
                            webRequest.Method = "HEAD";

                            using var webResponse = webRequest.GetResponse();
                            size = size.Add(ByteSize.FromBytes(Convert.ToDouble(webResponse.Headers.Get("Content-Length"))));
                        }

                        RL.Logger?.LogDebugSource($"The size of the download has been retrieved as {size}");

                        if (!await Services.MessageUI.DisplayMessageAsync(String.Format(isGame ? Resources.DownloadGame_ConfirmSize : Resources.Download_ConfirmSize, size), Resources.Download_ConfirmHeader, MessageType.Question, true))
                        {
                            return(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.HandleUnexpected("Getting download size");
                        if (!await Services.MessageUI.DisplayMessageAsync(isGame ? Resources.DownloadGame_Confirm : Resources.Download_Confirm, Resources.Download_ConfirmHeader, MessageType.Question, true))
                        {
                            return(false);
                        }
                    }
                }

                // Create the download dialog
                var dialog = new Downloader(new DownloaderViewModel(inputSources, outputDir, isCompressed));

                // Show the dialog
                dialog.ShowDialog();

                RL.Logger?.LogInformationSource($"The download finished with the result of {dialog.ViewModel.DownloadState}");

                // Return the result
                return(dialog.ViewModel.DownloadState == DownloaderViewModel.DownloadStates.Succeeded);
            }
            catch (Exception ex)
            {
                ex.HandleError($"Downloading files");
                await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Download_Error);

                return(false);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Downloads the specified files to a specified output directory
        /// </summary>
        /// <param name="inputSources">The files to download</param>
        /// <param name="isCompressed">True if the download is a compressed file, otherwise false</param>
        /// <param name="outputDir">The output directory to download to</param>
        /// <param name="isGame">Indicates if the download is for a game. If false it is assumed to be a generic patch.</param>
        /// <returns>True if the download succeeded, otherwise false</returns>
        public async Task <bool> DownloadAsync(IList <Uri> inputSources, bool isCompressed, FileSystemPath outputDir, bool isGame = false)
        {
            try
            {
                RL.Logger?.LogInformationSource($"A download is starting...");

                // Make sure there are input sources to download
                if (!inputSources.Any())
                {
                    RL.Logger?.LogInformationSource($"Download failed due to there not being any input sources");

                    await Services.MessageUI.DisplayMessageAsync(Resources.Download_NoFilesFound, MessageType.Error);

                    return(false);
                }

                // Allow user to confirm
                try
                {
                    ByteSize size = ByteSize.FromBytes(0);
                    foreach (var item in inputSources)
                    {
                        var webRequest = WebRequest.Create(item);
                        webRequest.Method = "HEAD";

                        using var webResponse = webRequest.GetResponse();
                        size = size.Add(ByteSize.FromBytes(Convert.ToDouble(webResponse.Headers.Get("Content-Length"))));
                    }

                    RL.Logger?.LogDebugSource($"The size of the download has been retrieved as {size}");

                    if (!await Services.MessageUI.DisplayMessageAsync(String.Format(isGame ? Resources.DownloadGame_ConfirmSize : Resources.Download_ConfirmSize, size), Resources.Download_ConfirmHeader, MessageType.Question, true))
                    {
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    ex.HandleUnexpected("Getting download size");
                    if (!await Services.MessageUI.DisplayMessageAsync(isGame ? Resources.DownloadGame_Confirm : Resources.Download_Confirm, Resources.Download_ConfirmHeader, MessageType.Question, true))
                    {
                        return(false);
                    }
                }

                // Create the download dialog
                var dialog = new Downloader(new DownloaderViewModel(inputSources, outputDir, isCompressed));

                // Show the dialog
                dialog.ShowDialog();

                RL.Logger?.LogInformationSource($"The download finished with the result of {dialog.ViewModel.DownloadState}");

                // Return the result
                return(dialog.ViewModel.DownloadState == DownloaderViewModel.DownloadStates.Succeeded);
            }
            catch (Exception ex)
            {
                ex.HandleError($"Downloading files");
                await Services.MessageUI.DisplayExceptionMessageAsync(ex, Resources.Download_Error);

                return(false);
            }
        }