protected function execute() { $uri = $this->request->getPath(); $storage = new binarypool_storage($this->bucket); // Access control if (!$storage->fileExists($uri)) { throw new binarypool_exception(115, 404, "File not found: " . $uri); } if (!$storage->isDir($uri)) { throw new binarypool_exception(115, 404, "File not found: " . $uri); } // List all assets matching the view $xml = '<status method="view">'; $xml .= '<bucket>' . htmlspecialchars($this->bucket) . '</bucket>'; // Remove leading slash and bucket name $dir = substr($uri, 2 + strlen($this->bucket)); $files = $storage->listDir($dir); foreach ($files as $file) { $asset = $storage->getAssetObject($file); $xml .= '<file id="' . htmlspecialchars($asset->getHash()) . '">'; $xml .= htmlspecialchars($asset->getBasePath()); $xml .= '</file>'; } $xml .= '</status>'; array_push($this->data, new api_model_xml($xml)); }
/** * Test preparation: create a binary which can be used for view * testing. */ function setUp() { parent::setUp(); $storage = new binarypool_storage('test'); $asset = $storage->save('IMAGE', array('_' => array('file' => $this->testfile))); $this->assertNotNull($asset); $this->assetFile = $asset; $this->assetId = '096dfa489bc3f21df56eded2143843f135ae967e'; binarypool_lastmodified::resetMemoryCache(); binarypool_views::$lastModified = null; binarypool_views::$storageFactory = null; }
/** * Returns all items which expired in the last seven days * including today. * * All the returned file names are relative paths. */ public static function getExpired($bucket) { $files = array(); $storage = new binarypool_storage($bucket); for ($day = 0; $day < 100; $day++) { // Date directory for given day $dateDir = date('Y/m/d', time() - $day * 24 * 60 * 60); // Get all asset files which expired in those days $retval = $storage->listDir('expiry/' . $dateDir); $files = array_merge($files, $retval); } return $files; }
protected function execute() { $uri = $this->request->getPath(); // Deletions allowed on bucket? $buckets = binarypool_config::getBuckets(); if (!isset($buckets[$this->bucket]['allowDeletions']) || $buckets[$this->bucket]['allowDeletions'] == false) { throw new binarypool_exception(108, 403, "Deletions not allowed on this bucket."); } $storage = new binarypool_storage($this->bucket); $storage->delete($uri); $this->setResponseCode(204); $this->response->send(); $this->ignoreView = true; }
/** * Get the path from the URI and implement access control. */ protected function getPath() { $uri = $this->getUri(); $storage = new binarypool_storage($this->bucket); // Access control if (!$storage->fileExists($uri)) { throw new binarypool_exception(115, 404, "File not found: " . $uri); } if ($storage->isDir($uri)) { $uri .= '/index.xml'; } if (!$storage->isFile($uri)) { throw new binarypool_exception(115, 404, "File not found: " . $uri); } return ltrim($uri, '/'); }
protected function execute() { $hash = $this->route['hash']; $uri = $this->request->getPath(); $storage = new binarypool_storage($this->bucket); $asset = $storage->getAssetBySha1($hash); if (!$storage->fileExists($asset)) { throw new binarypool_exception(115, 404, "File does not exist: {$uri}"); } $this->setResponseCode(302); $this->response->setHeader('X-Asset', $asset); $this->response->setHeader('Content-Type', 'text/xml'); $this->response->setContentLengthOutput(false); $storage->sendFile($asset); $this->response->send(); $this->ignoreView = true; }
/** * Checks if the asset file is an expired resource. */ public static function isExpired($bucket, $asset) { $storage = new binarypool_storage($bucket); $obj = $storage->getAssetObject($asset); if ($obj->getExpiry() > time()) { // Item expires in the future return false; } // Check callbacks foreach ($obj->getCallbacks() as $callback) { $status = self::getCallbackOpinion($callback); if ($status === FALSE) { // Permission not granted return false; } } return true; }
protected function upload($bucket) { $this->checkInput(); // Get params $type = $this->request->getParam('Type'); $callback = $this->request->getParam('Callback', ''); $files = $this->getFiles(); $url = $this->request->getParam('URL'); $created = true; $storage = new binarypool_storage($bucket); // 304 not modified if (0 == count($files) && !empty($url)) { $symlink = binarypool_views::getDownloadedViewPath($bucket, $url); $asset = $storage->getAssetForLink($symlink); $this->log->info("Unmodified file %s", $asset); $created = false; } else { // Save file $asset = $storage->save($type, $files); $this->log->info("Created file %s", $asset); } foreach ($files as $rendition => $file) { unlink($file['file']); } if ($callback !== '') { $storage->addCallback($asset, $callback); } $metadata = array(); $metadata['URL'] = $url; if ($created) { binarypool_views::created($bucket, $asset, $metadata); } else { $assetObj = $storage->getAssetObject($asset); binarypool_views::updated($bucket, $asset, $assetObj, $metadata); } $this->setResponseCode(201); $this->response->setHeader('Location', $asset); $this->response->setHeader('X-Asset', $asset); $xml = "<status method='post'><asset>" . htmlspecialchars($asset) . "</asset></status>"; array_push($this->data, new api_model_xml($xml)); }
protected function touch($bucket, $uri) { $storage = new binarypool_storage($bucket); $assetFile = $uri; if (!$storage->isFile($assetFile)) { $assetFile .= '/index.xml'; if (!$storage->isFile($assetFile)) { return false; } } // Get TTL from request $buckets = binarypool_config::getBuckets(); $ttl = $buckets[$bucket]['ttl']; if ($this->request->getParam('TTL')) { $newttl = intval($this->request->getParam('TTL')); if ($newttl <= $ttl) { // Don't allow higher TTL than bucket configuration $ttl = $newttl; } } // Set TTL $oldAsset = $storage->getAssetObject($assetFile); $asset = $storage->getAssetObject($assetFile); $asset->setExpiry(time() + $ttl * 24 * 60 * 60); $storage->saveAsset($asset, $assetFile); // Update views binarypool_views::updated($bucket, $assetFile, $oldAsset); $this->setResponseCode(204); return true; }
require_once dirname(__FILE__) . '/../localinc/binarypool/storage.php'; require_once dirname(__FILE__) . '/../localinc/binarypool/expiry.php'; function cleanSymlinks() { // Do symlink cleanup printf("[%10s] Cleaning up symlinks.\n", 'FINAL'); $cmd = binarypool_config::getUtilityPath('symlinks'); system("{$cmd} -cdrs " . binarypool_config::getRoot() . "*/created"); system("{$cmd} -cdrs " . binarypool_config::getRoot() . "*/expiry"); system("{$cmd} -cdrsv " . binarypool_config::getRoot() . "*/downloaded |grep '/dev/null' |xargs -0 rm"); } cleanSymlinks(); // Walk through each bucket $buckets = binarypool_config::getBuckets(); foreach (array_keys($buckets) as $bucket) { $storage = new binarypool_storage($bucket); printf("[%10s] Fetching list of expired binaries.\n", $bucket); $expired = binarypool_browser::getExpired($bucket); printf("[%10s] %d expired.\n", $bucket, $expired); foreach ($expired as $asset) { try { if (binarypool_expiry::isExpired($bucket, $asset)) { printf("[%10s] Deleting %s\n", $bucket, $asset); $storage->delete($asset); } } catch (binarypool_exception $e) { if ($e->getCode() == 112) { printf("[%10s] Asset does not exist %s\n", $bucket, $asset); } else { throw $e; }
/** * Load the asset file from storage. */ function testLoadAssetFileFromStorage() { $this->testAssetWithRendition90x90(); $storage = new binarypool_storage('test'); $basepath = 'test/somehashhere/'; $basepathAbs = binarypool_config::getRoot() . $basepath; $asset = $storage->getAssetObject($basepath . 'index.xml'); $this->assertEqual('test/somehashhere/', $asset->getBasePath()); $this->assertEqual($basepathAbs . 'vw_golf.jpg', $asset->getOriginal()); $this->assertEqual($basepathAbs . 'resultlist.jpg', $asset->getRendition('resultlist')); $this->assertEqual(array('resultlist' => $basepathAbs . 'resultlist.jpg'), $asset->getRenditions()); $this->assertEqual('096dfa489bc3f21df56eded2143843f135ae967e', $asset->getHash()); }
/** * Looks in the URL view for a symlink matching the provided * URL and if found, returns the mtime of the link target * * If the link points to /dev/null, we have a URL we were unable * to download before - if that symlink itself is older than 1 hour * we delete it and return 0. If the symlink is younger than hour, * we raise an exception and abort. Intended as mechanism to prevent * repeated attempts to download the same (non 200) URL in a short * time period * * @param String $bucket * @param String $url * @return array(time => int Unix timestamp (0 means not found), revalidate => (true|false), cache_age => int) */ public function getURLLastModified($bucket, $url) { $storage = new binarypool_storage($bucket); $symlink = binarypool_views::getDownloadedViewPath($bucket, $url); return $storage->getURLLastModified($url, $symlink); }