4 public function __construct($score_id) {
5 $this->score_id = $score_id;
8 public function show($dir, $ext, $content_type)
10 $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000);
11 $filename = sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext);
13 if (!file_exists($filename)) {
14 http_response_code(404);
18 $contents = file_get_contents($filename);
19 $etag = md5($contents);
21 if ($etag === filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH')) {
22 http_response_code(304);
26 $content_encoding = self::get_content_encoding();
27 header("Etag: ".$etag);
28 header("Content-Type: ".$content_type);
30 if ($content_encoding !== FALSE) {
31 header("Content-Encoding: ".$content_encoding);
34 echo gzdecode($contents);
38 private static function browser_accept_encodings()
40 if (!isset($_SERVER["HTTP_ACCEPT_ENCODING"])) return [];
42 return array_map('trim', explode(",", $_SERVER["HTTP_ACCEPT_ENCODING"]));
46 private static function get_content_encoding()
48 $supported_gzip_encodings = array_intersect(
49 self::browser_accept_encodings(),
52 return count($supported_gzip_encodings) > 0 ?
53 $supported_gzip_encodings[0] : FALSE;