Commit 097f930f authored by Thorsten Buss's avatar Thorsten Buss

inital commit

parent 8ff011b6
vendor/
phpunit.xml
Copyright (c) 2010 Thorsten Buss
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
This file was created by JetBrains PhpStorm 6.0.1 for binding GitHub repository
\ No newline at end of file
BNRepo - Data Repositories with different BackendAdapters (Local, FTP, SFTP, S3...)
=========
BNRepo is a PHP5 library based on [Gaufrette](https://github.com/KnpLabs/Gaufrette), that provides different Repositories for filesystem abstraction.
{
"name": "karl007/BNRepos",
"type": "library",
"description": "PHP5 library that provides Repositories as filesystem abstraction layer",
"keywords": ["file", "repository", "filesystem", "media", "abstraction"],
"license": "MIT",
"require": {
"php": ">=5.3.2",
"aws/aws-sdk-php": "2.2.0",
"symfony/yaml": "2.2",
"herzult/php-ssh": "@dev",
"knplabs/gaufrette": "v0.1.4",
"amazonwebservices/aws-sdk-for-php": "*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"suggest": {
"amazonwebservices/aws-sdk-for-php": "to use the Amazon S3 adapter With the OLD API",
"ext-curl": "*",
"ext-mbstring": "*",
"ext-fileinfo": "*"
},
"autoload": {
"psr-0": { "BNRepo": "src/" }
}
}
This diff is collapsed.
bnrepo-test-local:
type: local
dir: /tmp/bnrepo-test
create: true
bnrepo-test-s3:
type: s3
aws_key: AWS_KEY
aws_secret: AWS_SECRET
bucket: bnrepo-test
dir: bnrepo-test
create: true
use_old_version: false
options:
default_acl: public-read #DEFAULT is only BucketOwner can read, so everyone with the link can read
bnrepo-test-ftp:
type: ftp
host: HOST
username: USERNAME
password: PASSWORD
dir: /bnrepo-test
passive: false
bnrepo-test-sftp:
type: sftp
host: HOST
port: PORT
dir: /bnrepo-test
username: USERNAME
password: PASSWORD
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php.dist"
>
<testsuites>
<testsuite name="BNRepo Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
<?php
namespace BNRepo\Repository\Adapter;
use Gaufrette\Adapter\AmazonS3;
use Gaufrette\Exception\FileNotFound;
use Gaufrette\Exception\UnexpectedFile;
class AdapterAmazonS3 extends AmazonS3 implements AdapterUploadable, AdapterDownloadable, AdapterLinkable {
public function __construct(\AmazonS3 $service, $bucket, $options = array()) {
if (isset($options['directory'])) { // strip double slashes and secure that the dir not start and ends with an slash
$options['directory'] = substr(preg_replace('~/+~', '/', '/'. $options['directory'].'/'), 1, -1);
}
if (!isset($options['default_acl']))
$options['default_acl'] = \AmazonS3::ACL_OWNER_FULL_CONTROL;
parent::__construct($service, $bucket, $options);
}
/**
* Uploads a Local file
*
* @param string $localFile
* @param string $targetKey
*
* @return boolean TRUE if the rename was successful
* @throws FileNotFound when sourceKey does not exist
* @throws UnexpectedFile when targetKey exists
* @throws \RuntimeException when cannot rename
*/
public function upload($localFile, $targetKey) {
return $this->write($targetKey, file_get_contents($localFile));
}
/**
* Downloads a file to Local
*
* @param string $sourceKey
* @param string $localTargetFile
*
* @return boolean TRUE if the rename was successful
* @throws FileNotFound when sourceKey does not exist
* @throws UnexpectedFile when targetKey exists
* @throws \RuntimeException when cannot rename
*/
public function download($sourceKey, $localTargetFile) {
return file_put_contents($localTargetFile, $this->read($sourceKey));
}
/**
* Retrieve the S3 object URL for the given key.
*
* @param string $key
* @param integer|string $preauth Look at \AmazonS3::get_object_url() docs
* @param array $opt Look at \AmazonS3::get_object_url() docs
*
* @see \AmazonS3::get_object_url()
*
* @return string The S3 object URL
*/
public function getUrl($key, $validTime = 0, $options = array()) {
if (is_numeric($validTime))
$validTime = '+' . $validTime . ' seconds';
return $this->service->get_object_url(
$this->bucket,
$this->computePath($key),
$validTime,
$options
);
}
/**
* OVERWRITE CAUSE PRIVATE FLAG
*
* Computes the path for the specified key taking the bucket in account
*
* @param string $key The key for which to compute the path
*
* @return string
*/
protected function computePath($key) {
$directory = $this->getDirectory();
if (null === $directory || '' === $directory) {
return substr(preg_replace('~/+~', '/', '/' .$key . '/'), 1, -1);
}
return substr(preg_replace('~/+~', '/', '/' . $directory.'/'. $key . '/'), 1, -1);
}
/**
* OVERWRITE CAUSE PRIVATE FLAG
* Ensures the specified bucket exists. If the bucket does not exists
* and the create parameter is set to true, it will try to create the
* bucket
*
* @throws \RuntimeException if the bucket does not exists or could not be
* created
*/
protected function ensureBucketExists() {
if ($this->ensureBucket) {
return;
}
if (isset($this->options['region'])) {
$this->service->set_region($this->options['region']);
}
if ($this->service->if_bucket_exists($this->bucket)) {
$this->ensureBucket = true;
return;
}
if (!$this->options['create']) {
throw new \RuntimeException(sprintf(
'The configured bucket "%s" does not exist.',
$this->bucket
));
}
$response = $this->service->create_bucket(
$this->bucket,
$this->options['region']
);
if (!$response->isOK()) {
throw new \RuntimeException(sprintf(
'Failed to create the configured bucket "%s".',
$this->bucket
));
}
$this->ensureBucket = true;
}
/**
* Add Directory-Prefix and remove fullPath and emptyDirLine from output for similar response to local/ftp etc
* {@inheritDoc}
*/
public function keys() {
$this->ensureBucketExists();
$list = $this->service->get_object_list($this->bucket, array(
'prefix' => $this->getDirectory()
));
$keys = array();
$dirLength = strlen($this->getDirectory())+1; //+1 to remove the starting slash
foreach ($list as $file) {
if (strlen(dirname($file)) > $dirLength)
$keys[] = substr(dirname($file), $dirLength);
elseif (strlen($file) > $dirLength)
$keys[] = substr($file, $dirLength);
}
sort($keys);
return $keys;
}
/**
* OVERWRITE CAUSE NO CHANGABLE PUBLIC_ACL
* {@inheritDoc}
*/
public function write($key, $content) {
$this->ensureBucketExists();
$opt = array_replace_recursive(
array('acl' => $this->options['default_acl']),
$this->getMetadata($key),
array('body' => $content)
);
$response = $this->service->create_object(
$this->bucket,
$this->computePath($key),
$opt
);
if (!$response->isOK()) {
return false;
}
;
return intval($response->header["x-aws-requestheaders"]["Content-Length"]);
}
/**
* {@inheritDoc}
*/
public function isDirectory($key) {
// Check is not good, but the only solution
// If the $key is from the keys() function and not exists, its an Directory
return !$this->service->if_object_exists(
$this->bucket,
$this->computePath($key)
);
}
}
<?php
namespace BNRepo\Repository\Adapter;
use Aws\CloudFront\Exception\Exception;
use Aws\Common\Enum\Region;
use Aws\S3\Enum\CannedAcl;
use Aws\S3\S3Client;
use Gaufrette\Adapter\AmazonS3;
use Gaufrette\Exception\FileNotFound;
use Gaufrette\Exception\UnexpectedFile;
use Gaufrette\Adapter;
use Guzzle\Service\Resource\Model;
class AdapterAmazonS3Ver2 extends AmazonS3 implements AdapterUploadable, AdapterDownloadable, AdapterLinkable, Adapter {
protected $service;
protected $bucket;
protected $ensureBucket = false;
protected $metadata;
protected $options;
public function __construct(S3Client $service, $bucket, $options = array()) {
$this->service = $service;
$this->bucket = $bucket;
$this->options = array_replace_recursive(
array('directory' => '', 'create' => false, 'region' => Region::EU_WEST_1, 'default_acl' => CannedAcl::BUCKET_OWNER_FULL_CONTROL),
$options
);
// Set Directory Explicit, cause validation/correction rules
$this->setDirectory($options['directory']);
}
/**
* Uploads a Local file
*
* @param string $localFile
* @param string $targetKey
*
* @return boolean TRUE if the rename was successful
* @throws FileNotFound when sourceKey does not exist
* @throws UnexpectedFile when targetKey exists
* @throws \RuntimeException when cannot rename
*/
public function upload($localFile, $targetKey) {
return $this->write($targetKey, fopen($localFile, 'r'));
}
/**
* Downloads a file to Local
*
* @param string $sourceKey
* @param string $localTargetFile
*
* @return boolean TRUE if the rename was successful
* @throws FileNotFound when sourceKey does not exist
* @throws UnexpectedFile when targetKey exists
* @throws \RuntimeException when cannot rename
*/
public function download($sourceKey, $localTargetFile) {
try {
$this->getObject($sourceKey, array(
'SaveAs' => $localTargetFile
));
} catch (\Exception $e) {
return false;
}
return true;
}
/**
* Retrieve the S3 object URL for the given key.
*
* @param string $key
* @param integer|string $preauth Look at \AmazonS3::get_object_url() docs
* @param array $opt Look at \AmazonS3::get_object_url() docs
*
* @see \AmazonS3::get_object_url()
*
* @return string The S3 object URL
*/
public function getUrl($key, $validTime = 0, $options = array()) {
$url = "{$this->bucket}/{$this->computePath($key)}";
if (array_key_exists('filename', $options))
$url .= '?response-content-disposition='.urlencode("attachment; filename=\"{$options['filename']}\"");
$request = $this->service->get($url);
if (is_numeric($validTime))
$validTime = '+'.$validTime.' seconds';
return $this->service->createPresignedUrl($request, $validTime);
}
/**
* Computes the path for the specified key taking the bucket in account
*
* @param string $key The key for which to compute the path
*
* @return string
*/
protected function computePath($key) {
$directory = $this->getDirectory();
if (null === $directory || '' === $directory) {
return substr(preg_replace('~/+~', '/', '/' .$key . '/'), 1, -1);
}
return substr(preg_replace('~/+~', '/', '/' . $directory.'/'. $key . '/'), 1, -1);
}
/**
* OVERWRITE CAUSE PRIVATE FLAG
* Ensures the specified bucket exists. If the bucket does not exists
* and the create parameter is set to true, it will try to create the
* bucket
*
* @throws \RuntimeException if the bucket does not exists or could not be
* created
*/
protected function ensureBucketExists() {
if ($this->ensureBucket) {
return;
}
if (isset($this->options['region'])) {
$this->service->setRegion($this->options['region']);
}
if ($this->service->doesBucketExist($this->bucket)) {
$this->ensureBucket = true;
return;
}
if (!$this->options['create']) {
throw new \RuntimeException(sprintf(
'The configured bucket "%s" does not exist.',
$this->bucket
));
}
try {
/** @var $result Model */
$result = $this->service->createBucket(array(
'Bucket' => $this->bucket,
'ACL' => $this->options['default_acl'],
'LocationConstraint' => $this->options['region']
));
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
'Failed to create the configured bucket "%s".',
$this->bucket
), 0, $e);
}
$this->ensureBucket = true;
}
/**
* Add Directory-Prefix and remove fullPath and emptyDirLine from output for similar response to local/ftp etc
* {@inheritDoc}
*/
public function keys() {
$this->ensureBucketExists();
/** @var $list Model */
$list = $this->service->getIterator('ListObjects', array(
'Bucket' => $this->bucket,
'Prefix' => $this->getDirectory()
));
$keys = array();
$dirLength = strlen($this->getDirectory())+1; //+1 to remove the starting slash
foreach ($list as $file) {
$key = $file['Key'];
if (strlen(dirname($key)) > $dirLength)
$keys[] = substr(dirname($key), $dirLength);
elseif (strlen($key) > $dirLength)
$keys[] = substr($key, $dirLength);
}
sort($keys);
return $keys;
}
/**
* OVERWRITE CAUSE NO CHANGABLE PUBLIC_ACL
* {@inheritDoc}
*/
public function write($key, $content) {
$this->ensureBucketExists();
$options = array_replace_recursive(
array('acl' => $this->options['default_acl']),
$this->getMetadata($key),
array(
'Body' => $content,
'Bucket' => $this->bucket,
'Key' => $this->computePath($key),
)
);
try {
/** @var $response Model */
$response = $this->service->putObject($options);
} catch (Exception $e) {
return false;
}
if (is_string($content))
return mb_strlen($content);
return true;
}
// *********************
/**
* Set the base directory the user will have access to
*
* @param string $directory
*/
public function setDirectory($directory) {
// strip double slashes and secure that the dir not start and ends with an slash
$this->options['directory'] = substr(preg_replace('~/+~', '/', '/' . $directory . '/'), 1, -1);
}
/**
* Get the directory the user has access to
*
* @return string
*/
public function getDirectory() {
return $this->options['directory'];
}
/**
* {@inheritDoc}
*/
public function read($key) {
$this->ensureBucketExists();
try {
/** @var $response Model */
$response = $this->getObject($key);
} catch (\Exception $e) {
return false;
}
return $response['Body'];
}
/**
* {@inheritDoc}
*/
public function rename($sourceKey, $targetKey) {
$this->ensureBucketExists();
try {
/** @var $response Model */
$response = $this->service->copyObject(array(
'Bucket' => $this->bucket,
'Key' => $this->computePath($targetKey),
'CopySource' => urlencode($this->bucket.'/'.$this->computePath($sourceKey)),
));
} catch (\Exception $e) {
return false;
}
$this->delete($sourceKey);
return true;
}
/**
* {@inheritDoc}
*/
public function exists($key) {
$this->ensureBucketExists();
return $this->service->doesObjectExist(
$this->bucket,
$this->computePath($key)
);
}
/**
* {@inheritDoc}
*/
public function mtime($key) {
$this->ensureBucketExists();
return strtotime($this->getObject($key)['LastModified']);
}
/**
* {@inheritDoc}
*/
public function delete($key) {
$this->ensureBucketExists();
try {
/** @var $response Model */
$response = $this->service->deleteObject(array(
'Bucket' => $this->bucket,
'Key' => $this->computePath($key)
));
} catch (\Exception $e) {
return false;
}
return true;
}
/**
* {@inheritDoc}
*/
public function isDirectory($key) {
// Check is not good, but the only solution
// If the $key is from the keys() function and not exists, its an Directory
return !$this->service->doesObjectExist(
$this->bucket,
$this->computePath($key)
);
}
/**
* @param $key
* @param array $options
* @return Model
*/
public function getObject($key, $options=array()) {
$options = array_merge($options, array(
'Bucket' => $this->bucket,
'Key' => $this->computePath($key)
));
return $this->service->getObject($options);
}
}
<?php
/**
* User: thorsten
* Date: 15.04.13
* Time: 13:19
*/
namespace BNRepo\Repository\Adapter;
use Gaufrette\Exception\FileNotFound;
use Gaufrette\Exception\UnexpectedFile;
interface AdapterDownloadable {
/**
* Downloads a file to Local
*
* @param string $sourceKey
* @param string $localTargetFile
*
* @return boolean TRUE if the rename was successful
* @throws FileNotFound when sourceKey does not exist
* @throws UnexpectedFile when targetKey exists
* @throws \RuntimeException when cannot rename
*/
public function download($sourceKey, $localTargetFile);
}
<?php
namespace BNRepo\Repository\Adapter;
use Gaufrette\Adapter\Ftp;
use Gaufrette\Exception\FileNotFound;
use Gaufrette\Exception\UnexpectedFile;
class AdapterFtp extends Ftp implements AdapterUploadable, AdapterDownloadable {
public function __construct(