function getStorageAdapters() { global $ZendDb, $acl; $DirectusStorageAdaptersTableGateway = new DirectusStorageAdaptersTableGateway($acl, $ZendDb); $storageAdapters = $DirectusStorageAdaptersTableGateway->fetchAllByIdNoParams(); $adaptersByUniqueRole = array(); foreach ($storageAdapters as $adapter) { if (!empty($adapter['role'])) { $storageAdapters[$adapter['role']] = $adapter; } } return $storageAdapters; }
echo "<h1>404 Not found</h1>"; exit; }; $DirectusMedia = new TableGateway('directus_files', $ZendDb); $media = $DirectusMedia->select(function ($select) use($id) { $select->where->equalTo('id', $id); $select->limit(1); }); if (0 == $media->count()) { return $notFound(); } $media = $media->current(); if ($filename != $media['name']) { $correctUrl = $app->urlFor('media_proxy_file', array('id' => $id, 'format' => $format, 'filename' => $media['name'])); return $app->redirect($correctUrl); } $StorageAdapters = new DirectusStorageAdaptersTableGateway($acl, $ZendDb); $storage = $StorageAdapters->find($media['storage_adapter']); $params = @json_decode($storage['params'], true); $params = empty($params) ? array() : $params; $storage['params'] = $params; $MediaStorage = \Directus\Files\Storage\Storage::getStorage($storage); header('Content-type: ' . $media['type']); echo $MediaStorage->getFileContents($media['name'], $storage['destination']); exit; // Prevent Slim from overriding our headers })->conditions(array('id' => '\\d+'))->name('media_proxy_file'); foreach (glob("client_auth_proxies/*.php") as $filename) { require_once $filename; } $app->run();
public function addOrUpdateRecordByArray(array $recordData, $tableName = null) { foreach ($recordData as $columnName => $columnValue) { if (is_array($columnValue)) { $table = is_null($tableName) ? $this->table : $tableName; throw new SuppliedArrayAsColumnValue("Attempting to write an array as the value for column `{$table}`.`{$columnName}`."); } } $tableName = is_null($tableName) ? $this->table : $tableName; $rowExists = isset($recordData['id']); $TableGateway = new self($this->acl, $tableName, $this->adapter); if ($rowExists) { $Update = new Update($tableName); $Update->set($recordData); $Update->where(array('id' => $recordData['id'])); $TableGateway->updateWith($Update); Hooks::runHook('postUpdate', array($TableGateway, $recordData, $this->adapter, $this->acl)); } else { //If we are adding a new directus_files Item, We need to do that logic // @todo: clean up/refactor saving file process if ($tableName == "directus_files") { $Storage = new \Directus\Files\Storage\Storage(); //If trying to save to temp, force to default if (!isset($recordData['storage_adapter']) || $recordData['storage_adapter'] == '' || $Storage->storageAdaptersByRole['TEMP']['id'] == $recordData['storage_adapter']) { $recordData['storage_adapter'] = $Storage->storageAdaptersByRole['DEFAULT']['id']; } $StorageAdapters = new DirectusStorageAdaptersTableGateway($this->acl, $this->adapter); //If desired Storage Adapter Exists... $filesAdapter = $StorageAdapters->fetchOneById($recordData['storage_adapter']); //Save Temp Thumbnail name for use after files record save // @todo: make file name format sanatize by default // same as uniqueName by the adapter // replacing space with underscore $originalFile = $recordData['file_name']; // we do not need it part of our records Data unset($recordData['file_name']); $recordData['name'] = str_replace(' ', '_', $recordData['name']); $info = pathinfo($recordData['name']); if (in_array($info['extension'], $this->imagickExtensions)) { $thumbnailName = "THUMB_" . $info['filename'] . '.jpg'; } else { $thumbnailName = "THUMB_" . $recordData['name']; } //If we are using files ID, Dont save until after insert if ($Storage->getFilesSettings()['file_naming'] == "file_name") { //Save the file in TEMP Storage Adapter to Designated StorageAdapter $recordData['name'] = $Storage->saveFile($recordData['name'], $recordData['storage_adapter']); // $fileData = $Storage->saveData($recordData['data'], $recordData['name'], $filesAdapter['destination']); // $recordData['name'] = $fileData['name']; } else { if ($Storage->getFilesSettings()['file_naming'] == "file_hash") { //Save the file in TEMP Storage Adapter to Designated StorageAdapter $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION); $fileHashName = md5(microtime() . $recordData['name']); $newName = $Storage->saveFile($recordData['name'], $recordData['storage_adapter'], $fileHashName . '.' . $ext); $updateArray['name'] = $fileHashName . '.' . $ext; $recordData['name'] = $updateArray['name']; } } } $d = $recordData; unset($d['data']); $TableGateway->insert($d); $recordData['id'] = $TableGateway->getLastInsertValue(); if ($tableName == "directus_files") { $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION); $updateArray = array(); //If using file_id saving, then update record and set name to id if ($Storage->getFilesSettings()['file_naming'] == "file_id") { $newName = $Storage->saveFile($recordData['name'], $recordData['storage_adapter'], str_pad($recordData['id'], 11, "0", STR_PAD_LEFT) . '.' . $ext); $updateArray['name'] = str_pad($recordData['id'], 11, "0", STR_PAD_LEFT) . '.' . $ext; $recordData['name'] = $updateArray['name']; } // @todo: do not make this file create twice. // file should be copied to temp and then work from there. // but is copied on "media" also on "temp" then copied back to "media" if (file_exists($filesAdapter['destination'] . $originalFile)) { unlink($filesAdapter['destination'] . $originalFile); } if (!empty($updateArray)) { $Update = new Update($tableName); $Update->set($updateArray); $Update->where(array('id' => $recordData['id'])); $TableGateway->updateWith($Update); } //Save Temp Thumbnail to Thumbnail SA using file id: $params['id'] $tempLocation = $Storage->storageAdaptersByRole['TEMP']['destination']; if (file_exists($tempLocation . $thumbnailName)) { $thumbnailDestination = $Storage->storageAdaptersByRole['THUMBNAIL']['destination']; if (in_array($ext, $this->imagickExtensions)) { $ext = 'jpg'; } $Storage->ThumbnailStorage->acceptFile($tempLocation . $thumbnailName, $recordData['id'] . "." . $ext, $thumbnailDestination); unlink($tempLocation . $thumbnailName); } } Hooks::runHook('postInsert', array($TableGateway, $recordData, $this->adapter, $this->acl)); } $columns = TableSchema::getAllNonAliasTableColumnNames($tableName); $recordData = $TableGateway->fetchAll(function ($select) use($recordData, $columns) { $select->columns($columns)->limit(1); $select->where->equalTo('id', $recordData['id']); })->current(); return $recordData; }
use Directus\Db\TableGateway\DirectusSettingsTableGateway; use Directus\Db\TableGateway\DirectusStorageAdaptersTableGateway; use Directus\Files\Thumbnail; use Zend\Db\TableGateway\TableGateway; require 'directusLoader.php'; function out($string) { echo "{$string}\n"; } $supportedExtensions = array('jpg', 'jpeg', 'png', 'gif'); out("Running script with the following supported extensions: " . implode(", ", $supportedExtensions)); $db = \Directus\Bootstrap::get('ZendDb'); $acl = \Directus\Bootstrap::get('acl'); $Settings = new DirectusSettingsTableGateway($acl, $db); $filesSettings = $Settings->fetchCollection('media', array('storage_adapter', 'storage_destination', 'thumbnail_storage_adapter', 'thumbnail_storage_destination', 'thumbnail_size', 'thumbnail_quality', 'thumbnail_crop_enabled')); $StorageAdapters = new DirectusStorageAdaptersTableGateway($acl, $db); $storageAdaptersById = $StorageAdapters->fetchAllWithIdKeys(); foreach ($storageAdaptersById as $id => $storageAdapter) { $storageAdaptersById[$id] = \Directus\Files\Storage\Storage::getStorage($storageAdapter); } out("\nLoaded " . count($storageAdaptersById) . " storage adapters."); $DirectusMedia = new TableGateway('directus_files', $db); $mediaRecords = $DirectusMedia->select(); out("Found " . $mediaRecords->count() . " Directus media records."); $thumbnailStorageAdapterResultSet = $StorageAdapters->fetchByUniqueRoles(array('THUMBNAIL')); if (1 != count($thumbnailStorageAdapterResultSet)) { throw new \RuntimeException("Fatal: exactly one storage adapter with role THUMBNAIL is required"); } $thumbnailStorageAdapterSettings = array_pop($thumbnailStorageAdapterResultSet); if ('FileSystemAdapter' != $thumbnailStorageAdapterSettings['adapter_name']) { throw new \RuntimeException("Fatal: THUMBNAIL storage adapter: only FileSystemAdapter is currently supported by this script");
public function saveFile($fileName, $destStorageAdapterId, $newName = null) { $settings = $this->filesSettings; $finalName = null; $StorageAdapters = new DirectusStorageAdaptersTableGateway($this->acl, $this->adapter); //If desired Storage Adapter Exists... $filesAdapter = $StorageAdapters->fetchOneById($destStorageAdapterId); if ($filesAdapter) { //Get Temp File Path from Temp StorageAdapter $tempLocation = $this->storageAdaptersByRole['TEMP']['destination']; //Try to accept file into new fella if (file_exists($tempLocation . $fileName)) { $destName = $newName == null ? $fileName : $newName; $finalPath = $this->FilesStorage->acceptFile($tempLocation . $fileName, $destName, $filesAdapter['destination']); $finalName = basename($finalPath); unlink($tempLocation . $fileName); } else { $finalName = $fileName; } } else { die("ERROR! No Storage Adapter found with Designated ID: " . $destStorageAdapterId); } return $finalName; }
use Directus\Db\TableGateway\DirectusSettingsTableGateway; use Directus\Db\TableGateway\DirectusStorageAdaptersTableGateway; use Directus\Files\Thumbnail; use Zend\Db\TableGateway\TableGateway; require 'directusLoader.php'; function out($string) { echo "{$string}\n"; } $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'psd', 'psf', 'tif', 'tiff']; out('Running script with the following supported extensions: ' . implode(', ', $supportedExtensions)); $db = \Directus\Bootstrap::get('ZendDb'); $acl = \Directus\Bootstrap::get('acl'); $Settings = new DirectusSettingsTableGateway($acl, $db); $filesSettings = $Settings->fetchCollection('media', ['thumbnail_size', 'thumbnail_quality', 'thumbnail_crop_enabled']); $StorageAdapters = new DirectusStorageAdaptersTableGateway($acl, $db); $storageAdaptersById = $StorageAdapters->fetchAllWithIdKeys(); foreach ($storageAdaptersById as $id => $storageAdapter) { $storageAdaptersById[$id] = \Directus\Files\Storage\Storage::getStorage($storageAdapter); } out('Loaded ' . count($storageAdaptersById) . ' storage adapters.'); $DirectusMedia = new TableGateway('directus_files', $db); $mediaRecords = $DirectusMedia->select(); out('Found ' . $mediaRecords->count() . ' Directus media records.'); $thumbnailStorageAdapterResultSet = $StorageAdapters->fetchByUniqueRoles(['THUMBNAIL']); if (1 != count($thumbnailStorageAdapterResultSet)) { throw new \RuntimeException('Fatal: exactly one storage adapter with role THUMBNAIL is required'); } $thumbnailStorageAdapterSettings = array_pop($thumbnailStorageAdapterResultSet); if ('FileSystemAdapter' != $thumbnailStorageAdapterSettings['adapter_name']) { throw new \RuntimeException('Fatal: THUMBNAIL storage adapter: only FileSystemAdapter is currently supported by this script');