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