/** * Set this store as the new asset backend * * @param string $basedir Basedir to store assets, which will be placed beneath 'assets' folder */ public static function activate($basedir) { // Assign this as the new store $publicAdapter = new PublicAssetAdapter(ASSETS_PATH . '/' . $basedir); $publicFilesystem = new Filesystem($publicAdapter, ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]); $protectedAdapter = new ProtectedAssetAdapter(ASSETS_PATH . '/' . $basedir . '/.protected'); $protectedFilesystem = new Filesystem($protectedAdapter, ['visibility' => AdapterInterface::VISIBILITY_PRIVATE]); $backend = new AssetStoreTest_SpyStore(); $backend->setPublicFilesystem($publicFilesystem); $backend->setProtectedFilesystem($protectedFilesystem); Injector::inst()->registerService($backend, 'AssetStore'); // Assign flysystem backend to generated asset handler at the same time $generated = new GeneratedAssetHandler(); $generated->setFilesystem($publicFilesystem); Injector::inst()->registerService($generated, 'GeneratedAssetHandler'); Requirements::backend()->setAssetHandler($generated); // Disable legacy and set defaults Config::inst()->remove(get_class(new FlysystemAssetStore()), 'legacy_filenames'); Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', '/'); DBFile::config()->force_resample = false; File::config()->force_resample = false; self::reset(); self::$basedir = $basedir; // Ensure basedir exists SSFilesystem::makeFolder(self::base_path()); }
/** * Safely encodes the File object with all standard fields required * by the front end * * @param string $filename * @param string $hash * @param string $variant * @return array Encoded list of file attributes */ protected function encodeAssetAttributes($filename, $hash, $variant) { // Force regeneration of file thumbnail for this tuple (without saving into db) $object = DBFile::create(); $object->setValue(array('Filename' => $filename, 'Hash' => $hash, 'Variant' => $variant)); return array('filename' => $filename, 'hash' => $hash, 'variant' => $variant, 'name' => $object->getBasename(), 'url' => $object->getURL(), 'thumbnail_url' => $object->ThumbnailURL($this->getPreviewMaxWidth(), $this->getPreviewMaxHeight()), 'size' => $object->getAbsoluteSize(), 'type' => File::get_file_type($object->getFilename()), 'buttons' => (string) $this->renderWith($this->getTemplateFileButtons()), 'fieldname' => $this->getName()); }