/** * startCaching method * * Cache\[optimize] images in cache folder * * @param string $optimization 'true' or 'false' * @param string $srcPath folder name for source images * @return void */ public function startCaching($optimization = 'true', $srcPath = 'src_images') { $dir = new Folder(WWW_ROOT . 'img' . DS . $srcPath); $files = $dir->findRecursive('.*\\.(jpg|jpeg|png|gif|svg)'); /* * Error handler */ if (is_null($dir->path)) { $this->error('<red_text>Source folder not exists!</red_text>'); } if ($optimization != 'true' && $optimization != 'false') { $this->error('<red_text>Arguments \'optimization\' should equal \'true\' or \'false\'</red_text>'); } /* * Caching */ $counter = 1; $countFiles = count($files); $this->out('<info>Images caching</info>'); foreach ($files as $file) { $file = new File($file); $semanticType = explode(DS, $file->Folder()->path); $semanticType = $semanticType[count($semanticType) - 1]; //get semantic type name $this->adaptiveImagesController->passiveCaching($file->path, $semanticType); $this->_io->overwrite($this->progressBar($counter, $countFiles), 0, 50); $counter++; } /* * Optimization */ if ($optimization == 'true') { $cachePath = $this->adaptiveImagesController->getCachePath(); $pluginPath = Plugin::path('AdaptiveImages'); $cacheDir = new Folder($pluginPath . 'webroot' . DS . $cachePath); $files = $cacheDir->findRecursive('.*\\.(jpg|jpeg|png|gif|svg)'); $counter = 1; $countFiles = count($files); $this->out(''); $this->out('<info>Images optimization</info>'); foreach ($files as $file) { $this->_optimizeImage($file); $this->_io->overwrite($this->progressBar($counter, $countFiles), 0, 50); $counter++; } $this->hr(); $this->out('<green_text>Caching and optimization completed!</green_text>'); } elseif ($optimization == 'false') { $this->hr(); $this->out('<green_text>Caching completed!</green_text>'); } }
/** * Delete file on server represented by entity being deleted */ public function beforeDelete(Event $event, Entity $entity, \ArrayObject $options) { Configure::load('UploadManager.uploads', 'default'); $storagePath = Configure::read('Uploads.storagePath'); $file = new File($entity->path); $folder = $file->Folder(); // Check for empty directories on successful delete if ($file->delete()) { // Delete type folder if empty if (!$folder->find()) { $oldFolder = $folder; $folder->cd($folder->realpath($folder->pwd() . DS . '..')); $oldFolder->delete(); // Check for other possible empty parent (owner storage) if ($folder->pwd() !== $storagePath) { if (!$folder->find()) { $folder->delete(); } } } } else { $event->stopPropagation(); } }
/** * Delete an existing file on the server, if this causes * the directory to be empty, delete it; provided it is * not the root of uploads. * * @param path - location of file * @return result of delete (success/fail) */ public function delete($path) { $file = new File($path); $folder = $file->Folder(); // Check for empty directories on successful delete if ($file->delete()) { // Delete type folder if empty if (!$folder->find()) { $oldFolder = $folder; $folder->cd($folder->realpath($folder->pwd() . DS . '..')); $oldFolder->delete(); // Check for other possible empty parent (owner storage) if ($folder->pwd() !== $storagePath) { if (!$folder->find()) { $folder->delete(); } } } } else { return false; } }