feat(web/image_hosting): get_image

This commit is contained in:
Baoshuo Ren 2022-10-12 21:10:07 +08:00
parent 7c631ea72e
commit 2d9d99e1a6
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
4 changed files with 27 additions and 5 deletions

View File

@ -4,3 +4,21 @@
if (!Auth::check() && UOJConfig::$data['switch']['force-login']) { if (!Auth::check() && UOJConfig::$data['switch']['force-login']) {
redirectToLogin(); redirectToLogin();
} }
$name = $_GET['image_name'];
if (!validateString($name)) {
become404Page();
}
$file_name = UOJContext::storagePath()."/image_hosting/$name.png";
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $file_name);
if ($mimetype === false) {
become404Page();
}
finfo_close($finfo);
header("X-Sendfile: $file_name");
header("Content-type: $mimetype");
header("Cache-Control: max-age=604800", true);

View File

@ -4,7 +4,7 @@ function uojRand($l, $r) {
return mt_rand($l, $r); return mt_rand($l, $r);
} }
function uojRandString($len, $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { function uojRandString($len, $charset = '0123456789abcdefghijklmnopqrstuvwxyz') {
$n_chars = strlen($charset); $n_chars = strlen($charset);
$str = ''; $str = '';
for ($i = 0; $i < $len; $i++) { for ($i = 0; $i < $len; $i++) {
@ -13,10 +13,10 @@ function uojRandString($len, $charset = '0123456789abcdefghijklmnopqrstuvwxyzABC
return $str; return $str;
} }
function uojRandAvaiableFileName($dir) { function uojRandAvaiableFileName($dir, $length = 20, $suffix = '') {
do { do {
$fileName = $dir . uojRandString(20); $fileName = $dir . uojRandString($length);
} while (file_exists(UOJContext::storagePath().$fileName)); } while (file_exists(UOJContext::storagePath().$fileName.$suffix));
return $fileName; return $fileName;
} }

View File

@ -51,3 +51,7 @@ function validateIP($ip) {
function validateURL($url) { function validateURL($url) {
return filter_var($url, FILTER_VALIDATE_URL) !== false; return filter_var($url, FILTER_VALIDATE_URL) !== false;
} }
function validateString($str) {
return preg_match('/[^0-9a-zA-Z]/', $str) !== true;
}

View File

@ -5,7 +5,7 @@ Route::pattern('id', '[1-9][0-9]{0,9}');
Route::pattern('contest_id', '[1-9][0-9]{0,9}'); Route::pattern('contest_id', '[1-9][0-9]{0,9}');
Route::pattern('tab', '\S{1,20}'); Route::pattern('tab', '\S{1,20}');
Route::pattern('rand_str_id', '[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]{20}'); Route::pattern('rand_str_id', '[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]{20}');
Route::pattern('image_name', '[0123456789abcdefghijklmnopqrstuvwxyz]{7}'); Route::pattern('image_name', '[0-9a-z]{1,20}');
Route::pattern('upgrade_name', '[a-zA-Z0-9_]{1,50}'); Route::pattern('upgrade_name', '[a-zA-Z0-9_]{1,50}');
Route::group([ Route::group([