2015-09-03 07:46:08 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Assets\Flysystem;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
use Generator;
|
2016-03-01 06:30:52 +01:00
|
|
|
use LogicException;
|
2015-09-03 07:46:08 +02:00
|
|
|
use InvalidArgumentException;
|
2016-01-13 06:17:45 +01:00
|
|
|
use League\Flysystem\Directory;
|
2015-09-03 07:46:08 +02:00
|
|
|
use League\Flysystem\Exception;
|
|
|
|
use League\Flysystem\Filesystem;
|
2016-01-13 06:17:45 +01:00
|
|
|
use League\Flysystem\FilesystemInterface;
|
2015-09-03 07:46:08 +02:00
|
|
|
use League\Flysystem\Util;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\Storage\AssetNameGenerator;
|
|
|
|
use SilverStripe\Assets\Storage\AssetStore;
|
|
|
|
use SilverStripe\Assets\Storage\AssetStoreRouter;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\Control\SS_HTTPResponse;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Flushable;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asset store based on flysystem Filesystem as a backend
|
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
class FlysystemAssetStore implements AssetStore, AssetStoreRouter, Flushable {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Session key to use for user grants
|
|
|
|
*/
|
|
|
|
const GRANTS_SESSION = 'AssetStore_Grants';
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Filesystem
|
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
private $publicFilesystem = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filesystem to use for protected files
|
|
|
|
*
|
|
|
|
* @var Filesystem
|
|
|
|
*/
|
|
|
|
private $protectedFilesystem = null;
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable to use legacy filename behaviour (omits hash)
|
|
|
|
*
|
2015-12-09 22:19:23 +01:00
|
|
|
* Note that if using legacy filenames then duplicate files will not work.
|
|
|
|
*
|
2015-09-03 07:46:08 +02:00
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $legacy_filenames = false;
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* Flag if empty folders are allowed.
|
|
|
|
* If false, empty folders are cleared up when their contents are deleted.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $keep_empty_dirs = false;
|
|
|
|
|
2016-01-13 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Set HTTP error code for requests to secure denied assets.
|
|
|
|
* Note that this defaults to 404 to prevent information disclosure
|
|
|
|
* of secure files
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $denied_response_code = 404;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set HTTP error code to use for missing secure assets
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $missing_response_code = 404;
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* Custom headers to add to all custom file responses
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $file_response_headers = array(
|
|
|
|
'Cache-Control' => 'private'
|
|
|
|
);
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
/**
|
|
|
|
* Assign new flysystem backend
|
|
|
|
*
|
|
|
|
* @param Filesystem $filesystem
|
|
|
|
* @return $this
|
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
public function setPublicFilesystem(Filesystem $filesystem) {
|
|
|
|
if(!$filesystem->getAdapter() instanceof PublicAdapter) {
|
2016-03-01 06:30:52 +01:00
|
|
|
throw new InvalidArgumentException("Configured adapter must implement PublicAdapter");
|
2015-12-09 22:19:23 +01:00
|
|
|
}
|
|
|
|
$this->publicFilesystem = $filesystem;
|
2015-09-03 07:46:08 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the currently assigned flysystem backend
|
|
|
|
*
|
|
|
|
* @return Filesystem
|
2016-03-01 06:30:52 +01:00
|
|
|
* @throws LogicException
|
2015-09-03 07:46:08 +02:00
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
public function getPublicFilesystem() {
|
2016-03-01 06:30:52 +01:00
|
|
|
if(!$this->publicFilesystem) {
|
|
|
|
throw new LogicException("Filesystem misconfiguration error");
|
|
|
|
}
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this->publicFilesystem;
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* Assign filesystem to use for non-public files
|
|
|
|
*
|
|
|
|
* @param Filesystem $filesystem
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setProtectedFilesystem(Filesystem $filesystem) {
|
|
|
|
if(!$filesystem->getAdapter() instanceof ProtectedAdapter) {
|
2016-03-01 06:30:52 +01:00
|
|
|
throw new InvalidArgumentException("Configured adapter must implement ProtectedAdapter");
|
2015-12-09 22:19:23 +01:00
|
|
|
}
|
|
|
|
$this->protectedFilesystem = $filesystem;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get filesystem to use for non-public files
|
|
|
|
*
|
|
|
|
* @return Filesystem
|
2016-08-19 00:51:35 +02:00
|
|
|
* @throws Exception
|
2015-12-09 22:19:23 +01:00
|
|
|
*/
|
|
|
|
public function getProtectedFilesystem() {
|
2016-03-01 06:30:52 +01:00
|
|
|
if(!$this->protectedFilesystem) {
|
|
|
|
throw new Exception("Filesystem misconfiguration error");
|
|
|
|
}
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this->protectedFilesystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the store that contains the given fileID
|
|
|
|
*
|
|
|
|
* @param string $fileID Internal file identifier
|
|
|
|
* @return Filesystem
|
|
|
|
*/
|
|
|
|
protected function getFilesystemFor($fileID) {
|
|
|
|
if($this->getPublicFilesystem()->has($fileID)) {
|
|
|
|
return $this->getPublicFilesystem();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->getProtectedFilesystem()->has($fileID)) {
|
|
|
|
return $this->getProtectedFilesystem();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCapabilities() {
|
|
|
|
return array(
|
|
|
|
'visibility' => array(
|
|
|
|
self::VISIBILITY_PUBLIC,
|
|
|
|
self::VISIBILITY_PROTECTED
|
|
|
|
),
|
|
|
|
'conflict' => array(
|
|
|
|
self::CONFLICT_EXCEPTION,
|
|
|
|
self::CONFLICT_OVERWRITE,
|
|
|
|
self::CONFLICT_RENAME,
|
|
|
|
self::CONFLICT_USE_EXISTING
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVisibility($filename, $hash) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash);
|
|
|
|
if($this->getPublicFilesystem()->has($fileID)) {
|
|
|
|
return self::VISIBILITY_PUBLIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->getProtectedFilesystem()->has($fileID)) {
|
|
|
|
return self::VISIBILITY_PROTECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getAsStream($filename, $hash, $variant = null) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash, $variant);
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this
|
|
|
|
->getFilesystemFor($fileID)
|
|
|
|
->readStream($fileID);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getAsString($filename, $hash, $variant = null) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash, $variant);
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this
|
|
|
|
->getFilesystemFor($fileID)
|
|
|
|
->read($fileID);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function getAsURL($filename, $hash, $variant = null, $grant = true) {
|
|
|
|
if($grant) {
|
|
|
|
$this->grant($filename, $hash);
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
$fileID = $this->getFileID($filename, $hash, $variant);
|
2015-12-09 22:19:23 +01:00
|
|
|
|
|
|
|
// Check with filesystem this asset exists in
|
|
|
|
$public = $this->getPublicFilesystem();
|
|
|
|
$protected = $this->getProtectedFilesystem();
|
|
|
|
if($public->has($fileID) || !$protected->has($fileID)) {
|
|
|
|
/** @var PublicAdapter $publicAdapter */
|
|
|
|
$publicAdapter = $public->getAdapter();
|
|
|
|
return $publicAdapter->getPublicUrl($fileID);
|
|
|
|
} else {
|
|
|
|
/** @var ProtectedAdapter $protectedAdapter */
|
|
|
|
$protectedAdapter = $protected->getAdapter();
|
|
|
|
return $protectedAdapter->getProtectedUrl($fileID);
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) {
|
2015-09-03 07:46:08 +02:00
|
|
|
// Validate this file exists
|
|
|
|
if(!file_exists($path)) {
|
|
|
|
throw new InvalidArgumentException("$path does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get filename to save to
|
|
|
|
if(empty($filename)) {
|
|
|
|
$filename = basename($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for saving content
|
2015-12-09 22:19:23 +01:00
|
|
|
$callback = function(Filesystem $filesystem, $fileID) use ($path) {
|
2015-09-03 07:46:08 +02:00
|
|
|
// Read contents as string into flysystem
|
|
|
|
$handle = fopen($path, 'r');
|
|
|
|
if($handle === false) {
|
|
|
|
throw new InvalidArgumentException("$path could not be opened for reading");
|
|
|
|
}
|
|
|
|
$result = $filesystem->putStream($fileID, $handle);
|
|
|
|
fclose($handle);
|
|
|
|
return $result;
|
|
|
|
};
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// When saving original filename, generate hash
|
|
|
|
if(!$variant) {
|
|
|
|
$hash = sha1_file($path);
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
// Submit to conflict check
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this->writeWithCallback($callback, $filename, $hash, $variant, $config);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) {
|
2015-09-03 07:46:08 +02:00
|
|
|
// Callback for saving content
|
2015-12-09 22:19:23 +01:00
|
|
|
$callback = function(Filesystem $filesystem, $fileID) use ($data) {
|
2015-09-03 07:46:08 +02:00
|
|
|
return $filesystem->put($fileID, $data);
|
|
|
|
};
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// When saving original filename, generate hash
|
|
|
|
if(!$variant) {
|
|
|
|
$hash = sha1($data);
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
// Submit to conflict check
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this->writeWithCallback($callback, $filename, $hash, $variant, $config);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) {
|
2015-09-03 07:46:08 +02:00
|
|
|
// If the stream isn't rewindable, write to a temporary filename
|
|
|
|
if(!$this->isSeekableStream($stream)) {
|
|
|
|
$path = $this->getStreamAsFile($stream);
|
2015-12-09 22:19:23 +01:00
|
|
|
$result = $this->setFromLocalFile($path, $filename, $hash, $variant, $config);
|
2015-09-03 07:46:08 +02:00
|
|
|
unlink($path);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for saving content
|
2015-12-09 22:19:23 +01:00
|
|
|
$callback = function(Filesystem $filesystem, $fileID) use ($stream) {
|
2015-09-03 07:46:08 +02:00
|
|
|
return $filesystem->putStream($fileID, $stream);
|
|
|
|
};
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// When saving original filename, generate hash
|
|
|
|
if(!$variant) {
|
|
|
|
$hash = $this->getStreamSHA1($stream);
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
// Submit to conflict check
|
2015-12-09 22:19:23 +01:00
|
|
|
return $this->writeWithCallback($callback, $filename, $hash, $variant, $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($filename, $hash) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash);
|
|
|
|
$protected = $this->deleteFromFilesystem($fileID, $this->getProtectedFilesystem());
|
|
|
|
$public = $this->deleteFromFilesystem($fileID, $this->getPublicFilesystem());
|
|
|
|
return $protected || $public;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the given file (and any variants) in the given {@see Filesystem}
|
|
|
|
*
|
|
|
|
* @param string $fileID
|
|
|
|
* @param Filesystem $filesystem
|
|
|
|
* @return bool True if a file was deleted
|
|
|
|
*/
|
|
|
|
protected function deleteFromFilesystem($fileID, Filesystem $filesystem) {
|
|
|
|
$deleted = false;
|
|
|
|
foreach($this->findVariants($fileID, $filesystem) as $nextID) {
|
|
|
|
$filesystem->delete($nextID);
|
|
|
|
$deleted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate empty dirs
|
|
|
|
$this->truncateDirectory(dirname($fileID), $filesystem);
|
|
|
|
|
|
|
|
return $deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear directory if it's empty
|
|
|
|
*
|
|
|
|
* @param string $dirname Name of directory
|
|
|
|
* @param Filesystem $filesystem
|
|
|
|
*/
|
|
|
|
protected function truncateDirectory($dirname, Filesystem $filesystem) {
|
|
|
|
if ($dirname
|
2016-03-08 01:12:07 +01:00
|
|
|
&& ltrim(dirname($dirname), '.')
|
2015-12-09 22:19:23 +01:00
|
|
|
&& ! Config::inst()->get(get_class($this), 'keep_empty_dirs')
|
|
|
|
&& ! $filesystem->listContents($dirname)
|
|
|
|
) {
|
|
|
|
$filesystem->deleteDir($dirname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterable {@see Generator} of all files / variants for the given $fileID in the given $filesystem
|
|
|
|
* This includes the empty (no) variant.
|
|
|
|
*
|
|
|
|
* @param string $fileID ID of original file to compare with.
|
|
|
|
* @param Filesystem $filesystem
|
|
|
|
* @return Generator
|
|
|
|
*/
|
|
|
|
protected function findVariants($fileID, Filesystem $filesystem) {
|
2016-03-08 01:12:07 +01:00
|
|
|
$dirname = ltrim(dirname($fileID), '.');
|
|
|
|
foreach($filesystem->listContents($dirname) as $next) {
|
2015-12-09 22:19:23 +01:00
|
|
|
if($next['type'] !== 'file') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$nextID = $next['path'];
|
|
|
|
// Compare given file to target, omitting variant
|
|
|
|
if($fileID === $this->removeVariant($nextID)) {
|
|
|
|
yield $nextID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function publish($filename, $hash) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash);
|
|
|
|
$protected = $this->getProtectedFilesystem();
|
|
|
|
$public = $this->getPublicFilesystem();
|
|
|
|
$this->moveBetweenFilesystems($fileID, $protected, $public);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function protect($filename, $hash) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash);
|
|
|
|
$public = $this->getPublicFilesystem();
|
|
|
|
$protected = $this->getProtectedFilesystem();
|
|
|
|
$this->moveBetweenFilesystems($fileID, $public, $protected);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a file (and its associative variants) between filesystems
|
|
|
|
*
|
|
|
|
* @param string $fileID
|
|
|
|
* @param Filesystem $from
|
|
|
|
* @param Filesystem $to
|
|
|
|
*/
|
|
|
|
protected function moveBetweenFilesystems($fileID, Filesystem $from, Filesystem $to) {
|
|
|
|
foreach($this->findVariants($fileID, $from) as $nextID) {
|
|
|
|
// Copy via stream
|
|
|
|
$stream = $from->readStream($nextID);
|
|
|
|
$to->putStream($nextID, $stream);
|
|
|
|
fclose($stream);
|
|
|
|
$from->delete($nextID);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate empty dirs
|
|
|
|
$this->truncateDirectory(dirname($fileID), $from);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function grant($filename, $hash) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash);
|
|
|
|
$granted = Session::get(self::GRANTS_SESSION) ?: array();
|
|
|
|
$granted[$fileID] = true;
|
|
|
|
Session::set(self::GRANTS_SESSION, $granted);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function revoke($filename, $hash) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash);
|
|
|
|
$granted = Session::get(self::GRANTS_SESSION) ?: array();
|
|
|
|
unset($granted[$fileID]);
|
|
|
|
if($granted) {
|
|
|
|
Session::set(self::GRANTS_SESSION, $granted);
|
|
|
|
} else {
|
|
|
|
Session::clear(self::GRANTS_SESSION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canView($filename, $hash) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash);
|
|
|
|
if($this->getProtectedFilesystem()->has($fileID)) {
|
|
|
|
return $this->isGranted($fileID);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a grant exists for the given FileID
|
|
|
|
*
|
|
|
|
* @param string $fileID
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isGranted($fileID) {
|
|
|
|
// Since permissions are applied to the non-variant only,
|
|
|
|
// map back to the original file before checking
|
|
|
|
$originalID = $this->removeVariant($fileID);
|
2016-01-13 06:17:45 +01:00
|
|
|
$granted = Session::get(self::GRANTS_SESSION) ?: array();
|
2015-12-09 22:19:23 +01:00
|
|
|
return !empty($granted[$originalID]);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get sha1 hash from stream
|
|
|
|
*
|
|
|
|
* @param resource $stream
|
|
|
|
* @return string str1 hash
|
|
|
|
*/
|
|
|
|
protected function getStreamSHA1($stream) {
|
|
|
|
Util::rewindStream($stream);
|
|
|
|
$context = hash_init('sha1');
|
|
|
|
hash_update_stream($context, $stream);
|
|
|
|
return hash_final($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get stream as a file
|
|
|
|
*
|
|
|
|
* @param resource $stream
|
|
|
|
* @return string Filename of resulting stream content
|
2015-12-09 22:19:23 +01:00
|
|
|
* @throws Exception
|
2015-09-03 07:46:08 +02:00
|
|
|
*/
|
|
|
|
protected function getStreamAsFile($stream) {
|
|
|
|
// Get temporary file and name
|
|
|
|
$file = tempnam(sys_get_temp_dir(), 'ssflysystem');
|
|
|
|
$buffer = fopen($file, 'w');
|
2015-12-09 22:19:23 +01:00
|
|
|
if (!$buffer) {
|
|
|
|
throw new Exception("Could not create temporary file");
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
// Transfer from given stream
|
|
|
|
Util::rewindStream($stream);
|
2015-12-09 22:19:23 +01:00
|
|
|
stream_copy_to_stream($stream, $buffer);
|
|
|
|
if (! fclose($buffer)) {
|
|
|
|
throw new Exception("Could not write stream to temporary file");
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this stream is seekable
|
|
|
|
*
|
|
|
|
* @param resource $stream
|
|
|
|
* @return bool True if this stream is seekable
|
|
|
|
*/
|
|
|
|
protected function isSeekableStream($stream) {
|
|
|
|
return Util::isSeekableStream($stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invokes the conflict resolution scheme on the given content, and invokes a callback if
|
|
|
|
* the storage request is approved.
|
|
|
|
*
|
|
|
|
* @param callable $callback Will be invoked and passed a fileID if the file should be stored
|
|
|
|
* @param string $filename Name for the resulting file
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param string $hash SHA1 of the original file content
|
|
|
|
* @param string $variant Variant to write
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param array $config Write options. {@see AssetStore}
|
2015-09-03 07:46:08 +02:00
|
|
|
* @return array Tuple associative array (Filename, Hash, Variant)
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
protected function writeWithCallback($callback, $filename, $hash, $variant = null, $config = array()) {
|
2015-09-15 04:52:02 +02:00
|
|
|
// Set default conflict resolution
|
2015-12-09 22:19:23 +01:00
|
|
|
if(empty($config['conflict'])) {
|
2015-09-15 04:52:02 +02:00
|
|
|
$conflictResolution = $this->getDefaultConflictResolution($variant);
|
2015-12-09 22:19:23 +01:00
|
|
|
} else {
|
|
|
|
$conflictResolution = $config['conflict'];
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
2016-03-01 06:30:52 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Validate parameters
|
|
|
|
if($variant && $conflictResolution === AssetStore::CONFLICT_RENAME) {
|
|
|
|
// As variants must follow predictable naming rules, they should not be dynamically renamed
|
|
|
|
throw new InvalidArgumentException("Rename cannot be used when writing variants");
|
|
|
|
}
|
|
|
|
if(!$filename) {
|
|
|
|
throw new InvalidArgumentException("Filename is missing");
|
|
|
|
}
|
|
|
|
if(!$hash) {
|
|
|
|
throw new InvalidArgumentException("File hash is missing");
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
$filename = $this->cleanFilename($filename);
|
2015-09-15 04:52:02 +02:00
|
|
|
$fileID = $this->getFileID($filename, $hash, $variant);
|
2015-09-03 07:46:08 +02:00
|
|
|
|
|
|
|
// Check conflict resolution scheme
|
|
|
|
$resolvedID = $this->resolveConflicts($conflictResolution, $fileID);
|
2015-09-15 04:52:02 +02:00
|
|
|
if($resolvedID !== false) {
|
2015-12-09 22:19:23 +01:00
|
|
|
// Check if source file already exists on the filesystem
|
|
|
|
$mainID = $this->getFileID($filename, $hash);
|
|
|
|
$filesystem = $this->getFilesystemFor($mainID);
|
|
|
|
|
|
|
|
// If writing a new file use the correct visibility
|
|
|
|
if(!$filesystem) {
|
|
|
|
// Default to public store unless requesting protected store
|
|
|
|
if(isset($config['visibility']) && $config['visibility'] === self::VISIBILITY_PROTECTED) {
|
|
|
|
$filesystem = $this->getProtectedFilesystem();
|
|
|
|
} else {
|
|
|
|
$filesystem = $this->getPublicFilesystem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
// Submit and validate result
|
2015-12-09 22:19:23 +01:00
|
|
|
$result = $callback($filesystem, $resolvedID);
|
2015-09-03 07:46:08 +02:00
|
|
|
if(!$result) {
|
|
|
|
throw new Exception("Could not save {$filename}");
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
// in case conflict resolution renamed the file, return the renamed
|
|
|
|
$filename = $this->getOriginalFilename($resolvedID);
|
2016-03-01 06:30:52 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
} elseif(empty($variant)) {
|
2015-12-09 22:19:23 +01:00
|
|
|
// If deferring to the existing file, return the sha of the existing file,
|
2015-09-15 04:52:02 +02:00
|
|
|
// unless we are writing a variant (which has the same hash value as its original file)
|
|
|
|
$stream = $this
|
2015-12-09 22:19:23 +01:00
|
|
|
->getFilesystemFor($fileID)
|
2015-09-15 04:52:02 +02:00
|
|
|
->readStream($fileID);
|
|
|
|
$hash = $this->getStreamSHA1($stream);
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'Filename' => $filename,
|
2015-09-15 04:52:02 +02:00
|
|
|
'Hash' => $hash,
|
|
|
|
'Variant' => $variant
|
2015-09-03 07:46:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
/**
|
|
|
|
* Choose a default conflict resolution
|
|
|
|
*
|
|
|
|
* @param string $variant
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getDefaultConflictResolution($variant) {
|
|
|
|
// If using new naming scheme (segment by hash) it's normally safe to overwrite files.
|
|
|
|
// Variants are also normally safe to overwrite, since lazy-generation is implemented at a higher level.
|
|
|
|
$legacy = $this->useLegacyFilenames();
|
|
|
|
if(!$legacy || $variant) {
|
|
|
|
return AssetStore::CONFLICT_OVERWRITE;
|
|
|
|
}
|
2016-03-01 06:30:52 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Legacy behaviour is to rename
|
|
|
|
return AssetStore::CONFLICT_RENAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if legacy filenames should be used. These do not have hash path parts.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function useLegacyFilenames() {
|
|
|
|
return Config::inst()->get(get_class($this), 'legacy_filenames');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMetadata($filename, $hash, $variant = null) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash, $variant);
|
2015-12-09 22:19:23 +01:00
|
|
|
$filesystem = $this->getFilesystemFor($fileID);
|
|
|
|
if($filesystem) {
|
|
|
|
return $filesystem->getMetadata($fileID);
|
|
|
|
}
|
|
|
|
return null;
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getMimeType($filename, $hash, $variant = null) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash, $variant);
|
2015-12-09 22:19:23 +01:00
|
|
|
$filesystem = $this->getFilesystemFor($fileID);
|
|
|
|
if($filesystem) {
|
|
|
|
return $filesystem->getMimetype($fileID);
|
|
|
|
}
|
|
|
|
return null;
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function exists($filename, $hash, $variant = null) {
|
|
|
|
$fileID = $this->getFileID($filename, $hash, $variant);
|
2015-12-09 22:19:23 +01:00
|
|
|
$filesystem = $this->getFilesystemFor($fileID);
|
|
|
|
return !empty($filesystem);
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
/**
|
|
|
|
* Determine the path that should be written to, given the conflict resolution scheme
|
2016-03-01 06:30:52 +01:00
|
|
|
*
|
2015-09-03 07:46:08 +02:00
|
|
|
* @param string $conflictResolution
|
|
|
|
* @param string $fileID
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return string|false Safe filename to write to. If false, then don't write, and use existing file.
|
2015-09-03 07:46:08 +02:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function resolveConflicts($conflictResolution, $fileID) {
|
|
|
|
// If overwrite is requested, simply put
|
|
|
|
if($conflictResolution === AssetStore::CONFLICT_OVERWRITE) {
|
|
|
|
return $fileID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, check if this exists
|
2015-12-09 22:19:23 +01:00
|
|
|
$exists = $this->getFilesystemFor($fileID);
|
2015-09-03 07:46:08 +02:00
|
|
|
if(!$exists) {
|
|
|
|
return $fileID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flysystem defaults to use_existing
|
|
|
|
switch($conflictResolution) {
|
|
|
|
// Throw tantrum
|
2015-12-09 22:19:23 +01:00
|
|
|
case static::CONFLICT_EXCEPTION: {
|
2016-03-01 06:30:52 +01:00
|
|
|
throw new InvalidArgumentException("File already exists at path {$fileID}");
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename
|
2015-12-09 22:19:23 +01:00
|
|
|
case static::CONFLICT_RENAME: {
|
2015-09-03 07:46:08 +02:00
|
|
|
foreach($this->fileGeneratorFor($fileID) as $candidate) {
|
2015-12-09 22:19:23 +01:00
|
|
|
if(!$this->getFilesystemFor($candidate)) {
|
2015-09-03 07:46:08 +02:00
|
|
|
return $candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 06:30:52 +01:00
|
|
|
throw new InvalidArgumentException("File could not be renamed with path {$fileID}");
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Use existing file
|
2015-12-09 22:19:23 +01:00
|
|
|
case static::CONFLICT_USE_EXISTING:
|
2015-09-03 07:46:08 +02:00
|
|
|
default: {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an asset renamer for the given filename.
|
|
|
|
*
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param string $fileID Adapter specific identifier for this file/version
|
2015-09-03 07:46:08 +02:00
|
|
|
* @return AssetNameGenerator
|
|
|
|
*/
|
|
|
|
protected function fileGeneratorFor($fileID){
|
|
|
|
return Injector::inst()->createWithArgs('AssetNameGenerator', array($fileID));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs filename cleanup before sending it back.
|
|
|
|
*
|
|
|
|
* This name should not contain hash or variants.
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function cleanFilename($filename) {
|
|
|
|
// Since we use double underscore to delimit variants, eradicate them from filename
|
|
|
|
return preg_replace('/_{2,}/', '_', $filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-09 22:19:23 +01:00
|
|
|
* Given a FileID, map this back to the original filename, trimming variant and hash
|
2015-09-03 07:46:08 +02:00
|
|
|
*
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param string $fileID Adapter specific identifier for this file/version
|
|
|
|
* @return string Filename for this file, omitting hash and variant
|
2015-09-03 07:46:08 +02:00
|
|
|
*/
|
2015-12-09 22:19:23 +01:00
|
|
|
protected function getOriginalFilename($fileID) {
|
2015-09-03 07:46:08 +02:00
|
|
|
// Remove variant
|
2015-12-09 22:19:23 +01:00
|
|
|
$originalID = $this->removeVariant($fileID);
|
2015-09-03 07:46:08 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Remove hash (unless using legacy filenames, without hash)
|
|
|
|
if($this->useLegacyFilenames()) {
|
2015-12-09 22:19:23 +01:00
|
|
|
return $originalID;
|
2015-09-15 04:52:02 +02:00
|
|
|
} else {
|
|
|
|
return preg_replace(
|
|
|
|
'/(?<hash>[a-zA-Z0-9]{10}\\/)(?<name>[^\\/]+)$/',
|
|
|
|
'$2',
|
2015-12-09 22:19:23 +01:00
|
|
|
$originalID
|
2015-09-15 04:52:02 +02:00
|
|
|
);
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* Remove variant from a fileID
|
|
|
|
*
|
|
|
|
* @param string $fileID
|
|
|
|
* @return string FileID without variant
|
|
|
|
*/
|
|
|
|
protected function removeVariant($fileID) {
|
|
|
|
// Check variant
|
|
|
|
if (preg_match('/^(?<before>((?<!__).)+)__(?<variant>[^\\.]+)(?<after>.*)$/', $fileID, $matches)) {
|
|
|
|
return $matches['before'] . $matches['after'];
|
|
|
|
}
|
|
|
|
// There is no variant, so return original value
|
|
|
|
return $fileID;
|
|
|
|
}
|
|
|
|
|
2015-09-03 07:46:08 +02:00
|
|
|
/**
|
|
|
|
* Map file tuple (hash, name, variant) to a filename to be used by flysystem
|
|
|
|
*
|
|
|
|
* The resulting file will look something like my/directory/EA775CB4D4/filename__variant.jpg
|
|
|
|
*
|
|
|
|
* @param string $filename Name of file
|
2015-09-15 04:52:02 +02:00
|
|
|
* @param string $hash Hash of original file
|
2015-09-03 07:46:08 +02:00
|
|
|
* @param string $variant (if given)
|
2015-12-09 22:19:23 +01:00
|
|
|
* @return string Adapter specific identifier for this file/version
|
2015-09-03 07:46:08 +02:00
|
|
|
*/
|
2015-09-15 04:52:02 +02:00
|
|
|
protected function getFileID($filename, $hash, $variant = null) {
|
2015-09-03 07:46:08 +02:00
|
|
|
// Since we use double underscore to delimit variants, eradicate them from filename
|
|
|
|
$filename = $this->cleanFilename($filename);
|
|
|
|
$name = basename($filename);
|
|
|
|
|
|
|
|
// Split extension
|
|
|
|
$extension = null;
|
|
|
|
if(($pos = strpos($name, '.')) !== false) {
|
|
|
|
$extension = substr($name, $pos);
|
|
|
|
$name = substr($name, 0, $pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unless in legacy mode, inject hash just prior to the filename
|
2015-12-09 22:19:23 +01:00
|
|
|
if($this->useLegacyFilenames()) {
|
2015-09-03 07:46:08 +02:00
|
|
|
$fileID = $name;
|
|
|
|
} else {
|
|
|
|
$fileID = substr($hash, 0, 10) . '/' . $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add directory
|
|
|
|
$dirname = ltrim(dirname($filename), '.');
|
|
|
|
if($dirname) {
|
|
|
|
$fileID = $dirname . '/' . $fileID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add variant
|
|
|
|
if($variant) {
|
|
|
|
$fileID .= '__' . $variant;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add extension
|
|
|
|
if($extension) {
|
|
|
|
$fileID .= $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fileID;
|
|
|
|
}
|
|
|
|
|
2015-12-09 22:19:23 +01:00
|
|
|
/**
|
|
|
|
* Ensure each adapter re-generates its own server configuration files
|
|
|
|
*/
|
|
|
|
public static function flush() {
|
|
|
|
// Ensure that this instance is constructed on flush, thus forcing
|
|
|
|
// bootstrapping of necessary .htaccess / web.config files
|
|
|
|
$instance = singleton('AssetStore');
|
|
|
|
if ($instance instanceof FlysystemAssetStore) {
|
|
|
|
$publicAdapter = $instance->getPublicFilesystem()->getAdapter();
|
|
|
|
if($publicAdapter instanceof AssetAdapter) {
|
|
|
|
$publicAdapter->flush();
|
|
|
|
}
|
|
|
|
$protectedAdapter = $instance->getProtectedFilesystem()->getAdapter();
|
|
|
|
if($protectedAdapter instanceof AssetAdapter) {
|
|
|
|
$protectedAdapter->flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResponseFor($asset) {
|
|
|
|
// Check if file exists
|
|
|
|
$filesystem = $this->getFilesystemFor($asset);
|
|
|
|
if(!$filesystem) {
|
2016-01-13 06:17:45 +01:00
|
|
|
return $this->createMissingResponse();
|
2015-12-09 22:19:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Block directory access
|
|
|
|
if($filesystem->get($asset) instanceof Directory) {
|
|
|
|
return $this->createDeniedResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deny if file is protected and denied
|
|
|
|
if($filesystem === $this->getProtectedFilesystem() && !$this->isGranted($asset)) {
|
|
|
|
return $this->createDeniedResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve up file response
|
|
|
|
return $this->createResponseFor($filesystem, $asset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an {@see SS_HTTPResponse} for the given file from the source filesystem
|
2016-01-13 06:17:45 +01:00
|
|
|
* @param FilesystemInterface $flysystem
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param string $fileID
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
2016-01-13 06:17:45 +01:00
|
|
|
protected function createResponseFor(FilesystemInterface $flysystem, $fileID) {
|
2015-12-09 22:19:23 +01:00
|
|
|
// Build response body
|
|
|
|
// @todo: gzip / buffer response?
|
|
|
|
$body = $flysystem->read($fileID);
|
|
|
|
$mime = $flysystem->getMimetype($fileID);
|
|
|
|
$response = new SS_HTTPResponse($body, 200);
|
|
|
|
|
|
|
|
// Add headers
|
|
|
|
$response->addHeader('Content-Type', $mime);
|
|
|
|
$headers = Config::inst()->get(get_class($this), 'file_response_headers');
|
|
|
|
foreach($headers as $header => $value) {
|
|
|
|
$response->addHeader($header, $value);
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-13 06:17:45 +01:00
|
|
|
* Generate a response for requests to a denied protected file
|
2015-12-09 22:19:23 +01:00
|
|
|
*
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
|
|
|
protected function createDeniedResponse() {
|
2016-01-13 06:17:45 +01:00
|
|
|
$code = (int)Config::inst()->get(get_class($this), 'denied_response_code');
|
|
|
|
return $this->createErrorResponse($code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a response for missing file requests
|
|
|
|
*
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
|
|
|
protected function createMissingResponse() {
|
|
|
|
$code = (int)Config::inst()->get(get_class($this), 'missing_response_code');
|
|
|
|
return $this->createErrorResponse($code);
|
2015-12-09 22:19:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-13 06:17:45 +01:00
|
|
|
* Create a response with the given error code
|
2015-12-09 22:19:23 +01:00
|
|
|
*
|
2016-01-13 06:17:45 +01:00
|
|
|
* @param int $code
|
2015-12-09 22:19:23 +01:00
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
2016-01-13 06:17:45 +01:00
|
|
|
protected function createErrorResponse($code) {
|
|
|
|
$response = new SS_HTTPResponse('', $code);
|
2015-12-09 22:19:23 +01:00
|
|
|
|
|
|
|
// Show message in dev
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!Director::isLive()) {
|
2015-12-09 22:19:23 +01:00
|
|
|
$response->setBody($response->getStatusDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2015-09-03 07:46:08 +02:00
|
|
|
}
|