Pulizia codice caching control + insomnia test file
This commit is contained in:
@@ -135,7 +135,7 @@ namespace Lux.API.Controllers
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
|
||||
if (string.IsNullOrEmpty(id))
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
return NotFound();
|
||||
|
||||
string filePath = Path.Combine(basePath, "static", id);
|
||||
@@ -143,72 +143,11 @@ namespace Lux.API.Controllers
|
||||
if (!System.IO.File.Exists(filePath))
|
||||
return NotFound();
|
||||
|
||||
// vers 1
|
||||
#if false
|
||||
string mimeType = "txt";
|
||||
byte[] bytes = new byte[0];
|
||||
string extension = Path.GetExtension(id);
|
||||
string rawContent = "";
|
||||
if (System.IO.File.Exists(filePath))
|
||||
{
|
||||
// fix testo/binario...
|
||||
switch (extension)
|
||||
{
|
||||
case ".svg":
|
||||
mimeType = "image/svg+xml";
|
||||
rawContent = await System.IO.File.ReadAllTextAsync(filePath);
|
||||
bytes = Encoding.UTF8.GetBytes(rawContent);
|
||||
break;
|
||||
|
||||
case ".png":
|
||||
mimeType = "image/png";
|
||||
bytes = await System.IO.File.ReadAllBytesAsync(filePath);
|
||||
break;
|
||||
|
||||
case ".jpg":
|
||||
mimeType = "image/jpg";
|
||||
bytes = await System.IO.File.ReadAllBytesAsync(filePath);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//vers 2 ok
|
||||
#if false
|
||||
string extension = Path.GetExtension(id).ToLowerInvariant();
|
||||
string mimeType = extension switch
|
||||
{
|
||||
".svg" => "image/svg+xml",
|
||||
".png" => "image/png",
|
||||
".jpg" => "image/jpeg",
|
||||
".jpeg" => "image/jpeg",
|
||||
".gif" => "image/gif",
|
||||
_ => "application/octet-stream"
|
||||
};
|
||||
|
||||
byte[] bytes = await System.IO.File.ReadAllBytesAsync(filePath);
|
||||
#endif
|
||||
|
||||
// MIME automatico
|
||||
var provider = new FileExtensionContentTypeProvider();
|
||||
if (!provider.TryGetContentType(filePath, out string mimeType))
|
||||
mimeType = "application/octet-stream";
|
||||
|
||||
#if false
|
||||
// Lettura corretta (sempre binaria)
|
||||
byte[] bytes = await System.IO.File.ReadAllBytesAsync(filePath);
|
||||
#endif
|
||||
|
||||
// ETag (opzionale, vedi sotto)
|
||||
string etag = GenerateETag(filePath);
|
||||
#if false
|
||||
string etag = GenerateETag(bytes);
|
||||
#endif
|
||||
Response.Headers["ETag"] = etag;
|
||||
|
||||
// Last-Modified
|
||||
var lastModified = System.IO.File.GetLastWriteTimeUtc(filePath);
|
||||
Response.Headers["Last-Modified"] = lastModified.ToString("R");
|
||||
@@ -216,6 +155,10 @@ namespace Lux.API.Controllers
|
||||
// Cache-Control (1 giorno)
|
||||
Response.Headers["Cache-Control"] = "public,max-age=86400";
|
||||
|
||||
// ETag
|
||||
string etag = GenerateETag(filePath);
|
||||
Response.Headers["ETag"] = etag;
|
||||
|
||||
// Se il client ha già la versione aggiornata → 304
|
||||
if (Request.Headers.TryGetValue("If-None-Match", out var inm) &&
|
||||
inm.ToString() == etag)
|
||||
@@ -236,9 +179,6 @@ namespace Lux.API.Controllers
|
||||
|
||||
sw.Stop();
|
||||
Log.Info($"{mimeType} | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||
#if false
|
||||
return File(bytes, mimeType);
|
||||
#endif
|
||||
return File(stream, mimeType, enableRangeProcessing: true);
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user