Пример #1
0
        public async Task <IActionResult> Download(string filename)
        {
            if (filename == null)
            {
                return(Content("filename not present"));
            }

            var file = await _downloadService.DownloadFileAsync(filename);

            return(File(file.File, file.MimeType, file.Name));
        }
Пример #2
0
    private async Task <string> HashResourcePack(string url, IProgress <float> downloadProgress, Server server)
    {
        string result = "";

        if (string.IsNullOrEmpty(url))
        {
            return(result);
        }

        //ensure tmp directory
        new DirectoryInfo(Path.Combine(_application.AppPath, "tmp")).Create();
        FileInfo resourcePackFile = new FileInfo(
            Path.Combine(_application.AppPath, "tmp", Guid.NewGuid().ToString()
                         .Replace("-", "") + ".zip"));

        //Download the resource pack
        var client = new HttpClient();
        HttpResponseMessage response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));

        if (response.Headers.GetValues("ContentType").All(h => h != "application/zip"))
        {
            await _console.WriteError(server,
                                      "Failed to generate resource-pack hash: No zip at URL\nResuming with no hash...");

            return(result);
        }

        await _download.DownloadFileAsync(url, resourcePackFile.FullName, downloadProgress, CancellationToken.None);

        //Calculate sha-1
        await using (FileStream fs = resourcePackFile.OpenRead())
        {
            await using var bs = new BufferedStream(fs);
            using (SHA1 sha1 = SHA1.Create())
            {
                byte[] hash = await sha1.ComputeHashAsync(bs);

                StringBuilder formatted = new StringBuilder(2 * hash.Length);
                foreach (var b in hash)
                {
                    formatted.Append($"{b:X2}");
                }

                result = formatted.ToString();
            }
        }

        resourcePackFile.Delete();
        return(result);
    }
Пример #3
0
        /// <summary>
        /// Starts the download async.
        /// </summary>
        /// <returns>The download async.</returns>
        public async Task StartDownloadAsync()
        {
            var progressIndicator = new Progress <double>(ReportProgress);
            var cts = new CancellationTokenSource();

            try
            {
                IsDownloading = true;

                var url = "https://github.com/damienaicheh/XamarinAndroidParcelable/archive/master.zip";

                await _downloadService.DownloadFileAsync(url, progressIndicator, cts.Token);
            }
            catch (OperationCanceledException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                //Manage cancellation here
            }
            finally
            {
                IsDownloading = false;
            }
        }
        public async Task StartDownloadAsync(Dokument dokument)
        {
            var progressIndicator = new Progress <double>(ReportProgress);
            var cts = new CancellationTokenSource();

            try
            {
                IsDownloading = true;

                var url = "http://localhost:53404/api/Dokument/PreuzmiDokument/" + dokument.DokumentId;

                await _downloadService.DownloadFileAsync(url, progressIndicator, cts.Token);
            }
            catch (OperationCanceledException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                //Manage cancellation here
            }
            finally
            {
                IsDownloading = false;
            }
        }
        /// <summary>
        /// Starts the download async.
        /// </summary>
        /// <returns>The download async.</returns>
        public async Task StartDownloadAsync()
        {
            var progressIndicator = new Progress <double>(ReportProgress);
            var cts = new CancellationTokenSource();

            try
            {
                IsDownloading = true;

                var url = "https://sample-videos.com/img/Sample-jpg-image-5mb.jpg";

                await _downloadService.DownloadFileAsync(url, progressIndicator, cts.Token);
            }
            catch (OperationCanceledException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                //Manage cancellation here
            }
            finally
            {
                IsDownloading = false;
            }
        }