/** * Create the response. * * @param \League\Flysystem\FilesystemInterface $cache The cache file system. * @param string $path The cached file path. * * @return \Cake\Network\Response The response object. */ public function create(FilesystemInterface $cache, $path) { $stream = $cache->readStream($path); $contentType = $cache->getMimetype($path); $contentLength = (string) $cache->getSize($path); $response = new Response(); $response->type($contentType); $response->header('Content-Length', $contentLength); $response->body(function () use($stream) { rewind($stream); fpassthru($stream); fclose($stream); }); return $response; }
/** * @return ResourceDOInterface SuspectResource if it have been deleted or OriginResource if the Suspect is not equal */ public function __invoke() { $originName = $this->originResourceDO->getName(); $suspectName = $this->suspectResourceDO->getName(); $originType = $this->originResourceDO->getType(); $suspectType = $this->suspectResourceDO->getType(); $originFilePath = $this->originResourceDO->getFilePath(); $suspectFilePath = $this->suspectResourceDO->getFilePath(); if (!$originName || !$originType) { throw new CommandErrorException('Cannot destroy equal resource: the origin resource is empty'); } if (!$suspectName || !$suspectType) { throw new CommandErrorException('Cannot destroy equal resource: the suspect resource is empty'); } if ($originFilePath === $suspectFilePath) { throw new CommandErrorException('Cannot destroy equal resource: Origin and Suspect have same paths'); } // Unfortunately, this condition can not always work fine. // Because some Middlewares can compress, resize etc. the resource that saved before // and the second uploaded copy will never be equal if ($originType === $suspectType && $this->filesystem->has($originFilePath) === $this->filesystem->has($suspectFilePath) && $this->filesystem->getSize($originFilePath) === $this->filesystem->getSize($suspectFilePath) && $this->getFileHash($originFilePath) === $this->getFileHash($suspectFilePath)) { $command = new DestroyResourceCommand($this->suspectResourceDO, $this->filesystem); return $command(true); } return $this->originResourceDO; }
/** * Return string error message if not found to be correct * * @return string|bool */ public function assertBackups($today) { $day = $today; $sizes = []; for ($i = 0; $i < 2; $i++) { $expected = 's77_mail_' . $day->format('Y-m-d') . '.tar.gz'; try { $size = $this->filesystem->getSize($expected); if ($size === false) { return "Kan bestandsgrootte niet ophalen van '{$expected}'"; } $sizes[] = $size; if ($size < 3.4 * 1000 * 1000 * 1000) { $humanSize = $size / 1000 / 1000; return "Email backup lijkt te klein ({$humanSize} MB)"; } } catch (\League\Flysystem\FileNotFoundException $e) { return "Kon email backup niet vinden: '{$expected}'"; } $day = $day->sub(new \DateInterval('P1D')); } if (count(array_unique($sizes)) === 1) { return "Laatste twee e-mail backups zijn even groot."; } return true; }
public function testLastBackupsIdentical() { $meta = $this->getMeta(123123123123.0); $this->fs->getSize('s77_mail_2015-04-22.tar.gz')->willReturn($meta); $this->fs->getSize('s77_mail_2015-04-21.tar.gz')->willReturn($meta); $result = $this->assert->assertBackups(new \DateTime('2015-04-22')); $this->assertEquals("Laatste twee e-mail backups zijn even groot.", $result); }
/** * 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; }
/** * Create response. * @param FilesystemInterface $cache Cache file system. * @param string $path Cached file path. * @return Response Response object. */ public function create(FilesystemInterface $cache, $path) { $stream = $this->streamCallback->__invoke($cache->readStream($path)); $contentType = $cache->getMimetype($path); $contentLength = (string) $cache->getSize($path); $cacheControl = 'max-age=31536000, public'; $expires = date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT'; return $this->response->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength)->withHeader('Cache-Control', $cacheControl)->withHeader('Expires', $expires); }
/** * @return resource */ public function getGDHandle() { if (!$this->is_image) { return null; } $size = $this->_filesystem->getSize($this->getPath()); $handle = $this->_filesystem->readStream($this->getPath()); return imagecreatefromstring(fread($handle, $size)); }
/** * @override * @inheritDoc */ public function getSize($path) { try { $size = $this->fs->getSize($path); return $size === false ? 0 : $size; } catch (Error $ex) { } catch (Exception $ex) { } throw new ReadException("File {$path} size could not be determined.", $ex); }
/** * Returns size of a file. * * @param $path * @return int * @throws IoReadException */ public function size($path) { try { return $this->fs->getSize($path); } catch (Error $ex) { throw new IoReadException("File {$path} size could not be determined.", $ex); } catch (Exception $ex) { throw new IoReadException("File {$path} size could not be determined.", $ex); } }
/** * Generate and output image. * @param string $path Image path. * @param array $params Image manipulation params. * @throws InvalidArgumentException */ public function outputImage($path, array $params) { $path = $this->makeImage($path, $params); header('Content-Type:' . $this->cache->getMimetype($path)); header('Content-Length:' . $this->cache->getSize($path)); header('Cache-Control:' . 'max-age=31536000, public'); header('Expires:' . date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT'); $stream = $this->cache->readStream($path); rewind($stream); fpassthru($stream); fclose($stream); }
/** * Create response. * * @param \League\Flysystem\FilesystemInterface $cache Cache file system. * @param string $path Cached file path. * * @return \Psr\Http\Message\ResponseInterface Response object. */ public function create(FilesystemInterface $cache, $path) { $stream = new Stream($cache->readStream($path)); $contentType = $cache->getMimetype($path); $contentLength = (string) $cache->getSize($path); if ($contentType === false) { throw new FilesystemException('Unable to determine the image content type.'); } if ($contentLength === false) { throw new FilesystemException('Unable to determine the image content length.'); } return (new Response())->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength); }
/** * Create response. * @param FilesystemInterface $cache Cache file system. * @param string $path Cached file path. * @return ResponseInterface Response object. */ public function create(FilesystemInterface $cache, $path) { $stream = $this->streamCallback->__invoke($cache->readStream($path)); $contentType = $cache->getMimetype($path); $contentLength = (string) $cache->getSize($path); $cacheControl = 'max-age=31536000, public'; $expires = date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT'; if ($contentType === false) { throw new FilesystemException('Unable to determine the image content type.'); } if ($contentLength === false) { throw new FilesystemException('Unable to determine the image content length.'); } return $this->response->withBody($stream)->withHeader('Content-Type', $contentType)->withHeader('Content-Length', $contentLength)->withHeader('Cache-Control', $cacheControl)->withHeader('Expires', $expires); }
/** * 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 size of a given file. * * @param string $path * @return int */ public function size($path) { return $this->driver->getSize($path); }
/** * Get a file's size. * * @param string $path The path to the file. * * @return int|false The file size or false on failure. */ public function getSize($path) { return $this->fileSystem->getSize($path); }
/** * Sets the contents of a file to the specified value. * * @param string $fileIdentifier * @param string $contents * @return int The number of bytes written to the file */ public function setFileContents($fileIdentifier, $contents) { $this->filesystem->put($fileIdentifier, $contents); return $this->filesystem->getSize($fileIdentifier); }
/** * @inheritdoc */ public function getSize($path) { $this->migrateFile($path); $path = $this->strategy->encode($path); return $this->filesystem->getSize($path); }