From a5fefffc1d8b725120f57ab2d2cd38c4c970b9d7 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Thu, 14 May 2020 11:22:06 +0300 Subject: [PATCH] Added PHP preload support --- Dockerfile | 20 ++-- app/controllers/api/locale.php | 1 - app/controllers/shared/web.php | 2 +- app/preload.php | 56 +++++++++ src/Appwrite/Preloader/Preloader.php | 128 ++++++++++++++++++++ src/Appwrite/Storage/Devices/S3.php | 172 +++++++++++++++++++++++++-- 6 files changed, 357 insertions(+), 22 deletions(-) create mode 100644 app/preload.php create mode 100644 src/Appwrite/Preloader/Preloader.php diff --git a/Dockerfile b/Dockerfile index 25269eae08..039fe3be6d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -100,14 +100,6 @@ RUN \ apt-get clean && \ rm -rf /var/lib/apt/lists/* -# Set Upload Limit (default to 100MB) -RUN echo "upload_max_filesize = ${_APP_STORAGE_LIMIT}" >> /etc/php/$PHP_VERSION/fpm/conf.d/appwrite.ini -RUN echo "post_max_size = ${_APP_STORAGE_LIMIT}" >> /etc/php/$PHP_VERSION/fpm/conf.d/appwrite.ini -RUN echo "env[TESTME] = your-secret-key" >> /etc/php/$PHP_VERSION/fpm/conf.d/appwrite.ini - -# Add logs file -RUN echo "" >> /var/log/appwrite.log - # Nginx Configuration (with self-signed ssl certificates) COPY ./docker/nginx.conf /etc/nginx/nginx.conf COPY ./docker/ssl/cert.pem /etc/nginx/ssl/cert.pem @@ -136,6 +128,18 @@ RUN mkdir -p /storage/uploads && \ # Supervisord Conf COPY ./docker/supervisord.conf /etc/supervisord.conf +# Set Upload Limit (default to 100MB) +RUN echo "upload_max_filesize = ${_APP_STORAGE_LIMIT}" >> /etc/php/$PHP_VERSION/fpm/conf.d/appwrite.ini +RUN echo "post_max_size = ${_APP_STORAGE_LIMIT}" >> /etc/php/$PHP_VERSION/fpm/conf.d/appwrite.ini +RUN echo "env[TESTME] = your-secret-key" >> /etc/php/$PHP_VERSION/fpm/conf.d/appwrite.ini +RUN echo "opcache.preload_user=www-data" >> /etc/php/$PHP_VERSION/fpm/conf.d/appwrite.ini +RUN echo "opcache.preload=/usr/share/nginx/html/app/preload.php" >> /etc/php/$PHP_VERSION/fpm/conf.d/appwrite.ini +RUN echo "opcache.preload_user=www-data" >> /etc/php/$PHP_VERSION/cli/conf.d/appwrite.ini +RUN echo "opcache.preload=/usr/share/nginx/html/app/preload.php" >> /etc/php/$PHP_VERSION/cli/conf.d/appwrite.ini + +# Add logs file +RUN echo "" >> /var/log/appwrite.log + # Start COPY ./docker/bin/start /start RUN chmod 775 /start diff --git a/app/controllers/api/locale.php b/app/controllers/api/locale.php index 8f4f2e3272..940cb29e80 100644 --- a/app/controllers/api/locale.php +++ b/app/controllers/api/locale.php @@ -150,7 +150,6 @@ $utopia->get('/v1/locale/continents') } ); - $utopia->get('/v1/locale/currencies') ->desc('List Currencies') ->label('scope', 'locale.read') diff --git a/app/controllers/shared/web.php b/app/controllers/shared/web.php index 9f148817a2..b4fd17ca56 100644 --- a/app/controllers/shared/web.php +++ b/app/controllers/shared/web.php @@ -22,7 +22,7 @@ if (!empty($request->getQuery('version', ''))) { $layout ->setParam('title', APP_NAME) ->setParam('protocol', Config::getParam('protocol')) - ->setParam('domain', $domain) + ->setParam('domain', Config::getParam('domain')) ->setParam('home', $request->getServer('_APP_HOME')) ->setParam('setup', $request->getServer('_APP_SETUP')) ->setParam('class', 'unknown') diff --git a/app/preload.php b/app/preload.php new file mode 100644 index 0000000000..4eb8bd9d58 --- /dev/null +++ b/app/preload.php @@ -0,0 +1,56 @@ +paths(realpath(__DIR__ . '/../vendor')) +// //->paths(realpath(__DIR__ . '/config')) +// // ->ignore( +// // \Illuminate\Filesystem\Cache::class, +// // \Illuminate\Log\LogManager::class, +// // \Illuminate\Http\Testing\File::class, +// // \Illuminate\Http\UploadedFile::class, +// // \Illuminate\Support\Carbon::class, +// // ) +// ->load(); \ No newline at end of file diff --git a/src/Appwrite/Preloader/Preloader.php b/src/Appwrite/Preloader/Preloader.php new file mode 100644 index 0000000000..2fb9099663 --- /dev/null +++ b/src/Appwrite/Preloader/Preloader.php @@ -0,0 +1,128 @@ +paths = $paths; + + // We'll use composer's classmap + // to easily find which classes to autoload, + // based on their filename + $classMap = require __DIR__ . '/../../../vendor/composer/autoload_classmap.php'; + + $this->fileMap = array_flip($classMap); + } + + public function paths(string ...$paths): Preloader + { + $this->paths = array_merge( + $this->paths, + $paths + ); + + return $this; + } + + public function ignore(string ...$names): Preloader + { + $this->ignores = array_merge( + $this->ignores, + $names + ); + + return $this; + } + + public function load(): void + { + // We'll loop over all registered paths + // and load them one by one + foreach ($this->paths as $path) { + $this->loadPath(rtrim($path, '/')); + } + + $count = self::$count; + + echo "[Preloader] Preloaded {$count} classes" . PHP_EOL; + } + + private function loadPath(string $path): void + { + // If the current path is a directory, + // we'll load all files in it + if (is_dir($path)) { + $this->loadDir($path); + + return; + } + + // Otherwise we'll just load this one file + $this->loadFile($path); + } + + private function loadDir(string $path): void + { + $handle = opendir($path); + + // We'll loop over all files and directories + // in the current path, + // and load them one by one + while ($file = readdir($handle)) { + if (in_array($file, ['.', '..'])) { + continue; + } + + $this->loadPath("{$path}/{$file}"); + } + + closedir($handle); + } + + private function loadFile(string $path): void + { + // We resolve the classname from composer's autoload mapping + $class = $this->fileMap[$path] ?? null; + + // And use it to make sure the class shouldn't be ignored + if ($this->shouldIgnore($class)) { + return; + } + + // Finally we require the path, + // causing all its dependencies to be loaded as well + require_once($path); + + self::$count++; + + echo "[Preloader] Preloaded `{$class}`" . PHP_EOL; + } + + private function shouldIgnore(?string $name): bool + { + if ($name === null) { + return true; + } + + foreach ($this->ignores as $ignore) { + if (strpos($name, $ignore) === 0) { + return true; + } + } + + return false; + } +} \ No newline at end of file diff --git a/src/Appwrite/Storage/Devices/S3.php b/src/Appwrite/Storage/Devices/S3.php index e161c3be48..6b5ca7cc31 100644 --- a/src/Appwrite/Storage/Devices/S3.php +++ b/src/Appwrite/Storage/Devices/S3.php @@ -9,7 +9,7 @@ class S3 extends Device /** * @return string */ - public function getName() + public function getName():string { return 'S3 Storage'; } @@ -17,7 +17,7 @@ class S3 extends Device /** * @return string */ - public function getDescription() + public function getDescription():string { return 'S3 Bucket Storage drive for AWS or on premise solution'; } @@ -25,7 +25,7 @@ class S3 extends Device /** * @return string */ - public function getRoot() + public function getRoot():string { return ''; } @@ -35,14 +35,162 @@ class S3 extends Device * * @return string */ - public function getPath($filename) + public function getPath($filename):string { - $path = ''; - - for ($i = 0; $i < 4; ++$i) { - $path = ($i < strlen($filename)) ? $path.DIRECTORY_SEPARATOR.$filename[$i] : $path.DIRECTORY_SEPARATOR.'x'; - } - - return $this->getRoot().$path.DIRECTORY_SEPARATOR.$filename; + return ''; } -} + + + /** + * Upload. + * + * Upload a file to desired destination in the selected disk. + * + * @param string $target + * @param string $filename + * + * @throws \Exception + * + * @return string|bool saved destination on success or false on failures + */ + public function upload($source, $path):bool + { + return false; + } + + /** + * Read file by given path. + * + * @param string $path + * + * @return string + */ + public function read(string $path):string + { + return ''; + } + + /** + * Write file by given path. + * + * @param string $path + * @param string $data + * + * @return bool + */ + public function write(string $path, string $data):bool + { + return false; + } + + /** + * Move file from given source to given path, Return true on success and false on failure. + * + * @see http://php.net/manual/en/function.filesize.php + * + * @param string $source + * @param string $target + * + * @return bool + */ + public function move(string $source, string $target):bool + { + return false; + } + + /** + * Delete file in given path, Return true on success and false on failure. + * + * @see http://php.net/manual/en/function.filesize.php + * + * @param string $path + * + * @return bool + */ + public function delete(string $path):bool + { + return false; + } + + /** + * Returns given file path its size. + * + * @see http://php.net/manual/en/function.filesize.php + * + * @param $path + * + * @return int + */ + public function getFileSize(string $path):int + { + return 0; + } + + /** + * Returns given file path its mime type. + * + * @see http://php.net/manual/en/function.mime-content-type.php + * + * @param $path + * + * @return string + */ + public function getFileMimeType(string $path):string + { + return ''; + } + + /** + * Returns given file path its MD5 hash value. + * + * @see http://php.net/manual/en/function.md5-file.php + * + * @param $path + * + * @return string + */ + public function getFileHash(string $path):string + { + return ''; + } + + /** + * Get directory size in bytes. + * + * Return -1 on error + * + * Based on http://www.jonasjohn.de/snippets/php/dir-size.htm + * + * @param $path + * + * @return int + */ + public function getDirectorySize(string $path):int + { + return 0; + } + + /** + * Get Partition Free Space. + * + * disk_free_space — Returns available space on filesystem or disk partition + * + * @return float + */ + public function getPartitionFreeSpace():float + { + return 0.0; + } + + /** + * Get Partition Total Space. + * + * disk_total_space — Returns the total size of a filesystem or disk partition + * + * @return float + */ + public function getPartitionTotalSpace():float + { + return 0.0; + } +} \ No newline at end of file