Ejemplo n.º 1
0
 private function upload($imagesNum = 1)
 {
     $hash = new Unsee_Hash();
     for ($x = 1; $x <= $imagesNum; $x++) {
         $image = new Unsee_Image($hash);
         $image->setFile(TEST_DATA_PATH . '/images/good/1mb.jpg');
     }
     $hash->expireAt(time() + 100);
     return $hash;
 }
Ejemplo n.º 2
0
 public function __construct(Unsee_Hash $hash, $imgKey = null)
 {
     $newImage = is_null($imgKey);
     if ($newImage) {
         $imgKey = uniqid();
     }
     parent::__construct($hash->key . '_' . $imgKey);
     if ($newImage) {
         $keys = Unsee_Image::keys($hash->key . '*');
         $this->num = count($keys);
         $this->expireAt(time() + $hash->ttl());
     }
     $this->setSecureParams();
 }
Ejemplo n.º 3
0
 /**
  * Returns an array of image models assigned to the hash
  * @return \Unsee_Image[]
  */
 public function getImages()
 {
     // read files in directory
     $imagesKeys = Unsee_Image::keys($this->key . '*');
     $imageDocs = array();
     foreach ($imagesKeys as $key) {
         list(, $imgKey) = explode('_', $key);
         $imageDocs[] = new Unsee_Image($this, $imgKey);
     }
     usort($imageDocs, function ($a, $b) {
         if ($a->num === $b->num) {
             return 0;
         }
         return $a->num < $b->num ? -1 : 1;
     });
     return $imageDocs;
 }
Ejemplo n.º 4
0
 /**
  * Controller to handle file upload form
  * @throws Exception
  */
 public function indexAction()
 {
     $response = new stdClass();
     try {
         $upload = new Zend_File_Transfer();
     } catch (Exception $e) {
         $response->error = $e->getMessage();
         $this->_helper->json->sendJson($response);
     }
     $upload->addValidator('Count', false, array('min' => 1, 'max' => 100));
     $upload->addValidator('IsImage', false);
     $upload->addValidator('Size', false, array('max' => '10MB', 'bytestring' => false));
     $translate = Zend_Registry::get('Zend_Translate');
     $updating = false;
     try {
         if (!$upload->receive()) {
             throw new Exception($translate->translate('error_uploading'));
         } else {
             $files = $upload->getFileInfo();
             // Updating hash with new images
             if (!empty($_POST['hash']) && Unsee_Hash::isValid($_POST['hash'])) {
                 $hashDoc = new Unsee_Hash($_POST['hash']);
                 $updating = true;
                 $response = array();
                 if (!Unsee_Session::isOwner($hashDoc) && !$hashDoc->allow_anonymous_images) {
                     die('[]');
                 }
             } else {
                 // Creating a new hash
                 $hashDoc = new Unsee_Hash();
                 $this->setExpiration($hashDoc);
                 $response->hash = $hashDoc->key;
             }
             $imageAdded = false;
             foreach ($files as $file => $info) {
                 if ($upload->isUploaded($file)) {
                     $imgDoc = new Unsee_Image($hashDoc);
                     $res = $imgDoc->setFile($info['tmp_name']);
                     $imgDoc->setSecureParams();
                     //hack to populate correct secureTtd
                     if ($updating) {
                         $ticket = new Unsee_Ticket();
                         $ticket->issue($imgDoc);
                         $newImg = new stdClass();
                         $newImg->hashKey = $hashDoc->key;
                         $newImg->key = $imgDoc->key;
                         $newImg->src = '/image/' . $imgDoc->key . '/' . $imgDoc->secureMd5 . '/' . $imgDoc->secureTtd . '/';
                         $newImg->width = $imgDoc->width;
                         $newImg->ticket = md5(Unsee_Session::getCurrent() . $hashDoc->key);
                         $response[] = $newImg;
                     }
                     if ($res) {
                         $imageAdded = true;
                     }
                     // Remove uploaded file from temporary dir if it wasn't removed
                     if (file_exists($info['tmp_name'])) {
                         @unlink($info['tmp_name']);
                     }
                 }
             }
             if (!$imageAdded) {
                 throw new Exception('No images were added');
             }
         }
     } catch (Exception $e) {
         $response->error = $e->getMessage();
     }
     $this->_helper->json->sendJson($response);
 }