4 public function __construct($score_id) {
5 $this->score_id = $score_id;
9 public function get_filename($dir, $ext)
11 $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000);
12 return sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext);
16 public function show($dir, $ext, $content_type)
18 $filename = $this->get_filename($dir, $ext);
20 if (!file_exists($filename)) {
21 http_response_code(404);
25 $contents = file_get_contents($filename);
26 $etag = md5($contents);
28 if ($etag === filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH')) {
29 http_response_code(304);
33 $content_encoding = self::get_content_encoding();
34 header("Etag: ".$etag);
35 header("Content-Type: ".$content_type);
37 if ($content_encoding !== FALSE) {
38 header("Content-Encoding: ".$content_encoding);
41 echo gzdecode($contents);
46 public function save($dir, $ext, $contents)
48 if ($outputs === FALSE) return;
50 umask(2); // Group書き込み権限許可
52 $filename = $this->get_filename($dir, $ext);
53 $dirname = dirname($filename);
55 if (!file_exists($dirname)) {
56 mkdir($dirname, 02775, TRUE);
59 $zp = gzopen($filename, "w9");
61 foreach ($contents as $line) {
70 public function exists($dir, $ext)
72 $dirname = sprintf("%s/%d", $dir, floor($this->score_id / 1000) * 1000);
73 $filename = sprintf("%s/%d.%s.gz", $dirname, $this->score_id, $ext);
75 return file_exists($filename);
79 private static function browser_accept_encodings()
81 $accept_encoding = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_ENCODING');
82 if ($accept_encoding == NULL) return [];
84 return array_map('trim', explode(",", $accept_encoding));
88 private static function get_content_encoding()
90 $supported_gzip_encodings = array_intersect(
91 self::browser_accept_encodings(),
94 return count($supported_gzip_encodings) > 0 ?
95 $supported_gzip_encodings[0] : FALSE;