/** * Clean var/generation, var/di and var/cache * * @return void */ public function cleanGeneratedFiles() { if ($this->write->isExist(self::REGENERATE_FLAG)) { $enabledCacheTypes = []; //TODO: to be removed in scope of MAGETWO-53476 $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG); $configPool = new ConfigFilePool(); $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV); if ($this->write->isExist($this->write->getRelativePath($envPath))) { $enabledCacheTypes = $this->getEnabledCacheTypes(); $this->disableAllCacheTypes(); } //TODO: Till here $cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE)); $generationPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATION)); $diPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::DI)); // Clean var/generation dir if ($this->write->isDirectory($generationPath)) { $this->write->delete($generationPath); } // Clean var/di if ($this->write->isDirectory($diPath)) { $this->write->delete($diPath); } // Clean var/cache if ($this->write->isDirectory($cachePath)) { $this->write->delete($cachePath); } $this->write->delete(self::REGENERATE_FLAG); $this->enableCacheTypes($enabledCacheTypes); } }
/** * Delete Expired Captcha Images for specific website * * @param \Magento\Captcha\Helper\Data $helper * @param \Magento\Store\Model\Website|null $website * @param \Magento\Store\Model\Store|null $store * @return void */ protected function _deleteExpiredImagesForWebsite(\Magento\Captcha\Helper\Data $helper, \Magento\Store\Model\Website $website = null, \Magento\Store\Model\Store $store = null) { $expire = time() - $helper->getConfig('timeout', $store) * 60; $imageDirectory = $this->_mediaDirectory->getRelativePath($helper->getImgDir($website)); foreach ($this->_mediaDirectory->read($imageDirectory) as $filePath) { if ($this->_mediaDirectory->isFile($filePath) && pathinfo($filePath, PATHINFO_EXTENSION) == 'png' && $this->_mediaDirectory->stat($filePath)['mtime'] < $expire) { $this->_mediaDirectory->delete($filePath); } } }
/** * {@inheritDoc} */ protected function tearDown() { /** @var \Magento\TestFramework\App\State $appState */ $appState = $this->objectManager->get(\Magento\TestFramework\App\State::class); $appState->setMode($this->origMode); if ($this->staticDir->isExist('frontend/FrameworkViewMinifier')) { $this->staticDir->delete('frontend/FrameworkViewMinifier'); } parent::tearDown(); }
/** * Flash ISM_BaseRunner cache * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { $dir = $this->_mediaDirectory->getAbsolutePath(Upload::UPLOAD_POST_IMAGE_DIR . 'cache/'); if (is_dir($dir)) { $dir = $this->_mediaDirectory->getDriver()->readDirectory($dir); foreach ($dir as $file) { $this->_mediaDirectory->delete(Upload::UPLOAD_POST_IMAGE_DIR . 'cache/' . basename($file)); } } }
/** * Check whether generation process has already locked * * @return bool * @throws FileSystemException */ private function isProcessLocked() { if ($this->tmpDirectory->isExist($this->lockFilePath)) { $lockTime = (int) $this->tmpDirectory->readFile($this->lockFilePath); if (time() - $lockTime >= self::MAX_LOCK_TIME) { $this->tmpDirectory->delete($this->lockFilePath); return false; } return true; } return false; }
/** * Check and remove outdated certificate file by website * * @return void */ protected function _removeOutdatedCertFile() { $pattern = sprintf('cert_%s*', $this->getWebsiteId()); $entries = $this->varDirectory->search($pattern, self::BASEPATH_PAYPAL_CERT); foreach ($entries as $entry) { $this->varDirectory->delete($entry); } }
/** * Upload and create preview image * * @param string $scope the request key for file * @return $this */ public function uploadPreviewImage($scope) { $tmpDirPath = $this->themeImagePath->getTemporaryDirectory(); $tmpFilePath = $this->uploader->uploadPreviewImage($scope, $tmpDirPath); if ($tmpFilePath) { if ($this->theme->getPreviewImage()) { $this->removePreviewImage(); } $this->createPreviewImage($tmpFilePath); $this->mediaDirectory->delete($tmpFilePath); } return $this; }
/** * Check whether generation process has already locked * * @return bool */ protected function isProcessLocked() { $lockFilePath = $this->config->getLessMaterializationRelativePath() . '/' . self::LOCK_FILE; if ($this->tmpDirectory->isExist($lockFilePath)) { $lockTime = time() - (int) $this->tmpDirectory->readFile($lockFilePath); if ($lockTime >= self::MAX_LOCK_TIME) { $this->tmpDirectory->delete($lockFilePath); return false; } return true; } return false; }
/** * Sets list of allowed IP addresses * * @param string $addresses * @return bool * @throws \InvalidArgumentException */ public function setAddresses($addresses) { $addresses = (string) $addresses; if (empty($addresses)) { if ($this->flagDir->isExist(self::IP_FILENAME)) { return $this->flagDir->delete(self::IP_FILENAME); } return true; } if (!preg_match('/^[^\\s,]+(,[^\\s,]+)*$/', $addresses)) { throw new \InvalidArgumentException("One or more IP-addresses is expected (comma-separated)\n"); } $result = $this->flagDir->writeFile(self::IP_FILENAME, $addresses); return false !== $result ? true : false; }
/** * Create flag in case when value is set to 'true', remove it if value is set to 'false'. * * @param string $pathToFlagFile * @param bool $value * @return $this */ protected function setFlagValue($pathToFlagFile, $value) { if ($value) { try { $this->varReaderWriter->touch($pathToFlagFile); } catch (FileSystemException $e) { throw new \RuntimeException(sprintf('"%s" cannot be created.', $pathToFlagFile)); } } else { if ($this->varReaderWriter->isExist($pathToFlagFile)) { $this->varReaderWriter->delete($pathToFlagFile); } } return $this; }
/** * move image from tmp to catalog dir * * @param string $file * @return string path */ public function moveImageFromTmp($file) { if (strrpos($file, '.tmp') == strlen($file) - 4) { $file = substr($file, 0, strlen($file) - 4); } $destinationFile = $this->getUniqueFileName($file); /** @var $storageHelper \Magento\MediaStorage\Helper\File\Storage\Database */ $storageHelper = $this->fileStorageDb; if ($storageHelper->checkDbUsage()) { $storageHelper->renameFile($this->mediaConfig->getTmpMediaShortUrl($file), $this->mediaConfig->getMediaShortUrl($destinationFile)); $this->mediaDirectory->delete($this->mediaConfig->getTmpMediaPath($file)); $this->mediaDirectory->delete($this->getAttributeSwatchPath($destinationFile)); } else { $this->mediaDirectory->renameFile($this->mediaConfig->getTmpMediaPath($file), $this->getAttributeSwatchPath($destinationFile)); } return str_replace('\\', '/', $destinationFile); }
/** * Copy image and return new filename. * * @param string $file * @return string * @throws \Magento\Framework\Exception\LocalizedException */ protected function _copyImage($file) { try { $destinationFile = $this->_getUniqueFileName($file); if (!$this->_mediaDirectory->isFile($this->_mediaConfig->getMediaPath($file))) { throw new \Exception(); } if ($this->_fileStorageDb->checkDbUsage()) { $this->_fileStorageDb->copyFile($this->_mediaDirectory->getAbsolutePath($this->_mediaConfig->getMediaShortUrl($file)), $this->_mediaConfig->getMediaShortUrl($destinationFile)); $this->_mediaDirectory->delete($this->_mediaConfig->getMediaPath($destinationFile)); } else { $this->_mediaDirectory->copyFile($this->_mediaConfig->getMediaPath($file), $this->_mediaConfig->getMediaPath($destinationFile)); } return str_replace('\\', '/', $destinationFile); } catch (\Exception $e) { $file = $this->_mediaConfig->getMediaPath($file); throw new LocalizedException(__('We couldn\'t copy file %1. Please delete media with non-existing images and try again.', $file)); } }
/** * Open backup file (write or read mode) * * @param bool $write * @return $this * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Backup\Exception\NotEnoughPermissions */ public function open($write = false) { if ($this->getPath() === null) { throw new \Magento\Framework\Exception\InputException(__('The backup file path was not specified.')); } if ($write && $this->varDirectory->isFile($this->_getFilePath())) { $this->varDirectory->delete($this->_getFilePath()); } if (!$write && !$this->varDirectory->isFile($this->_getFilePath())) { throw new \Magento\Framework\Exception\InputException(__('The backup file "%1" does not exist.', $this->getFileName())); } $mode = $write ? 'wb' . self::COMPRESS_RATE : 'rb'; try { /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $varDirectory */ $varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR, DriverPool::ZLIB); $this->_stream = $varDirectory->openFile($this->_getFilePath(), $mode); } catch (\Magento\Framework\Exception\FileSystemException $e) { throw new \Magento\Framework\Backup\Exception\NotEnoughPermissions(__('Sorry, but we cannot read from or write to backup file "%1".', $this->getFileName())); } return $this; }
/** * Goes to specified host/path and fetches reports from there. * Save reports to database. * * @param \Magento\Framework\Filesystem\Io\Sftp $connection * @return int Number of report rows that were fetched and saved successfully * @throws \Magento\Framework\Exception\LocalizedException */ public function fetchAndSave(\Magento\Framework\Filesystem\Io\Sftp $connection) { $fetched = 0; $listing = $this->_filterReportsList($connection->rawls()); foreach ($listing as $filename => $attributes) { $localCsv = 'PayPal_STL_' . uniqid(\Magento\Framework\Math\Random::getRandomNumber()) . time() . '.csv'; if ($connection->read($filename, $this->_tmpDirectory->getAbsolutePath($localCsv))) { if (!$this->_tmpDirectory->isWritable($localCsv)) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot create a target file for reading reports.')); } $encoded = $this->_tmpDirectory->readFile($localCsv); $csvFormat = 'new'; $fileEncoding = mb_detect_encoding($encoded); if (self::FILES_OUT_CHARSET != $fileEncoding) { $decoded = @iconv($fileEncoding, self::FILES_OUT_CHARSET . '//IGNORE', $encoded); $this->_tmpDirectory->writeFile($localCsv, $decoded); $csvFormat = 'old'; } // Set last modified date, this value will be overwritten during parsing if (isset($attributes['mtime'])) { $date = new \DateTime(); $lastModified = $date->setTimestamp($attributes['mtime']); $this->setReportLastModified($lastModified->format('Y-m-d H:i:s')); } $this->setReportDate($this->_fileNameToDate($filename))->setFilename($filename)->parseCsv($localCsv, $csvFormat); if ($this->getAccountId()) { $this->save(); } if ($this->_dataSaveAllowed) { $fetched += count($this->_rows); } // clean object and remove parsed file $this->unsetData(); $this->_tmpDirectory->delete($localCsv); } } return $fetched; }
/** * Remove image. * * @param $file */ public function removeImage($file) { $this->_mediaDirectory->delete(self::UPLOAD_POST_IMAGE_DIR . $file); }
/** * @return void */ public function clearCache() { $directory = $this->_catalogProductMediaConfig->getBaseMediaPath() . '/cache'; $this->_mediaDirectory->delete($directory); $this->_coreFileStorageDatabase->deleteFolder($this->_mediaDirectory->getAbsolutePath($directory)); }
/** * Clears contents of the log * * @return void */ public function clear() { if ($this->directory->isExist($this->logFile)) { $this->directory->delete($this->logFile); } }
/** * @param string $dbCode */ protected function deletePackDb($dbCode) { $this->directory->delete($this->getDbArchiveFilePath($dbCode)); }