/** * Get the filesystem manager. * * @return MountManager * @throws \Exception */ public static function getFilesystem() { $config = new Config(); $manager = new MountManager(); foreach ($config->filesystems() as $name => $fsConfig) { $adapterName = '\\League\\Flysystem\\Adapter\\' . self::camelcase($fsConfig['type']); $adapter = new $adapterName($fsConfig['root']); $fs = new Filesystem($adapter); $manager->mountFilesystem($name, $fs); } return $manager; }
public function testLocalCacheFile() { $config = new Config(); $this->assertArrayHasKey('cache', $config->filesystems()); $item = new Item(null, $this->testUser); $item->save([], null, null, 'Test file contents.'); $this->assertSame(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . '1_v1_o', $item->getCachePath()); $this->assertFileExists(__DIR__ . '/data/cache/1_v1_o'); }
/** * Get a local filesystem path to the requested file, creating it if required. * @param string $format The file format, one of 'o', 'd', or 't'. * @param int $version The version of the file to fetch. * @return string The fully-qualified path to the file. * @throws \Exception */ public function getCachePath($format = 'o', $version = null) { if (is_null($version)) { $version = $this->getVersionCount(); } if ($version > $this->getVersionCount()) { throw new Exception("Version {$version} does not exist for Item " . $this->getId()); } $filesystem = App::getFilesystem(); $path = $this->getFilePath($version); // Get local filesystem root. $config = new Config(); $filesystems = $config->filesystems(); $root = realpath($filesystems['cache']['root']); // First of all copy the original file to the cache. $filenameOrig = $this->getId() . '_v' . $version . '_o'; if (!$filesystem->has("cache://" . $filenameOrig)) { $filesystem->copy("storage://{$path}", "cache://" . $filenameOrig); } $pathnameOrig = $root . DIRECTORY_SEPARATOR . $filenameOrig; if ($format === 'o') { return $pathnameOrig; } // Then create smaller version if required. $filenameDisplay = $this->getId() . '_v' . $version . '_t'; $pathnameDisplay = $root . DIRECTORY_SEPARATOR . $filenameDisplay; $manager = new ImageManager(); $image = $manager->make($pathnameOrig); if ($format === 'd') { // 'Display' size. $width = $image->getWidth(); $height = $image->getHeight(); $newWidth = $width > $height ? 700 : null; $newHeight = $width > $height ? null : 700; $image->resize($newWidth, $newHeight, function (Constraint $constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); } else { // Thumbnail. $image->fit(200); } $image->save($pathnameDisplay); clearstatcache(false, $pathnameDisplay); return $pathnameDisplay; }