Skip to content

Commit

Permalink
Upgrade to .NET 6.0 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiascibien authored Jun 23, 2022
1 parent d539fda commit 7ed678c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS build
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
WORKDIR /app

# copy csproj and restore as distinct layers
Expand All @@ -13,7 +13,7 @@ WORKDIR /app/image-search-bot
RUN dotnet publish -c Release -o out

# run application
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-alpine AS runtime
FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine AS runtime
WORKDIR /app
COPY --from=build /app/image-search-bot/out ./
CMD dotnet image-search-bot.dll
11 changes: 6 additions & 5 deletions ImageSearch/BingImageSearch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using ImageSearchBot.Config;
using Microsoft.Azure.CognitiveServices.Search.ImageSearch;

Expand All @@ -22,7 +24,7 @@ public override void Dispose()
_client.Dispose();
}

public override byte[] GetImage(int index)
public override async Task<byte[]> GetImageAsync(int index)
{
var imageResults = _client.Images.SearchAsync(
Config.Query,
Expand All @@ -32,10 +34,9 @@ public override byte[] GetImage(int index)
var clampedIndex = Math.Clamp(index, 0, imageResults.Value.Count);

var imageUrl = imageResults.Value[clampedIndex].ContentUrl;
using (var webClient = new WebClient())
{
return webClient.DownloadData(imageUrl);
}
using var webClient = new HttpClient();

return await webClient.GetByteArrayAsync(imageUrl);
}
}
}
7 changes: 4 additions & 3 deletions ImageSearch/IImageSearch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using ImageSearchBot.Config;

namespace ImageSearchBot.ImageSearch
Expand All @@ -8,8 +9,8 @@ public interface IImageSearch : IDisposable
string BotPrefix { get; }

int MaxImages { get; }
byte[] GetImage(int index);

Task<byte[]> GetImageAsync(int index);
}

public abstract class ImageSearch : IImageSearch
Expand All @@ -28,6 +29,6 @@ protected ImageSearch(string botPrefix, ImageSearchConfig config)

public abstract void Dispose();

public abstract byte[] GetImage(int index);
public abstract Task<byte[]> GetImageAsync(int index);
}
}
2 changes: 1 addition & 1 deletion ImgBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ await _bot.SendTextMessageAsync(message.Chat.Id,
var random = new Random((int)DateTime.Now.Ticks);
if (hasKeyword)
{
var image = _imageSearch.GetImage(random.Next(_imageSearch.MaxImages));
var image = await _imageSearch.GetImageAsync(random.Next(_imageSearch.MaxImages));

using (var memoryStream = new MemoryStream(image))
{
Expand Down
12 changes: 6 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using dotenv.net;
using ImageSearchBot.Config;
using ImageSearchBot.ImageSearch;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace ImageSearchBot
{
Expand Down Expand Up @@ -58,11 +58,11 @@ public static void Main(string[] args)
private static void CreateAndRunBot(string config)
{
var contents = File.ReadAllText(config);
var cfg = JsonConvert.DeserializeObject<RootConfig>(contents,
new JsonSerializerSettings()
var cfg = JsonSerializer.Deserialize<RootConfig>(contents,
new JsonSerializerOptions()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
});

Assert.Check(cfg != null, "Root configuration must be set");
Expand Down
37 changes: 18 additions & 19 deletions image-search-bot.csproj
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>ImageSearchBot</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>ImageSearchBot</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotenv.net" Version="1.0.6" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.Search.ImageSearch" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="Telegram.Bot" Version="14.12.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="dotenv.net" Version="1.0.6" />
<PackageReference Include="Microsoft.Azure.CognitiveServices.Search.ImageSearch" Version="2.0.0" />
<PackageReference Include="Telegram.Bot" Version="14.12.0" />
</ItemGroup>

<ItemGroup>
<None Update=".env">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="mikesmother.botconfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update=".env">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="mikesmother.botconfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit 7ed678c

Please sign in to comment.