/** * Set the streamed response headers. * @param StreamedResponse $response The response object. * @return StreamedResponse */ public function setHeaders(StreamedResponse $response) { $response->headers->set('Content-Type', $this->cache->getMimetype($this->path)); $response->headers->set('Content-Length', $this->cache->getSize($this->path)); $response->setPublic(); $response->setMaxAge(31536000); $response->setExpires(date_create()->modify('+1 years')); $response->setLastModified(date_create()->setTimestamp($this->cache->getTimestamp($this->path))); $response->isNotModified($this->request); return $response; }
/** * Return stat for given path. * Stat contains following fields: * - (int) size file size in b. required * - (int) ts file modification time in unix time. required * - (string) mime mimetype. required for folders, others - optionally * - (bool) read read permissions. required * - (bool) write write permissions. required * - (bool) locked is object locked. optionally * - (bool) hidden is object hidden. optionally * - (string) alias for symlinks - link target path relative to root path. optionally * - (string) target for symlinks - link target path. optionally * * If file does not exists - returns empty array or false. * * @param string $path file path * @return array|false **/ protected function _stat($path) { $stat = array('mime' => 'directory', 'ts' => time(), 'read' => true, 'write' => true, 'locked' => false, 'hidden' => false, 'size' => 0); // 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); // 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 push(BackupInterface $backup) { $backupDirectory = $backup->getName(); if (!$this->flysystem->has($backupDirectory) && !$this->flysystem->createDir($backupDirectory)) { throw new DestinationException(sprintf('Unable to create backup directory "%s" in flysystem destination.', $backupDirectory)); } $removedBackupFiles = $this->getFiles($backupDirectory); /** * @var FileInterface $backupFile */ foreach ($backup->getFiles() as $backupFile) { if (isset($removedBackupFiles[$backupFile->getRelativePath()])) { unset($removedBackupFiles[$backupFile->getRelativePath()]); } $path = $backupDirectory . '/' . $backupFile->getRelativePath(); try { if ($this->flysystem->has($path)) { if ($backupFile->getModifiedAt() > new \DateTime('@' . $this->flysystem->getTimestamp($path))) { $resource = fopen($backupFile->getPath(), 'r'); $this->flysystem->updateStream($path, $resource); fclose($resource); } } else { $resource = fopen($backupFile->getPath(), 'r'); $this->flysystem->putStream($path, $resource); fclose($resource); } } catch (\Exception $e) { throw new DestinationException(sprintf('Unable to backup file "%s" to flysystem destination.', $backupFile->getPath()), 0, $e); } } /** * @var FileInterface $removedBackupFile */ foreach ($removedBackupFiles as $removedBackupFile) { $path = $backupDirectory . '/' . $removedBackupFile->getRelativePath(); try { $this->flysystem->delete($path); } catch (\Exception $e) { throw new DestinationException(sprintf('Unable to cleanup backup destination "%s" after backup process, file "%s" could not be removed.', $backupDirectory, $path), 0, $e); } } $this->removeEmptyDirectories($backupDirectory); if (is_array($this->backups)) { $this->backups[$backup->getName()] = $backup; } }
/** * @override * @inheritDoc */ public function getTimestamp($path) { try { return $this->fs->getTimestamp($path); } catch (Error $ex) { } catch (Exception $ex) { } throw new ReadException("File {$path} timestamp could not be determined.", $ex); }
function it_can_remove_old_backups(FilesystemInterface $filesystem) { $files = [['path' => '.gitignore'], ['path' => 'foo.sql'], ['path' => 'foo1.sql'], ['path' => 'foo2.sql'], ['path' => 'foo3.sql'], ['path' => 'foo4.sql'], ['path' => 'foo5.sql'], ['path' => 'foo6.sql']]; $filesystem->listFiles()->willReturn($files); $filesystem->getTimestamp('foo.sql')->willReturn(10000); $filesystem->getTimestamp('foo1.sql')->willReturn(500); $filesystem->getTimestamp('foo2.sql')->willReturn(10000); $filesystem->getTimestamp('foo3.sql')->willReturn(10000); $filesystem->getTimestamp('foo4.sql')->willReturn(100); $filesystem->getTimestamp('.gitignore')->willReturn(50); $filesystem->getTimestamp('foo5.sql')->willReturn(10000); $filesystem->getTimestamp('foo6.sql')->willReturn(10000); $filesystem->delete('foo1.sql')->shouldBeCalled(); $filesystem->delete('foo4.sql')->shouldBeCalled(); $this->removeOldBackups(5)->shouldReturn(['foo4.sql', 'foo1.sql']); }
/** * Return stat for given path. * Stat contains following fields: * - (int) size file size in b. required * - (int) ts file modification time in unix time. required * - (string) mime mimetype. required for folders, others - optionally * - (bool) read read permissions. required * - (bool) write write permissions. required * - (bool) locked is object locked. optionally * - (bool) hidden is object hidden. optionally * - (string) alias for symlinks - link target path relative to root path. optionally * - (string) target for symlinks - link target path. optionally * * If file does not exists - returns empty array or false. * * @param string $path file path * @return array|false **/ 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)) { // Check if the parent doesn't have this path if ($this->_dirExists($path)) { return $stat; } // Neither a file or directory exist, return empty return array(); } $meta = $this->fs->getMetadata($path); // getMetadata() on failure if (!$meta) { return array(); } // Set item filename.extension to `name` if exists if (isset($meta['filename']) && isset($meta['extension'])) { $stat['name'] = $meta['filename']; if ($meta['extension'] !== '') { $stat['name'] .= '.' . $meta['extension']; } } // 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; }
/** * Get the file's last modification time. * * @param string $path * @return int */ public function lastModified($path) { return $this->driver->getTimestamp($path); }
/** * @param \Illuminate\Support\Collection $files * @return \Illuminate\Support\Collection */ private function sortFilesByCreationDate($files) { return $files->sortBy(function ($file) { return $this->filesystem->getTimestamp($file['path']); }); }
/** * Get a file's timestamp. * * @param string $path The path to the file. * * @throws FileNotFoundException * * @return string|false The timestamp or false on failure. */ public function getTimestamp($path) { return $this->fileSystem->getTimestamp($path); }