-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathChannelImageController.cs
More file actions
98 lines (89 loc) · 3.35 KB
/
Copy pathChannelImageController.cs
File metadata and controls
98 lines (89 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Microsoft.AspNetCore.Mvc;
using TelegramDownloader.Data;
using TelegramDownloader.Models;
namespace TelegramDownloader.Controllers
{
[Route("api/channel")]
[ApiController]
public class ChannelImageController : ControllerBase
{
private readonly ITelegramService _ts;
private readonly ILogger<ChannelImageController> _logger;
private static readonly string CacheFolder = Path.Combine(Path.GetTempPath(), "TFM_ChannelImages");
private static readonly SemaphoreSlim _downloadLock = new SemaphoreSlim(1, 1);
public ChannelImageController(ITelegramService ts, ILogger<ChannelImageController> logger)
{
_ts = ts;
_logger = logger;
// Ensure cache folder exists
if (!Directory.Exists(CacheFolder))
{
Directory.CreateDirectory(CacheFolder);
}
}
[HttpGet("image/{channelId}")]
[ResponseCache(Duration = 86400)] // Cache for 24 hours
public async Task<IActionResult> GetChannelImage(long channelId)
{
try
{
var cachedPath = Path.Combine(CacheFolder, $"{channelId}.jpg");
// Check if image is cached
if (System.IO.File.Exists(cachedPath))
{
var fileBytes = await System.IO.File.ReadAllBytesAsync(cachedPath);
return File(fileBytes, "image/jpeg");
}
// Download and cache the image
await _downloadLock.WaitAsync();
try
{
// Double-check after acquiring lock
if (System.IO.File.Exists(cachedPath))
{
var fileBytes = await System.IO.File.ReadAllBytesAsync(cachedPath);
return File(fileBytes, "image/jpeg");
}
var imageBytes = await _ts.DownloadChannelPhoto(channelId);
if (imageBytes != null && imageBytes.Length > 0)
{
await System.IO.File.WriteAllBytesAsync(cachedPath, imageBytes);
return File(imageBytes, "image/jpeg");
}
}
finally
{
_downloadLock.Release();
}
// Return 404 if no image
return NotFound();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting channel image for {ChannelId}", channelId);
return NotFound();
}
}
[HttpDelete("image/cache")]
public IActionResult ClearCache()
{
try
{
if (Directory.Exists(CacheFolder))
{
var files = Directory.GetFiles(CacheFolder, "*.jpg");
foreach (var file in files)
{
System.IO.File.Delete(file);
}
}
return Ok(new { message = "Cache cleared" });
}
catch (Exception ex)
{
_logger.LogError(ex, "Error clearing image cache");
return StatusCode(500, new { error = "Failed to clear cache" });
}
}
}
}