Pulizia codice caching control + insomnia test file
This commit is contained in:
@@ -135,7 +135,7 @@ namespace Lux.API.Controllers
|
|||||||
Stopwatch sw = new Stopwatch();
|
Stopwatch sw = new Stopwatch();
|
||||||
sw.Start();
|
sw.Start();
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(id))
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
string filePath = Path.Combine(basePath, "static", id);
|
string filePath = Path.Combine(basePath, "static", id);
|
||||||
@@ -143,72 +143,11 @@ namespace Lux.API.Controllers
|
|||||||
if (!System.IO.File.Exists(filePath))
|
if (!System.IO.File.Exists(filePath))
|
||||||
return NotFound();
|
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
|
// MIME automatico
|
||||||
var provider = new FileExtensionContentTypeProvider();
|
var provider = new FileExtensionContentTypeProvider();
|
||||||
if (!provider.TryGetContentType(filePath, out string mimeType))
|
if (!provider.TryGetContentType(filePath, out string mimeType))
|
||||||
mimeType = "application/octet-stream";
|
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
|
// Last-Modified
|
||||||
var lastModified = System.IO.File.GetLastWriteTimeUtc(filePath);
|
var lastModified = System.IO.File.GetLastWriteTimeUtc(filePath);
|
||||||
Response.Headers["Last-Modified"] = lastModified.ToString("R");
|
Response.Headers["Last-Modified"] = lastModified.ToString("R");
|
||||||
@@ -216,6 +155,10 @@ namespace Lux.API.Controllers
|
|||||||
// Cache-Control (1 giorno)
|
// Cache-Control (1 giorno)
|
||||||
Response.Headers["Cache-Control"] = "public,max-age=86400";
|
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
|
// Se il client ha già la versione aggiornata → 304
|
||||||
if (Request.Headers.TryGetValue("If-None-Match", out var inm) &&
|
if (Request.Headers.TryGetValue("If-None-Match", out var inm) &&
|
||||||
inm.ToString() == etag)
|
inm.ToString() == etag)
|
||||||
@@ -236,9 +179,6 @@ namespace Lux.API.Controllers
|
|||||||
|
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
Log.Info($"{mimeType} | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
Log.Info($"{mimeType} | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||||
#if false
|
|
||||||
return File(bytes, mimeType);
|
|
||||||
#endif
|
|
||||||
return File(stream, mimeType, enableRangeProcessing: true);
|
return File(stream, mimeType, enableRangeProcessing: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user