summaryrefslogtreecommitdiff
path: root/init.php
diff options
context:
space:
mode:
Diffstat (limited to 'init.php')
-rw-r--r--init.php143
1 files changed, 143 insertions, 0 deletions
diff --git a/init.php b/init.php
new file mode 100644
index 0000000..95e6f72
--- /dev/null
+++ b/init.php
@@ -0,0 +1,143 @@
+<?php
+require_once __DIR__ . "/vendor/autoload.php";
+
+use \Aws\S3\S3Client;
+use \Aws\Exception\AwsException;
+
+class Cache_S3 extends Plugin implements Cache_Adapter {
+
+ /** @var S3Client $s3 */
+ private $s3;
+ private $dir;
+
+ const CACHE_S3_ENDPOINT = "CACHE_S3_ENDPOINT";
+ const CACHE_S3_BUCKET = "CACHE_S3_BUCKET";
+ const CACHE_S3_REGION = "CACHE_S3_REGION";
+ const CACHE_S3_ACCESS_KEY = "CACHE_S3_ACCESS_KEY";
+ const CACHE_S3_SECRET_KEY = "CACHE_S3_SECRET_KEY";
+
+ function about() {
+ return array(null,
+ "File cache backend using S3 protocol (experimental)",
+ "fox",
+ true);
+ }
+
+ function init($host) {
+ Config::add(self::CACHE_S3_ENDPOINT, "", Config::T_STRING);
+ Config::add(self::CACHE_S3_BUCKET, "", Config::T_STRING);
+ Config::add(self::CACHE_S3_REGION, "", Config::T_STRING);
+ Config::add(self::CACHE_S3_ACCESS_KEY, "", Config::T_STRING);
+ Config::add(self::CACHE_S3_SECRET_KEY, "", Config::T_STRING);
+
+ $this->s3 = new Aws\S3\S3Client([
+ 'version' => 'latest',
+ 'region' => Config::get(self::CACHE_S3_REGION),
+ 'endpoint' => Config::get(self::CACHE_S3_ENDPOINT),
+ 'use_path_style_endpoint' => true,
+ 'credentials' => [
+ 'key' => Config::get(self::CACHE_S3_ACCESS_KEY),
+ 'secret' => Config::get(self::CACHE_S3_SECRET_KEY)
+ ],
+ ]);
+
+ $this->s3->registerStreamWrapper();
+ }
+
+ public function set_dir(string $dir): void {
+ $this->dir = $dir;
+
+ /* $this->s3->putBucketLifecycleConfiguration([
+ 'Bucket' => Config::get(self::CACHE_S3_BUCKET),
+ 'LifecycleConfiguration' => [
+ 'Rules' => [
+ [
+ 'Expiration' => [
+ 'Days' => (int)Config::CACHE_MAX_DAYS,
+ 'ExpiredObjectDeleteMarker' => false,
+ ],
+ 'Prefix' => $this->dir,
+ 'Status' => 'Enabled'
+ ],
+ ],
+ ]
+ ]); */
+ }
+
+ public function get_dir(): string {
+ return $this->dir;
+ }
+
+ public function make_dir(): bool {
+ return true;
+ }
+
+ public function is_writable(?string $filename = null): bool {
+ return true;
+ }
+
+ public function exists(string $filename): bool {
+ return $this->s3->doesObjectExist(Config::get(self::CACHE_S3_BUCKET),
+ $this->dir . '/' . $filename);
+ }
+
+ public function get_size(string $filename) {
+ return filesize($this->get_full_path($filename));
+ }
+
+ public function put(string $filename, $data) {
+ try {
+ $this->s3->putObject([
+ 'Bucket' => Config::get(self::CACHE_S3_BUCKET),
+ 'Key' => $this->dir . '/' . $filename,
+ 'Body' => $data
+ ]);
+ } catch (AwsException $e) {
+ //
+ }
+ }
+
+ public function touch(string $filename): bool {
+ return true; // no-op
+ }
+
+ public function get(string $filename): ?string {
+ $res = $this->s3->getObject([
+ 'Bucket' => Config::get(self::CACHE_S3_BUCKET),
+ 'Key' => $this->dir . '/' . $filename
+ ]);
+
+ return $res['Body']->__toString();
+ }
+
+ public function get_full_path(string $filename): string {
+ return 's3://' . Config::get(self::CACHE_S3_BUCKET) . '/' . $this->dir . '/' . $filename;
+ }
+
+ public function get_mime_type(string $filename) {
+ $fh = fopen($this->get_full_path($filename), 'r');
+
+ if ($fh) {
+ $mimetype = mime_content_type($fh);
+
+ fclose($fh);
+
+ return $mimetype;
+ }
+
+ return false;
+ }
+
+ public function send(string $filename) {
+ return readfile($this->get_full_path($filename));
+ }
+
+ public function expire_all(): void {
+ // TODO
+ }
+
+ function api_version() {
+ return 2;
+ }
+
+}