using Microsoft.AspNetCore.Mvc; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; namespace Campus_live_rewrite.Controllers { public class ImagesController : Controller { private static readonly HttpClient _recNetClient = new() { Timeout = TimeSpan.FromSeconds(3) }; private static DateTime _recNetRetryAfter = DateTime.MinValue; // Pictures asked for at the site ROOT (/DefaultPFP.png, /pfp_6_....png, /.png - // 400+ such 404s a day): the same files the image store serves under /img. Anything the // static website doesn't have at that name is sent there, query string and all. [HttpGet(@"/{file:regex(^\w[[\w.-]]*\.(png|jpg|jpeg)$)}")] public IActionResult RootImage(string file) => Redirect("/img/" + file + Request.QueryString); [Route("/img/{*img_path}")] public async Task ImgServer(string img_path, int width = 0, int height = 0, string sig = "p1") { img_path = Uri.UnescapeDataString(img_path ?? "").TrimStart('/'); // Confine to Data/Images. This route is catch-all too, and every candidate path // below appends img_path to a subfolder of Data/Images - so "../../../etc/passwd" // walked straight out and File.Exists happily served it. One check on the shared // parent is enough: nothing legitimate lives outside Data/Images, and the deeper // candidate folders are all beneath it. Also blocks the img.rec.net fallback URL // being built from a traversal string. string imagesRoot = Path.Combine(Environment.CurrentDirectory, "Data", "Images"); if (Campus_live_rewrite.api.server.PathSafety.ResolveUnder(imagesRoot, img_path) == null) { Console.WriteLine($"[img] REFUSED path traversal attempt: {img_path}"); return NotFound(); } HttpContext.Response.Headers.Append("content-signature", "key-id=KEY:RSA:p1.rec.net; data=IWwe/pZ5vWWqNSkSM/54isgDxlZkdrP0sUrppKCbNktO2yCOTjq746xWiiLsueGuVcAGQqkjeRTimxolHckS/YXSYkEJxtiCXbLlsRia2DyAqtWVkGWsfczzFhp/56U66FVzolTspPCvjScOVlGO7dDIK7sJ+ndcRauWjsQsC6g3e7rUc6uwY099a6gy7sw6xr5BFZQSz8wg+fqyHYD/Sc4nQQVOTFZNNASqbJYhpNhEMXRnafCMuLl8a3mkGwvy3t4q2D/7SM48xrGZjEV47qNx1A91KCe28XVToFh4BzwEUU8nZ0d+KwV79MGarLo1cY8igc8FcoThKcovI4ClOg=="); HttpContext.Response.Headers.Append("Content-Disposition", $"inline; filename=\"{Path.GetFileName(img_path)}\""); HttpContext.Response.Headers.Append("Access-Control-Allow-Origin", "*"); HttpContext.Response.Headers.Append("Access-Control-Allow-Headers", "*"); Response.Headers["Cache-Control"] = "public, max-age=14400"; HttpContext.Response.Headers.Append("Access-Control-Allow-Headers", "Content-Type, Authorization, Cache-Control"); HttpContext.Response.Headers.Append("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); var etag = $"\"{img_path.GetHashCode()}\""; HttpContext.Response.Headers.Append("ETag", etag); //return File(await System.IO.File.ReadAllBytesAsync(Path.Combine(Environment.CurrentDirectory, "Data", "Images", "imageee.jpg")), "image/png"); bool cropSquare = HttpContext.Request.Query.ContainsKey("cropsquare") && (HttpContext.Request.Query["cropsquare"].ToString().ToLower() == "true" || HttpContext.Request.Query["cropsquare"].ToString() == "1"); string[] paths = new[] { Path.Combine(Environment.CurrentDirectory, "Data", "Images", img_path), Path.Combine(Environment.CurrentDirectory, "Data", "Images", "rro_images", img_path), Path.Combine(Environment.CurrentDirectory, "Data", "Images", "rro_images", "base_rros", img_path), Path.Combine(Environment.CurrentDirectory, "Data", "Images", "player_images", img_path), Path.Combine(Environment.CurrentDirectory, "Data", "Images", "polaroid_images", img_path), Path.Combine(Environment.CurrentDirectory, "Data", "Images", "DefaultImgs", img_path), Path.Combine(Environment.CurrentDirectory, "Data", "Images", "rec_net", img_path), Path.Combine(Environment.CurrentDirectory, "Data", "Images", "custom_shirt_imgs", img_path), Path.Combine(Environment.CurrentDirectory, "Data", "Images", "LoadingScreenTips", img_path) //Path.Combine(Environment.CurrentDirectory, "Data", "cdn_rooms", img_path) }; foreach (var path in paths) { if (System.IO.File.Exists(path)) { try { var imageBytes = await System.IO.File.ReadAllBytesAsync(path); imageBytes = await ProcessImageAsync(imageBytes, cropSquare, width, height); return File(imageBytes, "image/png"); } catch { var fallbackBytes = await System.IO.File.ReadAllBytesAsync(path); return File(fallbackBytes, "image/png"); } } } string recNetLocalPath = Path.Combine(Environment.CurrentDirectory, "Data", "Images", "rec_net", img_path); // img.rec.net is gone. Every miss used to open a fresh HttpClient and sit through a // full TLS failure (seconds) before falling through - hundreds of times per boot, and // each one stalled the request that wanted the image. Now: one shared client with a // short timeout, and after any failure the upstream is skipped for 10 minutes. if (!System.IO.File.Exists(recNetLocalPath) && DateTime.UtcNow >= _recNetRetryAfter) { try { byte[] data = await _recNetClient.GetByteArrayAsync($"https://img.rec.net/{img_path}"); Directory.CreateDirectory(Path.GetDirectoryName(recNetLocalPath)!); await System.IO.File.WriteAllBytesAsync(recNetLocalPath, data); try { var processed = await ProcessImageAsync(data, cropSquare, width, height); return File(processed, GetMimeType(img_path)); } catch { return File(data, GetMimeType(img_path)); } } catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException) { _recNetRetryAfter = DateTime.UtcNow.AddMinutes(10); Console.WriteLine($"[rec_net fetch failed] {ex.Message} - skipping img.rec.net for 10 minutes"); } } else if (System.IO.File.Exists(recNetLocalPath)) { // Read inside the try: the file can be gone between Exists and the read (a // concurrent fetch writing it, a cleanup) and that was a 500 with a stack trace // in the journal instead of the 404 the client handles. try { var data = await System.IO.File.ReadAllBytesAsync(recNetLocalPath); try { var processed = await ProcessImageAsync(data, cropSquare, width, height); return File(processed, GetMimeType(img_path)); } catch { return File(data, GetMimeType(img_path)); } } catch (IOException ex) { Console.WriteLine($"[img] rec_net/{img_path} unreadable: {ex.Message}"); } } // Was: "rro_image" (singular). The directory on disk is "rro_images", and // DefaultRoomImage.jpg actually lives in "rec_net" - so the path was wrong // twice, File.Exists was ALWAYS false, and this fell through to NotFound(). // That is where the client's "HTTP Error 404" entries come from. Try the // real location first, then the plural directory, so a renamed/moved asset // still resolves instead of 404ing. foreach (var dir in new[] { "rec_net", "rro_images", "DefaultImgs" }) { var notFoundImagePath = Path.Combine( Environment.CurrentDirectory, "Data", "Images", dir, "DefaultRoomImage.jpg"); if (System.IO.File.Exists(notFoundImagePath)) { var notFoundImageBytes = await System.IO.File.ReadAllBytesAsync(notFoundImagePath); return File(notFoundImageBytes, "image/jpeg"); } } return NotFound(); } private static string GetMimeType(string filePath) { var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider(); if (!provider.TryGetContentType(filePath, out var contentType)) { contentType = "image/png"; } return contentType; } private static async Task ProcessImageAsync(byte[] imageBytes, bool cropSquare, int width, int height) { using var ms = new MemoryStream(imageBytes); using var image = await Image.LoadAsync(ms); if (cropSquare) { int size = Math.Min(image.Width, image.Height); int x = (image.Width - size) / 2; int y = (image.Height - size) / 2; image.Mutate(ctx => ctx.Crop(new Rectangle(x, y, size, size))); int targetSize = width > 0 ? width : height; if (targetSize > 0) { image.Mutate(ctx => ctx.Resize(targetSize, targetSize)); } } else if (width > 0 || height > 0) { int resizeWidth = width; int resizeHeight = height; if (width > 0 && height == 0) { resizeHeight = (int)((double)image.Height / image.Width * width); } else if (height > 0 && width == 0) { resizeWidth = (int)((double)image.Width / image.Height * height); } image.Mutate(ctx => ctx.Resize(resizeWidth, resizeHeight)); } using var output = new MemoryStream(); await image.SaveAsync(output, new PngEncoder()); return output.ToArray(); } } }