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; }
/** * The getExpired call should return an empty array if there * are no expired assets. */ function testGetExpiredEmpty() { // Empty Binary Pool $this->assertEqual(array(), binarypool_browser::getExpired('test')); // Binary Pool with one asset which has not been put in a view $asset = $this->storage->save('IMAGE', array('_' => array('file' => $this->testfile))); $this->assertEqual(array(), binarypool_browser::getExpired('test')); // Binary Pool with one asset which has not expired binarypool_views::created('test', $asset); $this->assertEqual(array(), binarypool_browser::getExpired('test')); }
function walk_callback($bucket, $root, $file) { global $processed, $storage, $max; if ($max > 0 && $processed >= $max) { printf("[%10s] Processed %d files. Terminating.\n", $bucket, $processed); exit(0); } if (is_link(rtrim($root . $file, '/'))) { // Ignore symlinks return; } if (!file_exists($root . $file . 'index.xml')) { return; } if (!assert_filemtime($root . $file . 'index.xml', $bucket)) { return false; } printf("[%10s] %s\n", $bucket, $file); $processed++; try { $asset = $storage->getAssetObject($file . 'index.xml'); if (file_exists($asset->getOriginal())) { printf("[%10s] Uploading file.\n", $bucket); $files = array('_' => array('file' => $asset->getOriginal())); foreach ($asset->getRenditions() as $key => $rendition) { if (file_exists($rendition)) { // Local file $files[$key] = array('file' => $rendition); } } $type = $asset->getType(); if (!$storage->save($type, $files, true)) { printf("[%10s] ERROR: Could not save asset.\n", $bucket); return; } } else { $asset->setBasePath($asset->getBasePath(), false); if (!$storage->saveAsset($asset, $file . 'index.xml')) { printf("[%10s] ERROR: Could not save asset.\n", $bucket); return; } printf("[%10s] Saved asset.\n", $bucket); binarypool_views::created($bucket, $file . 'index.xml', array('URL' => '')); } } catch (Exception $e) { printf("[%10s] ERROR: Got exception while saving asset.\n", $bucket); return; } // Move to trash $basepath = $asset->getBasePath(); $date = date('Y/m/d'); $trashDir = 'Trash/' . $date . '/' . $basepath; $trashDirAbs = $root . $trashDir; $idx = 0; while (file_exists($trashDirAbs)) { $trashDirAbs = $root . $trashDir . '-' . $idx; $idx++; } printf("[%10s] Moving file version to trash: %s\n", $bucket, $trashDirAbs); $trashParent = dirname($trashDirAbs); if (!file_exists($trashParent)) { mkdir($trashParent, 0755, true); } rename($root . $file, $trashDirAbs); symlink($trashDirAbs, rtrim($root . $file, '/')); }
protected function getFilesFromUrl() { $url = $this->request->getParam('URL'); $this->log->debug("Downloading file: %s", $url); if (!self::$lastModified) { self::$lastModified = new binarypool_lastmodified(); } $lastmodified = self::$lastModified->lastModified($this->bucket, $url); if (binarypool_config::getCacheRevalidate($this->bucket) === 0) { $lastmodified['time'] = 0; } $tmpfile = tempnam(sys_get_temp_dir(), 'binary'); if ($tmpfile == '' || $tmpfile === FALSE) { throw new binarypool_exception(104, 500, "Could not create temporary file"); } array_push($this->tmpfiles, $tmpfile); $result = array('code' => 0, 'headers' => array(), 'body' => ''); $retries = 3; if ($lastmodified['revalidate']) { $httpc = new binarypool_httpclient(); while ($retries) { try { $result = $httpc->download($url, $tmpfile, $lastmodified['time']); if ($result['code'] < 500) { break; } } catch (binarypool_httpclient_exception $e) { // ignore - dropped connections etc. - retry $this->log->debug("Failed download attempt from %s: %s", $url, $e); } sleep(1); $retries--; } } else { $result['code'] = 304; } if (304 == $result['code']) { $this->log->debug("File %s has not been modified", $url); return array(); } if ($result['code'] != 200 || !filesize($tmpfile)) { binarypool_views::flagBadUrl($this->bucket, $url); throw new binarypool_exception(121, 400, "File could not be fetched from URL: " . $url); } $url_parsed = parse_url($url); $filename = basename($url_parsed['path']); # Restrict filenames TO ALPHANUMS and reduce sequences of '.' to avoid # traversal issues, unicode issues, command injection etc. $filename = preg_replace(array('#\\.{2,}#', '#[^a-zA-Z0-9\\.]+#'), array('.', '_'), $filename); return array('_' => array('file' => $tmpfile, 'filename' => $filename)); }
function assignMockStorageFactory($storage) { $storageFactory = new Mock_binarypool_storagefactory(); $storageFactory->setReturnValue('getStorage', $storage); binarypool_views::$storageFactory = $storageFactory; }
private static function getStorage($bucket) { if (!self::$storageFactory) { self::$storageFactory = new binarypool_storagefactory(); } return self::$storageFactory->getStorage($bucket); }
/** * 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); }