diff --git a/cache/handle_404.php b/cache/handle_404.php index 53e384c..0181921 100644 --- a/cache/handle_404.php +++ b/cache/handle_404.php @@ -8,6 +8,8 @@ require_once __DIR__ . "/../required.php"; +header("Content-Type: text/plain;utf-8"); + $urlparts = explode("/", $_GET['file']); $fileparts = explode(".", end($urlparts)); @@ -21,12 +23,23 @@ if (!preg_match("/^[A-Za-z0-9\-!_]+$/", $fileparts[0])) { die("Encoded image URL invalid, refusing to parse."); } -$url = Base64::decode($fileparts[0]); +if (strpos($fileparts[0], "SHA1_") === 0) { + $hash = str_replace("SHA1_", "", $fileparts[0]); + if ($database->has("imagecache", ["hash" => $hash])) { + $url = $database->get("imagecache", 'url', ["hash" => $hash]); + echo $url; + } else { + http_response_code(404); + die("Not found."); + } +} else { + $url = Base64::decode($fileparts[0]); +} if (!filter_var($url, FILTER_VALIDATE_URL)) { http_response_code(403); die("Invalid URL."); } -//header("Content-Type: image/jpeg"); -echo Thumbnail::addThumbnailToCache($url, (int) $fileparts[1]); \ No newline at end of file +header("Content-Type: image/jpeg"); +echo Thumbnail::addThumbnailToCache($url, (int) $fileparts[1]); diff --git a/database.mwb b/database.mwb index 26402ce..572f41e 100644 Binary files a/database.mwb and b/database.mwb differ diff --git a/lib/Thumbnail.lib.php b/lib/Thumbnail.lib.php index 12bb2a6..3eb5084 100644 --- a/lib/Thumbnail.lib.php +++ b/lib/Thumbnail.lib.php @@ -41,22 +41,44 @@ class Thumbnail { } /** - * Make a thumbnail, save it to the disk cache, and return a url relative to - * the app root. + * Return a thumbnail URL relative to the app root for the given image URL. * @param string $url * @param int $width * @param int,bool $height * @return string */ static function getThumbnailCacheURL(string $url, int $width = 150, $height = true): string { + global $database; $encodedfilename = Base64::encode($url); + if (strlen("$encodedfilename.$width.jpg") > 250) { + // We're too long for common filesystems + $encodedfilename = "SHA1_" . sha1($url); + if (!$database->has("imagecache", ["url" => $url])) { + $database->insert("imagecache", ["url" => $url, "hash" => $encodedfilename, "created" => date("Y-m-d H:i:s")]); + } + } $path = "cache/thumb/$encodedfilename.$width.jpg"; return $path; } + /** + * Generate a thumbnail and save it to the cache + * @param string $url + * @param int $width + * @param type $height + * @return type + */ static function addThumbnailToCache(string $url, int $width = 150, $height = true) { + global $database; $encodedfilename = Base64::encode($url); + if (strlen("$encodedfilename.$width.jpg") > 250) { + // We're too long for common filesystems + $encodedfilename = "SHA1_" . sha1($url); + if (!$database->has("imagecache", ["url" => $url])) { + $database->insert("imagecache", ["url" => $url, "hash" => $encodedfilename, "created" => date("Y-m-d H:i:s")]); + } + } $path = "cache/thumb/$encodedfilename.$width.jpg"; $image = self::getThumbnailFromUrl($url, $width, $height); file_put_contents(__DIR__ . "/../$path", $image);