55 */
66class Scraper_screenshot extends Scraper_Base {
77
8- private $ ___sTodayDirPath ;
8+ protected $ _sType = ' screenshot ' ;
99
1010 protected function _construct () {
1111
12- $ this ->___setImageCacheDirectory ();
13- 1412 // Sanitize parameters
1513 $ this ->_aClientArguments [ 'load-images ' ] = isset ( $ _REQUEST [ 'load-images ' ] ) && ! Utility::getBoolean ( $ _REQUEST [ 'load-images ' ] )
1614 ? false
@@ -20,6 +18,23 @@ protected function _construct() {
2018
2119 public function do () {
2220
21+ // Check cache
22+ $ _aRequestArguments = $ this ->_aRequestArguments ;
23+ $ _sFileType = in_array ( $ _aRequestArguments [ 'file-type ' ], array ( 'jpg ' , 'pdf ' , 'png ' , 'jpeg ' , 'bmp ' , 'ppm ' ) )
24+ ? $ _aRequestArguments [ 'file-type ' ]
25+ : 'jpg ' ;
26+ $ _sFileBaseName = $ this ->_getRequestCacheName () . ". {$ _sFileType }" ;
27+ $ _sFilePath = $ this ->_sCacheDirPathToday . '/ ' . $ _sFileBaseName ;
28+ 29+ // Use cache
30+ if ( file_exists ( $ _sFilePath ) ) {
31+ $ _iModifiedTime = ( integer ) filemtime ( $ _sFilePath );
32+ if ( $ _iModifiedTime + ( ( integer ) $ this ->_aBaseArguments [ 'cache_lifespan ' ] ) > time () ) {
33+ $ this ->___render ( $ _sFilePath );
34+ return ;
35+ }
36+ }
37+ 2338 $ _oScreenCapture = new ScreenCapture (
2439 $ this ->_aBaseArguments [ 'binary_path ' ],
2540 $ this ->_aBaseArguments [ 'user_agent ' ],
@@ -28,12 +43,6 @@ public function do() {
2843 );
2944
3045 /// Request Arguments
31- $ _aRequestArguments = $ this ->_aRequestArguments ;
32- $ _sFileType = in_array ( $ _aRequestArguments [ 'file-type ' ], array ( 'jpg ' , 'pdf ' , 'png ' , 'jpeg ' , 'bmp ' , 'ppm ' ) )
33- ? $ _aRequestArguments [ 'file-type ' ]
34- : 'jpg ' ;
35- $ _sFileBaseName = md5 ( $ this ->_aBaseArguments [ 'url ' ] ) . ". {$ _sFileType }" ;
36- $ _sFilePath = $ this ->___sTodayDirPath . '/ ' . $ _sFileBaseName ; // $_sFilePath = Registry::$sDirPath . '/_capture/file.jpg';
3746 $ _aRequestArguments [ 'file_path ' ] = $ _sFilePath ;
3847 $ _aRequestArguments [ 'file_type ' ] = $ _sFileType ;
3948 $ _oScreenCapture ->setRequestArguments ( $ _aRequestArguments );
@@ -45,22 +54,6 @@ public function do() {
4554 $ this ->___render ( $ _sFilePath );
4655
4756 }
48- private function ___setImageCacheDirectory () {
49- 50- $ _sToday = date ("Ymd " );
51- $ _sTodayDirPath = Registry::$ sTempDirPath . '/capture/ ' . $ _sToday ;
52- if ( ! file_exists ( $ _sTodayDirPath ) ) {
53- mkdir ( $ _sTodayDirPath , 0777 , true );
54- }
55- $ this ->___sTodayDirPath = $ _sTodayDirPath ;
56- 57- // Delete old cache directories
58- $ _aSubDirs = $ this ->getSubDirPaths ( Registry::$ sTempDirPath . '/capture/ ' , array ( $ _sToday ) );
59- foreach ( $ _aSubDirs as $ _sDirPath ) {
60- $ this ->deleteDir ( $ _sDirPath );
61- }
62- 63- }
6457
6558 private function ___render ( $ sFilePath ) {
6659 $ _aImageInfo = getimagesize ( $ sFilePath );
0 commit comments