mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-04 11:24:44 +00:00
Fixes for Downloading ReleaseAsset zip File #854
This commit addressed the `BuildResponse` wasn't handling response `content-type` `application/octet-stream` for binary items.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Octokit;
|
||||
using Octokit.Tests.Integration;
|
||||
@@ -188,8 +191,45 @@ public class ReleasesClientTests
|
||||
|
||||
var response = await _github.Connection.Get<object>(new Uri(asset.Url), new Dictionary<string, string>(), "application/octet-stream");
|
||||
|
||||
Assert.Equal("This is a plain text file.", response.Body);
|
||||
Assert.Contains("This is a plain text file.", Encoding.ASCII.GetString((byte[]) response.Body));
|
||||
}
|
||||
[IntegrationTest]
|
||||
public async Task CanDownloadBinaryAsset()
|
||||
{
|
||||
var releaseWithNoUpdate = new NewRelease("0.1") { Draft = true };
|
||||
var release = await _releaseClient.Create(_context.RepositoryOwner, _context.RepositoryName, releaseWithNoUpdate);
|
||||
|
||||
var stream = Helper.LoadFixture("hello-world.zip");
|
||||
|
||||
var newAsset = new ReleaseAssetUpload("hello-world.zip"
|
||||
, "application/octet-stream"
|
||||
, stream
|
||||
, null);
|
||||
|
||||
var result = await _releaseClient.UploadAsset(release, newAsset);
|
||||
|
||||
Assert.True(result.Id > 0);
|
||||
|
||||
var asset = await _releaseClient.GetAsset(_context.RepositoryOwner, _context.RepositoryName, result.Id);
|
||||
|
||||
Assert.Equal(result.Id, asset.Id);
|
||||
|
||||
var response = await _github.Connection.Get<object>(new Uri(asset.Url), new Dictionary<string, string>(), "application/octet-stream");
|
||||
|
||||
var textContent = String.Empty;
|
||||
|
||||
using (var zipstream = new MemoryStream((byte[]) response.Body))
|
||||
using(var archive = new ZipArchive(zipstream))
|
||||
{
|
||||
var enttry = archive.Entries[0];
|
||||
var data = new byte[enttry.Length];
|
||||
await enttry.Open().ReadAsync(data, 0, data.Length);
|
||||
textContent = Encoding.ASCII.GetString(data);
|
||||
}
|
||||
|
||||
Assert.Contains("This is a plain text file.",textContent );
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
@@ -38,6 +38,8 @@
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.IO.Compression" />
|
||||
<Reference Include="System.IO.Compression.FileSystem" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Reactive.Core, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
@@ -144,6 +146,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<EmbeddedResource Include="fixtures\hello-world.zip" />
|
||||
<None Include="packages.config">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
|
||||
Binary file not shown.
@@ -70,16 +70,22 @@ namespace Octokit.Internal
|
||||
|
||||
object responseBody = null;
|
||||
string contentType = null;
|
||||
|
||||
// We added support for downloading images,zip-files and application/octet-stream.
|
||||
// Let's constrain this appropriately.
|
||||
var binaryContentTypes = new[] {"application/zip"
|
||||
,"application/x-gzip",
|
||||
"application/octet-stream"};
|
||||
|
||||
using (var content = responseMessage.Content)
|
||||
{
|
||||
if (content != null)
|
||||
{
|
||||
contentType = GetContentMediaType(responseMessage.Content);
|
||||
|
||||
// We added support for downloading images and zip-files. Let's constrain this appropriately.
|
||||
if (contentType != null && (contentType.StartsWith("image/")
|
||||
|| contentType.Equals("application/zip", StringComparison.OrdinalIgnoreCase)
|
||||
|| contentType.Equals("application/x-gzip", StringComparison.OrdinalIgnoreCase)))
|
||||
if (contentType != null && (contentType.StartsWith("image/") ||
|
||||
binaryContentTypes
|
||||
.Any(item => item.Equals(contentType,StringComparison.OrdinalIgnoreCase))))
|
||||
{
|
||||
responseBody = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user