public function clear($maxAge, $prefix = null) { $prefix = $prefix ?: $this->prefix; $matches = $this->filesystem->listFiles($prefix); $now = time(); $toDelete = array(); // Collect the directories that are old, // this also means the files inside are old // but after the files are deleted the dirs // would remain foreach ($matches['dirs'] as $key) { if ($maxAge <= $now - $this->filesystem->getTimestamp($key)) { $toDelete[] = $key; } } // The same directory is returned for every file it contains array_unique($toDelete); foreach ($matches['keys'] as $key) { if ($maxAge <= $now - $this->filesystem->getTimestamp($key)) { $this->filesystem->delete($key); } } foreach ($toDelete as $key) { // The filesystem will throw exceptions if // a directory is not empty try { $this->filesystem->delete($key); } catch (\Exception $e) { continue; } } }
/** * @param string $providerPrefix * @param string $uri * @param string $query * * @return bool|mixed */ public function get($providerPrefix, $uri, $query) { $hash = $this->getHash($providerPrefix, $uri, $query); $ttlCutoff = time() - $this->ttl; if ($this->filesystem->has($hash) and $this->filesystem->getTimestamp($hash) > $ttlCutoff) { $data = $this->filesystem->read($hash); if (!empty($data)) { return $data; } } return false; }
/** * Get cached string from store. * * @param string $key * * @return null|string */ public function get($key) { if (!is_string($key)) { return; } try { $file = $this->get_file_name($key); // Expire the file if the expires time is not zero. if ($this->args['expires'] > 0) { $time = $this->filesystem->getTimestamp($file); // If time is bigger than expires and file timestamp // the file should be deleted and null should be returned // since the cache has expired. if (time() > $this->args['expires'] * $time) { $this->filesystem->delete($file); return; } } // Try to read the file. $content = $this->filesystem->read($file); // Delete the file if empty. if (empty($content)) { $this->filesystem->delete($file); } return $content; } catch (FileNotFoundException $e) { return; } }
/** * Return admin assets * @param Response $response * @param $asset * @return Response|static * @throws FileNotFoundException */ public function assets(Response $response, $asset) { $filesystem = new Filesystem(new Adapter(FS2ADMIN)); $expires = 8640000; try { // generate cache data $timestamp_string = gmdate('D, d M Y H:i:s ', $filesystem->getTimestamp($asset)) . 'GMT'; $etag = md5($filesystem->read($asset)); $mime_type = $filesystem->getMimetype($asset); if (0 !== strpos($mime_type, 'image')) { $mime_type = 'text/css'; } $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false; $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false; if (($if_none_match && $if_none_match == "\"{$etag}\"" || !$if_none_match) && ($if_modified_since && $if_modified_since == $timestamp_string)) { return $response->withStatus('304'); } else { $response = $response->withHeader('Last-Modified', $timestamp_string)->withHeader('ETag', "\"{$etag}\""); } // send out content type, expire time and the file $response->getBody()->write($filesystem->read($asset)); return $response->withHeader('Expires', gmdate('D, d M Y H:i:s ', time() + $expires) . 'GMT')->withHeader('Content-Type', $mime_type)->withHeader('Pragma', 'cache')->withHeader('Cache-Control', 'cache'); } catch (FileNotFoundException $e) { throw $e; } }
/** * get the languages configuration file path.<br> * the languages are store in db. use this class/method to generate them into a file and get the file path that were generated. * * @return string return languages configuration file path. */ public function getConfigFile() { $adapter = new Local($this->language_config_folder); $filesystem = new Filesystem($adapter); unset($adapter); if ($filesystem->has($this->language_db_file)) { $lang_file_ts = $filesystem->getTimestamp($this->language_db_file); $day_old = (time() - $lang_file_ts) / 60 / 60 / 24; if ($day_old > 30) { // older than 30 days, rebuild languages. $build_result = $this->buildConfigFile(); } unset($day_old, $filesystem, $lang_file_ts); if (isset($build_result) && $build_result !== true) { \System\Libraries\Log::write('LanguagesDb', 'notice', 'Could not generate the languages from db from getConfigFile method.'); } return $this->language_config_folder . DS . $this->language_db_file; } elseif ($this->buildConfigFile() === true) { // never build the language configuration file before, build it. unset($filesystem); return $this->language_config_folder . DS . $this->language_db_file; } else { return null; } }
/** */ public function read($id, array $options = []) { if (!isset($options['refresh']) && $id == $this->id && $this->config != null) { return $this->config; } try { $data = $this->filesystem->read($this->getPathFromId($id)); $config = $this->validate($this->parse($data)); $this->config = $config; $this->id = $id; $this->timestamp = $this->filesystem->getTimestamp($this->getPathFromId($id)); return $config; } catch (\League\Flysystem\Exception $e) { throw $e; //FIXME better handling } }
/** * @inheritdoc */ public function getTimestamp($path) { try { return $this->fileSystem->getTimestamp($this->getInnerPath($path)); } catch (FileNotFoundException $e) { throw $this->exceptionWrapper($e, $path); } }
/** * @inheritdoc */ protected function _stat($path) { $stat = array('size' => 0, 'ts' => time(), 'read' => true, 'write' => true, 'locked' => false, 'hidden' => false, 'mime' => 'directory'); // If root, just return from above if ($this->root == $path) { $stat['name'] = $this->root; return $stat; } // If not exists, return empty // if ( !$this->fs->has($path)) { // return array(); // } // // $meta = $this->fs->getMetadata($path); try { $meta = $this->fs->getMetadata($path); } catch (FileNotFoundException $e) { $path = $path . '/'; try { $meta = $this->fs->getMetadata($path); } catch (FileNotFoundException $e) { return array(); } } // Get timestamp/size $stat['ts'] = isset($meta['timestamp']) ? $meta['timestamp'] : $this->fs->getTimestamp($path); $stat['size'] = isset($meta['size']) ? $meta['size'] : $this->fs->getSize($path); // Check if file, if so, check mimetype if ($meta['type'] == 'file') { $stat['mime'] = isset($meta['mimetype']) ? $meta['mimetype'] : $this->fs->getMimetype($path); $imgMimes = ['image/jpeg', 'image/png', 'image/gif']; if ($this->urlBuilder && in_array($stat['mime'], $imgMimes)) { $stat['url'] = $this->urlBuilder->getUrl($path, ['ts' => $stat['ts']]); $stat['tmb'] = $this->urlBuilder->getUrl($path, ['ts' => $stat['ts'], 'w' => $this->tmbSize, 'h' => $this->tmbSize, 'fit' => $this->options['tmbCrop'] ? 'crop' : 'contain']); } } // if (!isset($stat['url']) && $this->fs->getUrl()) { // $stat['url'] = 1; // } return $stat; }
/** * {@inheritdoc} */ public function filemtime($path) { return $this->flysystem->getTimestamp($this->buildPath($path)); }
/** * Get the file's last modification time. * * @param string $path * @return int */ public function lastModified($path) { return parent::getTimestamp($path); }
/** * Render markup to load the CSS or JS assets. * * @param string[] $attributes Optional attributes, such as ['async'] * @param string[] $assets The files to be processed * @param string $extension ".css" or ".js" * @param string $source_dir The folder containing the source assets * @param FilterInterface[] $filters How to process these assets * @param string $format_link Template for an HTML link to the asset * @param string $format_inline Template for an inline asset * * @return string */ private function processAssets(array $attributes, array $assets, $extension, $source_dir, $filters, $format_link, $format_inline) { $hashes = []; $path = $this->getDestination(); foreach ($assets as $asset) { if ($this->isAbsoluteUrl($asset)) { $hash = $this->hash($asset); } else { $hash = $this->hash($asset . $this->public->getTimestamp($source_dir . '/' . $asset)); } if (!$this->public->has($path . '/' . $hash . $extension)) { if ($this->isAbsoluteUrl($asset)) { $data = $this->getLoader()->loadUrl($asset); } else { $data = $this->public->read($source_dir . '/' . $asset); } foreach ($filters as $filter) { $data = $filter->filter($data, $asset, $this); } $this->public->write($path . '/' . $hash . $extension, $data); $this->public->write($path . '/' . $hash . '.min' . $extension, $data); } $hashes[] = $hash; } // The file name of our pipelined asset. $hash = $this->hash(implode('', $hashes)); $asset_file = $path . '/' . $hash . '.min' . $extension; $this->concatenateFiles($path, $hashes, $hash, $extension); $this->concatenateFiles($path, $hashes, $hash, '.min' . $extension); $this->createGzip($asset_file); foreach ($this->notifiers as $notifier) { $notifier->created($asset_file); } if ($this->getDestinationUrl() === '') { $url = url($path); } else { $url = $this->getDestinationUrl(); } if ($this->isEnabled()) { $inline_threshold = $this->getInlineThreshold(); if ($inline_threshold > 0 && $this->public->getSize($asset_file) <= $inline_threshold) { return sprintf($format_inline, $this->public->read($asset_file)); } else { return $this->htmlLinks($url, [$hash], '.min' . $extension, $format_link, $attributes); } } else { return $this->htmlLinks($url, $hashes, $extension, $format_link, $attributes); } }
public function testGetTimestamp() { $this->assertInternalType('integer', $this->filesystem->getTimestamp('2.txt')); }
protected function getThumbnailResponse($path, Filesystem $fs, $size, $cachename) { $fileextensionswiththumbnails = $this->container->getParameter('fileextensionswiththumbnails'); $pathexpl = explode('.', $path); $fileextension = strtolower($pathexpl[count($pathexpl) - 1]); $ctime = new \DateTime(); if ($size == 'xxs' || !in_array($fileextension, $fileextensionswiththumbnails)) { $imgdata = $this->getIcon($path, $size); } else { $timestamp = $fs->getTimestamp($path); $fromcache = $this->getImageFromCache($cachename, $timestamp); if ($fromcache) { //ist bereits im cache: $thumbnaildata = $fromcache; $ctime = new \DateTime(); $ctime->setTimestamp(intval($thumbnaildata[0])); $imgdata = $thumbnaildata[1]; } else { $imgdata = $fs->getThumbnail($path, $size); if ($imgdata) { $this->memcached->set($cachename, serialize($ctime->getTimestamp() . '|' . $imgdata)); } else { //icon ausgeben: $imgdata = $this->getIcon($path, $size); } } } $expires = 1 * 24 * 60 * 60; $response = new Response($imgdata); $response->headers->set('Content-Length', strlen($imgdata)); $response->headers->set('Pragma', 'public'); $response->headers->set('Last-Modified', $ctime->format('D, d M Y H:i:s') . ' GMT'); $response->headers->set('Cache-Control', 'maxage=' . $expires); $response->headers->set('Content-Type', 'image/png; charset=UTF-8'); $response->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); return $response; }
/** * Get a file's timestamp * * ```php * getTimestamp('cache/file.tmp') * getTimestamp('~/file.tmp$/') * ``` * * @param string $path path to file or regexp pattern * @return string|false timestamp or FALSE when fails * to fetch timestamp from existing file */ public function getTimestamp($path) { if (StringHelper::isRegexp($path) && !($path = $this->searchByPattern($path))) { return false; } try { return parent::getTimestamp($path); } catch (\Exception $e) { $this->errors[] = $e->getMessage(); } return false; }