Support including a short hash in json urls.
This commit is contained in:
parent
c8dc4aaac4
commit
1221acdf53
1 changed files with 64 additions and 24 deletions
|
@ -1,8 +1,14 @@
|
|||
<?php
|
||||
define('FTM_ROOT', __DIR__ . '/..');
|
||||
define('FTM_HASH', 'sha1');
|
||||
define('FTM_PATH_PUB', FTM_ROOT . '/public');
|
||||
define('FTM_PATH_PRV', FTM_ROOT . '/private');
|
||||
|
||||
define('FTM_COMMON_CONFIG', FTM_PATH_PRV . '/common.ini');
|
||||
define('FTM_SOUND_LIBRARY', FTM_PATH_PRV . '/sound-library.ini');
|
||||
define('FTM_SOUND_PACKS', FTM_PATH_PRV . '/sound-packs.ini');
|
||||
define('FTM_TEXT_TRIGGERS', FTM_PATH_PRV . '/text-triggers.ini');
|
||||
|
||||
define('FTM_LEGACY_SOUND_TYPE', [
|
||||
'opus' => 'audio/ogg',
|
||||
'ogg' => 'audio/ogg',
|
||||
|
@ -11,6 +17,14 @@ define('FTM_LEGACY_SOUND_TYPE', [
|
|||
'wav' => 'audio/wav',
|
||||
]);
|
||||
|
||||
define('FTM_FILE_HASH_MAP', [
|
||||
'common' => [FTM_COMMON_CONFIG],
|
||||
'sounds' => [FTM_SOUND_LIBRARY, FTM_SOUND_PACKS],
|
||||
'sounds2' => [FTM_SOUND_LIBRARY, FTM_SOUND_PACKS],
|
||||
'texttriggers' => [FTM_TEXT_TRIGGERS],
|
||||
'soundtriggers' => [FTM_TEXT_TRIGGERS],
|
||||
]);
|
||||
|
||||
header('X-Powered-By: Futami');
|
||||
header('Cache-Control: max-age=86400, must-revalidate');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
|
@ -34,6 +48,24 @@ function json_out($data): void {
|
|||
exit;
|
||||
}
|
||||
|
||||
function ftm_hash_str(string $data): string {
|
||||
return hash(FTM_HASH, $data);
|
||||
}
|
||||
function ftm_hash_file(string $path): string {
|
||||
return hash_file(FTM_HASH, $path);
|
||||
}
|
||||
function hash_mapped_file(string $name): string {
|
||||
$files = FTM_FILE_HASH_MAP[$name] ?? [];
|
||||
if(count($files) === 1)
|
||||
return ftm_hash_file($files[0]);
|
||||
|
||||
$data = '';
|
||||
foreach($files as $file)
|
||||
$data .= file_get_contents($file);
|
||||
|
||||
return ftm_hash_str($data);
|
||||
}
|
||||
|
||||
function match_etag($eTag): void {
|
||||
if(filter_input(INPUT_SERVER, 'HTTP_IF_NONE_MATCH') === $eTag) {
|
||||
http_response_code(304);
|
||||
|
@ -41,10 +73,10 @@ function match_etag($eTag): void {
|
|||
}
|
||||
}
|
||||
function gen_etag(string $user, string $data): string {
|
||||
return sprintf('W/"%s-%s"', hash('sha1', $data), $user);
|
||||
return sprintf('W/"%s-%s"', ftm_hash_str($data), $user);
|
||||
}
|
||||
function gen_etag_file(string $user, string $path): string {
|
||||
return gen_etag($user, hash_file('sha1', $path));
|
||||
return gen_etag($user, ftm_hash_file($path));
|
||||
}
|
||||
function etag(string $etag): void {
|
||||
match_etag($etag);
|
||||
|
@ -60,22 +92,32 @@ function etag_file(string $user, string $path): void {
|
|||
$reqPath = '/' . trim(parse_url((string)filter_input(INPUT_SERVER, 'REQUEST_URI'), PHP_URL_PATH), '/');
|
||||
|
||||
if($reqPath === '/common.json') {
|
||||
$commonPath = FTM_PATH_PRV . '/common.ini';
|
||||
etag_file('common', $commonPath);
|
||||
etag_file('common', FTM_COMMON_CONFIG);
|
||||
|
||||
$common = parse_ini_file($commonPath, false, INI_SCANNER_TYPED);
|
||||
$config = parse_ini_file(FTM_COMMON_CONFIG, false, INI_SCANNER_TYPED);
|
||||
$common = new stdClass;
|
||||
|
||||
$colours = $common['colours'];
|
||||
$common['colours'] = [];
|
||||
foreach($colours as $n => $c)
|
||||
$common['colours'][] = compact('n', 'c');
|
||||
foreach($config as $key => $value) {
|
||||
if($key === 'colours') {
|
||||
$dict = $value;
|
||||
$value = [];
|
||||
foreach($dict as $name => $raw)
|
||||
$value[] = ['n' => $name, 'c' => $raw];
|
||||
} elseif(is_string($value) && str_starts_with($value, '::')) {
|
||||
$parts = explode(':', substr($value, 2));
|
||||
$hash = hash_mapped_file($parts[0]);
|
||||
$value = sprintf($parts[1], $hash);
|
||||
}
|
||||
|
||||
$common->{$key} = $value;
|
||||
}
|
||||
|
||||
json_out($common);
|
||||
}
|
||||
|
||||
if($reqPath === '/sounds2.json') {
|
||||
$sndLibData = file_get_contents(FTM_PATH_PRV . '/sound-library.ini');
|
||||
$sndPackData = file_get_contents(FTM_PATH_PRV . '/sound-packs.ini');
|
||||
if(preg_match('#^/sounds2(\.[a-f0-9]{8})?.json$#', $reqPath)) {
|
||||
$sndLibData = file_get_contents(FTM_SOUND_LIBRARY);
|
||||
$sndPackData = file_get_contents(FTM_SOUND_PACKS);
|
||||
|
||||
etag_data('sounds2', $sndLibData . $sndPackData);
|
||||
|
||||
|
@ -101,9 +143,9 @@ if($reqPath === '/sounds2.json') {
|
|||
json_out(compact('library', 'packs'));
|
||||
}
|
||||
|
||||
if($reqPath === '/sounds.json') {
|
||||
$sndLibData = file_get_contents(FTM_PATH_PRV . '/sound-library.ini');
|
||||
$sndPackData = file_get_contents(FTM_PATH_PRV . '/sound-packs.ini');
|
||||
if(preg_match('#^/sounds(\.[a-f0-9]{8})?.json$#', $reqPath)) {
|
||||
$sndLibData = file_get_contents(FTM_SOUND_LIBRARY);
|
||||
$sndPackData = file_get_contents(FTM_SOUND_PACKS);
|
||||
|
||||
etag_data('sounds', $sndLibData . $sndPackData);
|
||||
|
||||
|
@ -138,11 +180,10 @@ if($reqPath === '/sounds.json') {
|
|||
json_out(compact('library', 'packs'));
|
||||
}
|
||||
|
||||
if($reqPath === '/texttriggers.json') {
|
||||
$textTriggersPath = FTM_PATH_PRV . '/text-triggers.ini';
|
||||
etag_file('texttriggers', $textTriggersPath);
|
||||
if(preg_match('#^/texttriggers(\.[a-f0-9]{8})?.json$#', $reqPath)) {
|
||||
etag_file('texttriggers', FTM_TEXT_TRIGGERS);
|
||||
|
||||
$triggers = parse_ini_file($textTriggersPath, true, INI_SCANNER_TYPED);
|
||||
$triggers = parse_ini_file(FTM_TEXT_TRIGGERS, true, INI_SCANNER_TYPED);
|
||||
|
||||
if($triggers === false)
|
||||
$triggers = [];
|
||||
|
@ -152,12 +193,11 @@ if($reqPath === '/texttriggers.json') {
|
|||
json_out($triggers);
|
||||
}
|
||||
|
||||
if($reqPath === '/soundtriggers.json') {
|
||||
$textTriggersPath = FTM_PATH_PRV . '/text-triggers.ini';
|
||||
etag_file('soundtriggers', $textTriggersPath);
|
||||
if(preg_match('#^/soundtriggers(\.[a-f0-9]{8})?.json$#', $reqPath)) {
|
||||
etag_file('soundtriggers', FTM_TEXT_TRIGGERS);
|
||||
|
||||
$textTriggers = parse_ini_file($textTriggersPath, true, INI_SCANNER_TYPED);
|
||||
$sndLib = parse_ini_file(FTM_PATH_PRV . '/sound-library.ini', true, INI_SCANNER_TYPED);
|
||||
$textTriggers = parse_ini_file(FTM_TEXT_TRIGGERS, true, INI_SCANNER_TYPED);
|
||||
$sndLib = parse_ini_file(FTM_SOUND_LIBRARY, true, INI_SCANNER_TYPED);
|
||||
$soundTrigs = [];
|
||||
|
||||
foreach($textTriggers as $triggerInfo) {
|
||||
|
|
Loading…
Reference in a new issue