Exemple #1
0
 /**
  * Returns data array of stream variables
  *
  * @return array
  */
 public function stream_stat()
 {
     if (!$this->_objectName) {
         return false;
     }
     $stat = array();
     $stat['dev'] = 0;
     $stat['ino'] = 0;
     $stat['mode'] = 0777;
     $stat['nlink'] = 0;
     $stat['uid'] = 0;
     $stat['gid'] = 0;
     $stat['rdev'] = 0;
     $stat['size'] = 0;
     $stat['atime'] = 0;
     $stat['mtime'] = 0;
     $stat['ctime'] = 0;
     $stat['blksize'] = 0;
     $stat['blocks'] = 0;
     if (($slash = strchr($this->_objectName, '/')) === false || $slash == strlen($this->_objectName) - 1) {
         /* bucket */
         $stat['mode'] |= 040000;
     } else {
         $stat['mode'] |= 0100000;
     }
     $info = $this->_s3->getInfo($this->_objectName);
     if (!empty($info)) {
         $stat['size'] = $info['size'];
         $stat['atime'] = time();
         $stat['mtime'] = $info['mtime'];
     }
     return $stat;
 }
Exemple #2
0
 public function testBadNames()
 {
     try {
         $this->_amazon->createBucket("This is a Very Bad Name");
         $this->fail("Expected exception not thrown");
     } catch (Zend_Service_Amazon_S3_Exception $e) {
         $this->assertContains("contains invalid characters", $e->getMessage());
     }
     try {
         $this->_amazon->isBucketAvailable("This is a Very Bad Name");
         $this->fail("Expected exception not thrown");
     } catch (Zend_Uri_Exception $e) {
         $this->assertContains("not a valid HTTP host", $e->getMessage());
     }
     try {
         $this->_amazon->putObject("This is a Very Bad Name/And It Gets Worse", "testdata");
         $this->fail("Expected exception not thrown");
     } catch (Zend_Service_Amazon_S3_Exception $e) {
         $this->assertContains("contains invalid characters", $e->getMessage());
     }
     try {
         $this->_amazon->getObject("This is a Very Bad Name/And It Gets Worse");
         $this->fail("Expected exception not thrown");
     } catch (Zend_Service_Amazon_S3_Exception $e) {
         $this->assertContains("contains invalid characters", $e->getMessage());
     }
     try {
         $this->_amazon->getInfo("This is a Very Bad Name/And It Gets Worse");
         $this->fail("Expected exception not thrown");
     } catch (Zend_Service_Amazon_S3_Exception $e) {
         $this->assertContains("contains invalid characters", $e->getMessage());
     }
 }
 /**
  * Returns data array of stream variables
  *
  * @return array
  */
 public function stream_stat()
 {
     if (!$this->_objectName) {
         return false;
     }
     $stat = array();
     $stat['dev'] = 0;
     $stat['ino'] = 0;
     $stat['mode'] = 0;
     $stat['nlink'] = 0;
     $stat['uid'] = 0;
     $stat['gid'] = 0;
     $stat['rdev'] = 0;
     $stat['size'] = 0;
     $stat['atime'] = 0;
     $stat['mtime'] = 0;
     $stat['ctime'] = 0;
     $stat['blksize'] = 0;
     $stat['blocks'] = 0;
     $info = $this->_s3->getInfo($this->_objectName);
     if (!empty($info)) {
         $stat['size'] = $info['size'];
         $stat['atime'] = time();
         $stat['mtime'] = $info['mtime'];
     }
     return $stat;
 }
Exemple #4
0
 public function testGetInfoWithBadName()
 {
     $this->setExpectedException(
         'Zend\Service\Amazon\Sqs\Exception\InvalidArgumentException',
         'Bucket name "This is a Very Bad Name" contains invalid characters');
     $this->_amazon->getInfo("This is a Very Bad Name/And It Gets Worse");
 }
Exemple #5
0
 /**
  * Get a key/value array of metadata for the given path.
  *
  * @param  string $path
  * @param  array $options
  * @return array
  */
 public function fetchMetadata($path, $options = array())
 {
     try {
         return $this->_s3->getInfo($this->_getFullPath($path, $options));
     } catch (Zend_Service_Amazon_S3_Exception $e) {
         throw new Zend_Cloud_StorageService_Exception('Error on fetch: ' . $e->getMessage(), $e->getCode(), $e);
     }
 }
Exemple #6
0
 /**
  * Returns last modified time of file, as a Unix timestamp.
  *
  * @param string $filename
  * @return string
  */
 public function getTimestamp($filename)
 {
     $this->_initApi();
     $info = $this->_api->getInfo($this->_config['bucket'] . $this->_getUri($filename));
     if (array_key_exists('mtime', $info)) {
         return $info['mtime'];
     }
     throw new Exception("Could not retrieve timestamp of {$filename}.");
 }
Exemple #7
0
 public function testBadNames()
 {
     try {
         $this->_amazon->createBucket("This is a Very Bad Name");
         $this->fail("Expected exception not thrown");
     } catch (S3\Exception $e) {
         $this->assertContains("contains invalid characters", $e->getMessage());
     }
     try {
         $this->_amazon->isBucketAvailable("This is a Very Bad Name");
         $this->fail("Expected exception not thrown");
     } catch (\Zend\HTTP\Client\Adapter\Exception $e) {
         $this->assertContains("Unable to Connect to", $e->getMessage());
         //@todo check why \Http\Client exception is thrown instead of \Zend\Uri\Exception
     } catch (\Zend\URI\Exception $e) {
         $this->assertContains("not a valid HTTP host", $e->getMessage());
     }
     try {
         $this->_amazon->putObject("This is a Very Bad Name/And It Gets Worse", "testdata");
         $this->fail("Expected exception not thrown");
     } catch (S3\Exception $e) {
         $this->assertContains("contains invalid characters", $e->getMessage());
     }
     try {
         $this->_amazon->getObject("This is a Very Bad Name/And It Gets Worse");
         $this->fail("Expected exception not thrown");
     } catch (S3\Exception $e) {
         $this->assertContains("contains invalid characters", $e->getMessage());
     }
     try {
         $this->_amazon->getInfo("This is a Very Bad Name/And It Gets Worse");
         $this->fail("Expected exception not thrown");
     } catch (S3\Exception $e) {
         $this->assertContains("contains invalid characters", $e->getMessage());
     }
 }
Exemple #8
0
 public function testGetInfoWithBadName()
 {
     $this->setExpectedException('ZendService\\Amazon\\S3\\Exception\\InvalidArgumentException', 'Bucket name "VERY.BAD.NAME" contains invalid characters');
     $this->_amazon->getInfo("VERY.BAD.NAME/And It Gets Worse");
 }
Exemple #9
0
 public function addFile($fileInfo, $userInfo, $privacy = false, $details = false)
 {
     $config = self::$_registry->get("config");
     $s3 = new Zend_Service_Amazon_S3($config['services']['S3']['key'], $config['services']['S3']['secret']);
     $filenameFilter = new Ml_Filter_FilenameRobot();
     $filenameValidator = new Ml_Validate_Filename();
     if (isset($details['title']) && !empty($details['title'])) {
         $title = $details['title'];
     } else {
         $title = mb_substr(trim($fileInfo['name']), 0, 100);
         /* try to use a good initial title for the file */
         $titleNameposition = mb_strrpos($title, ".");
         $titleSize = mb_strlen($title);
         if ($titleSize > 5 && $titleSize - $titleNameposition <= 5) {
             $tryTitle = mb_substr($title, 0, $titleNameposition);
             if (!empty($tryTitle) && strrpos($tryTitle, ".") < mb_strlen($tryTitle) - 4) {
                 $title = $tryTitle;
             }
         }
     }
     //get the max value of mt_getrandmax() or the max value of the unsigned int type
     $maxRand = mt_getrandmax() < 4294967295.0 ? mt_getrandmax() : 4294967295.0;
     $secret = mt_rand(0, $maxRand);
     $downloadSecret = mt_rand(0, $maxRand);
     $filename = $filenameFilter->filter($fileInfo['name']);
     if (!$filenameValidator->isValid($filename)) {
         $extension = $filenameFilter->filter(strchr($filename, '.'));
         if ($filenameValidator->isValid($extension)) {
             $filename = mt_rand() . $extension;
         } else {
             $filename = mt_rand();
         }
     }
     $this->_dbAdapter->beginTransaction();
     try {
         $this->_dbAdapter->insert("upload_history", array("byUid" => $userInfo['id'], "fileSize" => $fileInfo['size'], "filename" => $fileInfo['name']));
         $uploadId = $this->_dbAdapter->lastInsertId("upload_history");
         if (!$uploadId) {
             throw new Exception("Can not create upload ID.");
         }
         $objectKey = $userInfo['alias'] . "/" . $uploadId . "-" . $downloadSecret . "/" . $filename;
         $put = $s3->putFile($fileInfo['tmp_name'], $config['services']['S3']['sharesBucket'] . "/" . $objectKey, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ, "Content-Type" => Zend_Service_Amazon_S3::getMimeType($objectKey), 'Content-Disposition' => 'attachment;', "x-amz-meta-id" => $uploadId, "x-amz-meta-uid" => $userInfo['id'], "x-amz-meta-username" => $userInfo['alias']));
         if (!$put) {
             throw new Exception("Could not upload to storage service.");
         }
         $getInfo = $s3->getInfo($objectKey);
         //If for error we can't retrieve the md5 from the s3 server...
         if (!$getInfo) {
             $md5 = md5_file($fileInfo['tmp_name']);
         } else {
             $md5 = $getInfo['etag'];
         }
         if (!isset($details['short'])) {
             $details['short'] = '';
         }
         if (!isset($details['description'])) {
             $details['description'] = '';
         }
         $this->_dbTable->insert(array("id" => $uploadId, "byUid" => $userInfo['id'], "secret" => $secret, "download_secret" => $downloadSecret, "privacy" => $privacy, "title" => $title, "filename" => $filename, "short" => $details['short'], "description" => $details['description'], "type" => mb_substr($fileInfo['type'], 0, 50), "fileSize" => $fileInfo['size'], "md5" => $md5));
         if (!$this->_dbAdapter->lastInsertId()) {
             throw new Exception("Could not create insert for new file.");
         }
         $this->_dbAdapter->commit();
         return $uploadId;
     } catch (Exception $e) {
         $this->_dbAdapter->rollBack();
         throw $e;
     }
 }