Resimler için cache oluşturuldu

This commit is contained in:
2025-12-03 20:49:08 +03:00
parent 0759be39b4
commit 8f8b5e37fc
3 changed files with 41 additions and 51 deletions

View File

@@ -1599,6 +1599,40 @@ function resolveTvDataAbsolute(relPath) {
return resolved;
}
function serveCachedFile(req, res, filePath, { maxAgeSeconds = 86400 } = {}) {
if (!fs.existsSync(filePath)) {
return res.status(404).send("Dosya bulunamadı");
}
let stats;
try {
stats = fs.statSync(filePath);
} catch (err) {
return res.status(500).send("Dosya okunamadı");
}
const mtime = stats.mtimeMs;
const etag = `"${stats.size}-${Number(mtime).toString(16)}"`;
const ifNoneMatch = req.headers["if-none-match"];
const ifModifiedSince = req.headers["if-modified-since"]
? new Date(req.headers["if-modified-since"]).getTime()
: null;
if (ifNoneMatch && ifNoneMatch === etag) {
return res.status(304).end();
}
if (ifModifiedSince && ifModifiedSince >= mtime) {
return res.status(304).end();
}
res.setHeader(
"Cache-Control",
`public, max-age=${maxAgeSeconds}, stale-while-revalidate=${maxAgeSeconds}`
);
res.setHeader("ETag", etag);
res.setHeader("Last-Modified", new Date(mtime).toUTCString());
return res.sendFile(filePath);
}
function resolveYoutubeDataAbsolute(relPath) {
const normalized = sanitizeRelative(relPath);
const resolved = path.resolve(YT_DATA_ROOT, normalized);
@@ -4312,72 +4346,28 @@ app.get("/thumbnails/:path(*)", requireAuth, (req, res) => {
const relThumb = req.params.path || "";
const fullPath = resolveThumbnailAbsolute(relThumb);
if (!fullPath) return res.status(400).send("Geçersiz thumbnail yolu");
if (!fs.existsSync(fullPath)) return res.status(404).send("Thumbnail yok");
res.sendFile(fullPath);
return serveCachedFile(req, res, fullPath, { maxAgeSeconds: 60 * 60 * 24 });
});
app.get("/movie-data/:path(*)", requireAuth, (req, res) => {
const relPath = req.params.path || "";
const fullPath = resolveMovieDataAbsolute(relPath);
if (!fullPath) return res.status(400).send("Geçersiz movie data yolu");
if (!fs.existsSync(fullPath)) return res.status(404).send("Dosya bulunamadı");
// Cache kontrolü için dosya değişim zamanını ekle
const stats = fs.statSync(fullPath);
const lastModified = stats.mtime.getTime();
// Eğer client If-Modified-Since header gönderdiyse kontrol et
const ifModifiedSince = req.headers['if-modified-since'];
if (ifModifiedSince) {
const clientTime = new Date(ifModifiedSince).getTime();
if (clientTime >= lastModified) {
return res.status(304).end(); // Not Modified
}
}
// Cache-Control header'larını ayarla
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.setHeader('Last-Modified', new Date(lastModified).toUTCString());
res.sendFile(fullPath);
return serveCachedFile(req, res, fullPath, { maxAgeSeconds: 60 * 60 * 24 });
});
app.get("/yt-data/:path(*)", requireAuth, (req, res) => {
const relPath = req.params.path || "";
const fullPath = resolveYoutubeDataAbsolute(relPath);
if (!fullPath) return res.status(400).send("Geçersiz yt data yolu");
if (!fs.existsSync(fullPath)) return res.status(404).send("Dosya bulunamadı");
res.sendFile(fullPath);
return serveCachedFile(req, res, fullPath, { maxAgeSeconds: 60 * 60 * 24 });
});
app.get("/tv-data/:path(*)", requireAuth, (req, res) => {
const relPath = req.params.path || "";
const fullPath = resolveTvDataAbsolute(relPath);
if (!fullPath) return res.status(400).send("Geçersiz tv data yolu");
if (!fs.existsSync(fullPath)) return res.status(404).send("Dosya bulunamadı");
// Cache kontrolü için dosya değişim zamanını ekle
const stats = fs.statSync(fullPath);
const lastModified = stats.mtime.getTime();
// Eğer client If-Modified-Since header gönderdiyse kontrol et
const ifModifiedSince = req.headers['if-modified-since'];
if (ifModifiedSince) {
const clientTime = new Date(ifModifiedSince).getTime();
if (clientTime >= lastModified) {
return res.status(304).end(); // Not Modified
}
}
// Cache-Control header'larını ayarla
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.setHeader('Last-Modified', new Date(lastModified).toUTCString());
res.sendFile(fullPath);
return serveCachedFile(req, res, fullPath, { maxAgeSeconds: 60 * 60 * 24 });
});
// --- Torrentleri listele ---