Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download to release #2742

Closed
1 task done
Vincenzoferrara opened this issue Jul 7, 2023 · 6 comments
Closed
1 task done

download to release #2742

Vincenzoferrara opened this issue Jul 7, 2023 · 6 comments
Labels
Status: Triage This is being looked at and prioritized Type: Documentation Improvements or additions to documentation

Comments

@Vincenzoferrara
Copy link

Describe the need

I'm trying to download a release file but there's nothing written in the documentation, I haven't found anything useful even in the questions.
Could you have some examples? A thousand thanks

SDK Version

No response

API Version

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Vincenzoferrara Vincenzoferrara added Status: Triage This is being looked at and prioritized Type: Documentation Improvements or additions to documentation labels Jul 7, 2023
@kfcampbell
Copy link
Member

Could you do something like the following?

using Octokit;
using System;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;

var client = new GitHubClient(new ProductHeaderValue("MyApp"));
var release = await client.Repository.Release.Get("integrations", "terraform-provider-github", "v5.29.0");
Console.WriteLine($"Release {release.Name} has {release.Assets.Count} assets");
using (var httpClient = new HttpClient())
{
  foreach (var asset in release.Assets)
  {
    Console.WriteLine($"Retrieving {asset.Name}");
    var fileData = await httpClient.GetByteArrayAsync(asset.BrowserDownloadUrl);
    File.WriteAllBytes(asset.Name, fileData);
  }
}

@Vincenzoferrara
Copy link
Author

Vincenzoferrara commented Jul 7, 2023

my repository is private.
I honestly thought there was a command (which also included file verification)

chatgpt create this code

`
private const string GitHubToken = "";
private const string GitHubOwner = "";
private const string GitHubRepo = "";
private static readonly GitHubClient client = new GitHubClient(new ProductHeaderValue("UpdateManager")) { Credentials = new Credentials(GitHubToken) };
private static readonly string temp_dir = Path.Combine(Path.GetTempPath(), "UpdateTemp");
var releases = await client.Repository.Release.GetAll(GitHubOwner, GitHubRepo);
release = releases[0];

var asset = release.Assets.FirstOrDefault(a => a.Name == file_name);

            if (asset != null)
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Add("Authorization", $"token {GitHubToken}");
                    using (var response = await httpClient.GetAsync(asset.BrowserDownloadUrl))
                    {
                        response.EnsureSuccessStatusCode();
                        using (var stream = await response.Content.ReadAsStreamAsync())
                        {
                            using (var fileStream = File.Create(file_name))
                            {
                                await stream.CopyToAsync(fileStream);
                            }
                        }
                    }
                }
            }

`

but I don't know how to integrate the verification of downloaded files
(ps the code i before is part of a bigger code so i may have skipped somewhere)

@kfcampbell
Copy link
Member

What do you mean by "integrate the verification of downloaded files"?

@Vincenzoferrara
Copy link
Author

check if the downloaded file was current, or if it is in an inconsistent state.

I know you are using a checksum.

for example if the downalod stops the internet what happens to the file?
so here is a file integrity check, to verify that the file is the same as the original

@Vincenzoferrara
Copy link
Author

however the above code doesn't work, it fails to connect giving a 404 error

i can't figure out how to use the token, with your toolkit it works so it's a problem of my code.

Could I have an example of the code? Thanks in advance

ps: I also tried using a token with full permissions but it gives the same error

@Vincenzoferrara
Copy link
Author

Vincenzoferrara commented Mar 31, 2024

this code it works for me ` try
{
var releases = await client.Repository.Release.GetAll(GitHubOwner, GitHubRepo);
var release = releases[0];
var latestAsset = await client.Repository.Release.GetAllAssets(GitHubOwner, GitHubRepo, release.Id);

            foreach (var asset in latestAsset)
            {
                if (asset.ContentType == "application/zip" && asset.Name == file_name)
                {
                    var url = asset.Url;

                    using (HttpClient client = new HttpClient())
                    {

                        client.DefaultRequestHeaders.Add("User-Agent", "Anything");
                        client.DefaultRequestHeaders.Add("Accept", "application/octet-stream");
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GitHubToken);

                        // Esegui la richiesta HTTP
                        HttpResponseMessage response = await client.GetAsync(url);

                        response.EnsureSuccessStatusCode();

                        // Leggi il contenuto dell'asset
                        using (Stream stream = await response.Content.ReadAsStreamAsync())
                        {
                            using (FileStream fileStream = File.Create(file_name))
                            {
                                await stream.CopyToAsync(fileStream);
                            }
                        }

                        Console.WriteLine("Update complete! Please restart the application.");
                        Console.WriteLine("Download completato.");
                    }
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            download_not_error = false;
            Console.WriteLine($"Errore durante il download degli aggiornamenti: {ex.Message}");
        }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Triage This is being looked at and prioritized Type: Documentation Improvements or additions to documentation
Projects
Archived in project
Development

No branches or pull requests

2 participants