Skip to content

Commit 0a472ab

Browse files
committed
Emoji export
1 parent 81c427b commit 0a472ab

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Api/v0/Controllers/EmojiController.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace devanewbot.Api.v0.Controllers;
2+
3+
using System.IO;
4+
using System.IO.Compression;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using System.Net.Mime;
8+
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Mvc;
10+
using SlackNet;
11+
12+
[Route("/api/v0/emoji")]
13+
public class EmojiController : Controller
14+
{
15+
protected ISlackApiClient Client { get; }
16+
17+
protected IHttpClientFactory HttpClientFactory { get; }
18+
19+
20+
public EmojiController(ISlackApiClient client, IHttpClientFactory httpClientFactory)
21+
{
22+
Client = client;
23+
HttpClientFactory = httpClientFactory;
24+
}
25+
26+
[HttpGet("export")]
27+
public async Task<IActionResult> Export()
28+
{
29+
var emojis = await Client.Emoji.List();
30+
using var memoryStream = new MemoryStream();
31+
using var archive = new ZipArchive(memoryStream);
32+
using var httpClient = HttpClientFactory.CreateClient();
33+
foreach (var emoji in emojis.Where(e => !e.Value.StartsWith("alias:")))
34+
{
35+
var response = await httpClient.GetStreamAsync(emoji.Value);
36+
var entry = archive.CreateEntry($"{emoji.Key}.png");
37+
var entryStream = entry.Open();
38+
await response.CopyToAsync(entryStream);
39+
}
40+
41+
return File(memoryStream, "application/zip", "emoji.zip");
42+
}
43+
}

Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
var configuration = builder.Configuration;
3434

3535
builder.Services
36+
.AddHttpClient()
3637
.AddSingleton<Slack>()
3738
.AddSingleton<Client>()
3839
.AddTransient<InviteService>()

0 commit comments

Comments
 (0)