Skip to content

Commit

Permalink
Merge pull request #9 from Leprechaunz/psr-2
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Oct 9, 2018
2 parents f8104b8 + 88e273e commit c56bd8f
Show file tree
Hide file tree
Showing 12 changed files with 467 additions and 288 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
}
],
"require": {
"php": ">=5.3.1",
"php": ">=5.6.0",
"monolog/monolog": "~1.6",
"slim/slim": "~2.3",
"slim/views": "0.*",
"twig/twig": "~1.13",
"kunalvarma05/dropbox-php-sdk": "^0.2.1"
"kunalvarma05/dropbox-php-sdk": "^0.2.1",
"ext-gd": "*"
},
"autoload":{
"psr-0":{
"": ""
"psr-4":{
"Mre\\Unicorn\\": ""
}
}
}
4 changes: 1 addition & 3 deletions config_sample.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use lib\Config;
use Mre\Unicorn\lib\Config;

// You can set a name for your gallery which
// will be shown at the homepage.
Expand Down Expand Up @@ -37,5 +37,3 @@
// Constant with absolute path to the public-facing root of the server.
// Don't change.
Config::write("root_path", $_SERVER['DOCUMENT_ROOT']);

?>
23 changes: 10 additions & 13 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,39 @@
ini_set('html_errors', 'On');

// Prepare app
$app = new \Slim\Slim(array(
$app = new \Slim\Slim([
'templates.path' => 'templates',
));
]);

// Create monolog logger and store logger in container as singleton
// (Singleton resources retrieve the same log resource definition each time)
$app->container->singleton('log', function () {
$log = new \Monolog\Logger('slim-skeleton');
$log->pushHandler(new \Monolog\Handler\StreamHandler(
'logs/app.log', \Psr\Log\LogLevel::DEBUG));
$log->pushHandler(new \Monolog\Handler\StreamHandler('logs/app.log', \Psr\Log\LogLevel::DEBUG));
return $log;
});

// Prepare view
$app->view(new \Slim\Views\Twig());
$app->view->parserOptions = array(
$app->view->parserOptions = [
'charset' => 'utf-8',
'cache' => realpath('../templates/cache'),
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true
);
$app->view->parserExtensions = array(new \Slim\Views\TwigExtension());
'autoescape' => true,
];
$app->view->parserExtensions = [new \Slim\Views\TwigExtension()];


// Set application-wide route conditions for URL parameters
\Slim\Route::setDefaultConditions(array(
\Slim\Route::setDefaultConditions([
// Album name is alphanumeric with underscores instead
// of spaces.
'album' => '[_a-zA-Z0-9]{1,}',
// Image filenames consist of a name and an extension
// separated by a dot.
'image' => '[a-zA-Z0-9]+\.[a-zA-Z_]{3,4}'
));
'image' => '[a-zA-Z0-9]+\.[a-zA-Z_]{3,4}',
]);


// Automatically load router files
Expand All @@ -53,5 +52,3 @@

// Run app
$app->run();

?>
87 changes: 53 additions & 34 deletions lib/Cache.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
<?php

namespace lib;

use lib\Dropbox;
use lib\File;
use lib\Config;
namespace Mre\Unicorn\lib;

class Cache
{
private $cursor_file;
/**
* @var Dropbox
*/
private $dropbox;

/**
* @var string
*/
private $cursorFile;

/**
* @var
*/
private $api;

/**
* @var bool|null|string
*/
private $cursor;

public function __construct()
{
$this->dropbox = Dropbox::get_instance();
$this->cursor_file = Config::read("storage_object_dir") . "/user.cursor";
$this->cursor = $this->read_cursor();
$this->dropbox = Dropbox::getInstance();
$this->cursorFile = Config::read("storage_object_dir") . "/user.cursor";
$this->cursor = $this->readCursor();
}

/**
* Returns the current "state" of the cache
* in form of a Dropbox cursor for the delta API
* (see Dropbox API documentation for more info).
*/
private function read_cursor()
private function readCursor()
{
if (($content = File::read($this->cursor_file)) == '') {
if (($content = File::read($this->cursorFile)) == '') {
// An empty file means we have no cursor yet (no cache available).
// In this case, the API expects a null object.
return null;
Expand All @@ -34,28 +47,31 @@ private function read_cursor()
return $content;
}

private function write_cursor($cursor)
private function writeCursor($cursor)
{
File::write($this->cursor_file, $cursor);
File::write($this->cursorFile, $cursor);
}

public function is_up_to_date()
/**
* @return bool
*/
public function isUpToDate()
{
if (! file_exists($this->cursor_file)) {
if (!file_exists($this->cursorFile)) {
return false;
}

$delta = date("U") - filemtime($this->cursor_file);
$delta = date("U") - filemtime($this->cursorFile);

return $delta <= Config::read("cache_update_after");
}

private function remove_cursor()
private function removeCursor()
{
File::remove($this->cursor_file);
File::remove($this->cursorFile);
}

public function clear_cache_dir()
public function clearCacheDir()
{
$cache_dir = Config::read("cache_dir");
// Clear cache
Expand All @@ -68,39 +84,42 @@ public function clear_cache_dir()

public function clear()
{
$this->remove_cursor();
$this->clear_cache_dir();
$this->removeCursor();
$this->clearCacheDir();
}

/**
* Use the Dropbox delta API to cache the gallery.
* Every image will be stored inside the cache directory
* This increases loading speed significantly.
*
* @param bool $forceUpdate
* @param bool $purge
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
*/
public function refresh($force_update = false, $purge = false)
public function refresh($forceUpdate = false, $purge = false)
{

// Do we need to check for updates?
/*if ($this->is_up_to_date() && !$force_update) {
/*if ($this->isUpToDate() && !$forceUpdate) {
return;
}*/

if ($purge) {
$this->clear();
$this->cursor = $this->read_cursor();
$this->cursor = $this->readCursor();
}

// Get changes
if ($this->cursor) {
$changes = $this->dropbox->api->listFolderContinue($this->cursor);
} else {
$changes = $this->dropbox->api->listFolder('/', array("recursive" => true));
$changes = $this->dropbox->api->listFolder('/', ["recursive" => true]);
}

do {
$entries = $changes->getItems();
foreach ($entries as $entry) {
$this->write_entry($entry);
$this->writeEntry($entry);
}
// Refresh cursor
$this->cursor = $changes->getCursor();
Expand All @@ -109,30 +128,30 @@ public function refresh($force_update = false, $purge = false)
} while ($changes->hasMoreItems());

// Save current status
$this->write_cursor($this->cursor);
$this->writeCursor($this->cursor);
}

/**
* @param $entry
*
* @throws \Kunnu\Dropbox\Exceptions\DropboxClientException
*/
private function write_entry($entry)
private function writeEntry($entry)
{
if ($entry instanceof \Kunnu\Dropbox\Models\FolderMetadata) {
// Don't write albums, only images.
return;
}
$dirname = File::sanitize(dirname($entry->path_display));
$basename = basename($entry->path_display);
$local_path = Config::read("cache_dir") . "/" . $dirname . "/" . $basename;
$localPath = Config::read("cache_dir") . "/" . $dirname . "/" . $basename;

// Check if dir already exists. Create if not.
Directory::rmkdir($local_path);
Directory::rmkdir($localPath);

$outfile = Dropbox::get_instance()->api->download($entry->path_display);
File::write($local_path, $outfile->getContents());
Image::create_thumbnail($local_path);
$outfile = Dropbox::getInstance()->api->download($entry->path_display);
File::write($localPath, $outfile->getContents());
Image::createThumbnail($localPath);
}
}

28 changes: 22 additions & 6 deletions lib/Config.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
<?php

namespace lib;
namespace Mre\Unicorn\lib;

// Configuration Class
class Config {
static $confArray;
/**
* Configuration Class
*/
class Config
{
/**
* @var array
*/
private static $confArray;

public static function read($name) {
/**
* @param string $name
* @return mixed
*/
public static function read($name)
{
return self::$confArray[$name];
}

public static function write($name, $value) {
/**
* @param $name
* @param $value
*/
public static function write($name, $value)
{
self::$confArray[$name] = $value;
}
}
Loading

0 comments on commit c56bd8f

Please sign in to comment.