Skip to content

Commit

Permalink
cache test 3
Browse files Browse the repository at this point in the history
  • Loading branch information
magnum357i committed May 25, 2019
1 parent 67b9fe7 commit 7dff10f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 72 deletions.
Empty file removed cache/index.html
Empty file.
Empty file removed demo/upload/index.html
Empty file.
2 changes: 1 addition & 1 deletion src/MyAnimeList/Builder/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class AbstractBuilder {
protected $_data = [];

/**
* Saving directories
* Saving folders
*/
public static $folders = [ 'main' => NULL, 'file' => NULL, 'image' => NULL ];

Expand Down
2 changes: 1 addition & 1 deletion src/MyAnimeList/Builder/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class AbstractPage extends AbstractBuilder {
public static $id = NULL;

/**
* Saving directories
* Saving folders
*/
public static $folders = [

Expand Down
4 changes: 2 additions & 2 deletions src/MyAnimeList/Builder/AbstractSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ abstract class AbstractSearch extends AbstractBuilder {
public static $query = NULL;

/**
* Saving directories
* Saving folders
*/
public static $folders = [

'main' => 'search',
'file' => 'json',
'image' => 'cover'
'image' => NULL
];

/**
Expand Down
4 changes: 2 additions & 2 deletions src/MyAnimeList/Builder/AbstractWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
abstract class AbstractWidget extends AbstractBuilder {

/**
* Saving directories
* Saving folders
*/
public static $folders = [

'main' => 'widget',
'file' => 'json',
'image' => 'cover'
'image' => NULL
];

/**
Expand Down
138 changes: 72 additions & 66 deletions src/MyAnimeList/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,93 @@

class Cache implements CacheInterface {

/**
*/
const FOLDER_PERM = 0755;
const FILE_PERM = 0666;

/**
* Saving directories
*/
protected $folders = [
protected $paths = [

'file' => NULL,
'image' => NULL,
'main' => NULL
'image' => NULL
];

/**
* Cache folder
*/
protected $cacheFolder = 'myanimelist';

/**
* Type
*/
protected $type = NULL;

/**
* Cache path
*/
protected $path = NULL;
protected $uploadPath = NULL;

/**
* Set parameters
*
* @param $type MAL Type
* @param $type MAL Type
* @param $folders Saving directories
* @return void
*/
public function __construct( $type, $folders=[] ) {

foreach( $this->folders as $key => $value ) $this->folders[ $key ] = $folders[ $key ];
$this->paths[ 'file' ] = ( $folders[ 'file' ] != NULL ) ? $this->fixSeparator( implode( '/', [ $this->cacheFolder, $folders[ 'main' ], $folders[ 'file' ], $type ] ) ) : NULL;
$this->paths[ 'image' ] = ( $folders[ 'image' ] != NULL ) ? $this->fixSeparator( implode( '/', [ $this->cacheFolder, $folders[ 'main' ], $folders[ 'image' ], $type ] ) ) : NULL;

$this->type = $type;

$this->setPath( __DIR__ . '../../../../cache' );
$this->setPath( __DIR__ . '/../../../cache' );
}

/**
* Set path to save
*
* @param $root Root path
* @param $path Path to save
* @return void
*/
public function setPath( $path ) {

$this->path = $path;
$path = $this->fixSeparator( $path );
$seperator = DIRECTORY_SEPARATOR;
$count = 1;

while ( $count > 0 ) $path = preg_replace( "@[^\\{$seperator}]+{$seperator}\.\.\\{$seperator}@", '', $path, 1, $count );

$this->uploadPath = $path;
}

/**
* Create path if not
*
* @return void
*/
public function createPathIfNot() {

foreach ( [ 'file', 'image' ] as $type ) {

if ( $this->paths[ $type ] != NULL ) {

$path = $this->uploadPath . DIRECTORY_SEPARATOR . $this->paths[ $type ];

if( !is_dir( $path ) ) {

if( !@mkdir( $path, static::FOLDER_PERM, TRUE ) OR !@chmod( $path, static::FOLDER_PERM ) ) {

throw new \Exception( "[MyAnimeList Cache Error] Directory could not create" );
}

@file_put_contents( $path . '/index.html', '' );
}
}
}
}

/**
* File size in bytes
*
* @param $fileName File name
* @param $type 'poster' or 'file'?
* @param $type 'poster'|'file'
* @return bool
*/
public function fileSize( $fileName, $type ) {
Expand All @@ -81,13 +110,11 @@ public function fileSize( $fileName, $type ) {

if ( $type == 'poster' ) {

$path = $this->fixSeparator( implode( '/', [ $this->path, $this->cacheFolder, $this->folders[ 'main' ], $this->folders[ 'image' ], $this->type ] ) );
$file = $this->fixSeparator( $path . '/' . $fileName . '.jpg' );
$file = implode( DIRECTORY_SEPARATOR, [ $this->uploadPath, $this->paths[ 'image' ], "{$fileName}.json" ] );
}
else if ( $type == 'file' ) {

$path = $this->fixSeparator( implode( '/', [ $this->path, $this->cacheFolder, $this->folders[ 'main' ], $this->folders[ 'file' ], $this->type ] ) );
$file = $this->fixSeparator( $path . '/' . $fileName . '.json' );
$file = implode( DIRECTORY_SEPARATOR, [ $this->uploadPath, $this->paths[ 'file' ], "{$fileName}.json" ] );
}

return filesize( $file );
Expand All @@ -101,8 +128,7 @@ public function fileSize( $fileName, $type ) {
*/
public function hasFile( $fileName ) {

$path = $this->fixSeparator( implode( '/', [ $this->path, $this->cacheFolder, $this->folders[ 'main' ], $this->folders[ 'file' ], $this->type ] ) );
$file = $this->fixSeparator( $path . '/' . $fileName . '.json' );
$file = implode( DIRECTORY_SEPARATOR, [ $this->uploadPath, $this->paths[ 'file' ], "{$fileName}.json" ] );

return ( file_exists( $file ) ) ? TRUE : FALSE;
}
Expand All @@ -115,19 +141,12 @@ public function hasFile( $fileName ) {
*/
public function readFile( $fileName ) {

$path = $this->fixSeparator( implode( '/', [ $this->path, $this->cacheFolder, $this->folders[ 'main' ], $this->folders[ 'file' ], $this->type ] ) );
$file = $this->fixSeparator( $path . '/' . $fileName . '.json' );
$file = implode( DIRECTORY_SEPARATOR, [ $this->uploadPath, $this->paths[ 'file' ], "{$fileName}.json" ] );
$content = @file_get_contents( $file );

try {

$content = json_decode( file_get_contents( $file ), TRUE );
}
catch ( \Exception $e ) {
if ( !$content ) throw new \Exception( "[MyAnimeList Cache Error] File could not read" );

throw new \Exception( "[MyAnimeList Cache Error] {$e}" );
}

return $content;
return json_decode( $content, TRUE );
}

/**
Expand All @@ -141,31 +160,13 @@ public function writeFile( $fileName, $content ) {

if ( empty( $content ) ) return NULL;

$path = $this->fixSeparator( implode( '/', [ $this->path, $this->cacheFolder, $this->folders[ 'main' ], $this->folders[ 'file' ], $this->type ] ) );

if ( !is_dir( $path ) ) mkdir( $path, 0700, TRUE );
$this->createPathIfNot();

$file = $this->fixSeparator( $path . '/' . $fileName . '.json' );
$file = implode( DIRECTORY_SEPARATOR, [ $this->uploadPath, $this->paths[ 'file' ], "{$fileName}.json" ] );

try {
if ( !@file_put_contents( $file, json_encode( $content ) ) ) throw new \Exception( "[MyAnimeList Cache Error] File could not save" );

file_put_contents( $file, json_encode( $content ) );
}
catch ( \Exception $e ) {

throw new \Exception( "[MyAnimeList Cache Error] {$e}" );
}
}

/**
* Converts slashes by OS
*
* @param $path Path
* @return string
*/
protected function fixSeparator( $path ) {

return str_replace( [ '/', '\\' ], DIRECTORY_SEPARATOR, $path );
@chmod( $file, static::FILE_PERM );
}

/**
Expand All @@ -178,23 +179,28 @@ protected function fixSeparator( $path ) {
*/
public function savePoster( $fileName, $url, $overWrite=FALSE ) {

$path = $this->fixSeparator( implode( '/', [ $this->path, $this->cacheFolder, $this->folders[ 'main' ], $this->folders[ 'image' ], $this->type ] ) );
$poster = $this->fixSeparator( $path . '/' . $fileName . '.jpg' );
$this->createPathIfNot();

if ( !is_dir( $path ) ) mkdir( $path, 0700, TRUE );
$poster = implode( DIRECTORY_SEPARATOR, [ $this->uploadPath, $this->paths[ 'image' ], "{$fileName}.json" ] );

if ( $overWrite == TRUE OR !file_exists( $poster ) ) {

try {
if( !@copy( $url, $poster ) ) throw new \Exception( "[MyAnimeList Cache Error] Poster could not save" );

copy( $url, $poster );
}
catch ( \Exception $e ) {

throw new \Exception( "[MyAnimeList Cache Error] {$e}" );
}
@chmod( $poster, static::FILE_PERM );
}

return $this->fixSeparator( str_replace( $this->fixSeparator( filter_input( INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_STRING ) ), '', $poster ) );
}

/**
* Converts slashes by OS
*
* @param $path Path
* @return string
*/
protected function fixSeparator( $path ) {

return str_replace( [ '/', '\\' ], DIRECTORY_SEPARATOR, $path );
}
}

0 comments on commit 7dff10f

Please sign in to comment.