Skip to content
This repository has been archived by the owner on Oct 18, 2020. It is now read-only.

Commit

Permalink
Add weak ETag based caching for served files - #315
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Jul 17, 2017
1 parent 515500a commit 480e647
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const async = require("async");
const Busboy = require("busboy");
const chalk = require("chalk");
const escRe = require("escape-string-regexp");
const etag = require("etag");
const fs = require("graceful-fs");
const imgSize = require("image-size");
const readdirp = require("readdirp");
Expand Down Expand Up @@ -1002,7 +1003,7 @@ function handleFileRequest(req, res, download) {
if (stats.isDirectory() && shareLink) {
streamArchive(req, res, filepath, download);
} else {
streamFile(req, res, filepath, download, stats);
streamFile(req, res, filepath, download, stats, shareLink);
}
} else {
if (error.code === "ENOENT") {
Expand Down Expand Up @@ -1324,8 +1325,8 @@ function streamArchive(req, res, zipPath, download) {
});
}

function streamFile(req, res, filepath, download, stats) {
// send exprects a url-encoded argument
function streamFile(req, res, filepath, download, stats, shareLink) {
// send expects a url-encoded argument
sendFile(req, encodeURIComponent(utils.removeFilesPath(filepath).substring(1)), {
root: paths.files,
dotfiles: "allow",
Expand All @@ -1334,7 +1335,9 @@ function streamFile(req, res, filepath, download, stats) {
cacheControl: false,
}).on("headers", function(res) {
res.setHeader("Content-Type", utils.contentType(filepath));
res.setHeader("Cache-Control", "private, no-store, max-age=0");
res.setHeader("Cache-Control",
shareLink ? "public, max-age=0" : "private, no-store, max-age=0");
res.setHeader('ETag', etag(filepath + "/" + stats.mtime));
if (download) {
res.setHeader("Content-Disposition", utils.getDispo(filepath));
}
Expand Down

0 comments on commit 480e647

Please sign in to comment.