コード例 #1
0
ファイル: Uploader.php プロジェクト: sitengine/sitengine
 public function run($prefix, array $amzHeaders)
 {
     $this->_countSkipped = 0;
     $this->_countReplaced = 0;
     $this->_countCopied = 0;
     $this->_countErrors = 0;
     $this->_countKeys = 0;
     $uploadDir = dir($this->_pathName);
     while (false !== ($file = $uploadDir->read())) {
         if (preg_match('/^\\.(\\.|DS_Store)?$/', $file)) {
             continue;
         }
         try {
             $this->_countKeys++;
             $key = $prefix ? $prefix . '/' . $file : $file;
             $inFile = $this->_pathName . '/' . $file;
             $this->_print('uploading: ' . $file . ' -> ' . $key);
             require_once 'Sitengine/Amazon/S3/Object.php';
             $object = new Sitengine_Amazon_S3_Object($this->_connection, $this->_bucketName, $key);
             $head = null;
             if (!$this->_force) {
                 $head = $object->head();
                 if ($head->getHttpResponse()->getStatus() == 200) {
                     $size = $head->getClient()->getLastResponse()->getHeader('Content-length');
                     if ($size !== null && $size == filesize($inFile)) {
                         $this->_print(" -> SKIPPED (EXISTS)\n");
                         $this->_countSkipped++;
                         continue;
                     }
                 }
             }
             $put = $object->put($inFile, array(), $amzHeaders);
             if ($put->getHttpResponse()->isError()) {
                 # try again
                 $put = $object->put($inFile, array(), $amzHeaders);
                 if ($put->getHttpResponse()->isError()) {
                     require_once 'Sitengine/Amazon/S3/Utils/Exception.php';
                     throw new Sitengine_Amazon_S3_Utils_Exception('Upload Error');
                 }
             }
             if ($head !== null && $head->getHttpResponse()->getStatus() == 200) {
                 $this->_countReplaced++;
                 $this->_print(" -> REPLACE OK\n");
             } else {
                 $this->_countCopied++;
                 $this->_print(" -> COPY OK\n");
             }
         } catch (Exception $exception) {
             $this->_countErrors++;
             $msg = " -> ERROR (" . $exception->getMessage() . ")\n";
             $this->_print($msg);
         }
     }
     $uploadDir->close();
     return array('skipped' => $this->_countSkipped, 'replaced' => $this->_countReplaced, 'copied' => $this->_countCopied, 'errors' => $this->_countErrors, 'keys' => $this->_countKeys);
 }
コード例 #2
0
 protected function _saveUploadedFile($fileId, Sitengine_Upload $upload, $name)
 {
     try {
         require_once 'Sitengine/Mime/Type.php';
         $width = 0;
         $height = 0;
         $mime = $upload->getMime();
         if ($mime == 'application/octet-stream') {
             #if(preg_match('/.*\.(gif|jpg|jpeg|png|mp3|pdf|wav|doc|xls|zip|aif|tif|css|sit|tar)$/i', $name))
             #{
             # try to fix mimetype if file is being uploaded through a flash app
             require_once 'Sitengine/Mime/Type.php';
             $mime = Sitengine_Mime_Type::get($name);
             #}
         }
         if (Sitengine_Mime_Type::isImage($mime)) {
             $info = getimagesize($upload->getTempName());
             if (!$info) {
                 require_once 'Sitengine/Exception.php';
                 throw new Sitengine_Exception('uploaded file is not an image');
             }
             $width = $info[0];
             $height = $info[1];
         }
         $data = array('name' => $name, 'source' => $upload->getName(), 'mime' => $mime, 'size' => $upload->getSize(), 'width' => $width, 'height' => $height);
         #$key = $this->_configs[$fileId]['prefix'].'/'.$name;
         #$object = $this->_configs[$fileId]['object'];
         require_once 'Sitengine/Amazon/S3/Object.php';
         $object = new Sitengine_Amazon_S3_Object($this->_configs[$fileId]['connection'], $this->_configs[$fileId]['bucket'], $this->_configs[$fileId]['prefix'] . '/' . $name, $this->_configs[$fileId]['cname'], $this->_configs[$fileId]['ssl']);
         $amzHeaders = $this->_configs[$fileId]['amzHeaders'];
         $response = $object->put($upload->getTempName(), array(), $amzHeaders);
         if ($response->getHttpResponse()->isError()) {
             require_once 'Sitengine/Exception.php';
             throw new Sitengine_Exception('file could not be uploaded to s3');
         }
         $this->_newFiles[$fileId] = $name;
         $this->_files[$fileId] = $data;
     } catch (Exception $exception) {
         $this->_rollback();
         throw $exception;
     }
 }