예제 #1
0
 private function uploadHandler()
 {
     #$script_dir = Yii::app()->basePath.'/data/p3media';
     #$script_dir_url = Yii::app()->baseUrl;
     $options = array('url' => $this->createUrl("/p3media/p3Media/update", array('path' => Yii::app()->user->id . "/")), 'upload_dir' => $this->module->getDataPath() . DIRECTORY_SEPARATOR, 'upload_url' => $this->createUrl("/p3media/p3Media/update", array('preset' => 'raw', 'path' => Yii::app()->user->id . "/")), 'script_url' => $this->createUrl("/p3media/import/uploadFile", array('path' => Yii::app()->user->id . "/")), 'field_name' => 'files', 'image_versions' => array('thumbnail' => array('upload_url' => $this->createUrl("/p3media/file/image", array('preset' => 'p3media-upload', 'path' => urlencode(Yii::app()->user->id . "/"))), 'max_width' => 80, 'max_height' => 80)));
     // wrapper for jQuery-file-upload/upload.php
     $upload_handler = new UploadHandler($options);
     header('Pragma: no-cache');
     header('Cache-Control: private, no-cache');
     header('Content-Disposition: inline; filename="files.json"');
     header('X-Content-Type-Options: nosniff');
     ob_start();
     switch ($_SERVER['REQUEST_METHOD']) {
         case 'HEAD':
         case 'GET':
             $upload_handler->get();
             #$contents = ob_get_contents();
             $contents = "{}";
             // we do not show existing files, since this list may get very long
             break;
         case 'POST':
             // check if file exists
             $upload = $_FILES[$options['field_name']];
             $tmp_name = $_FILES[$options['field_name']]['tmp_name'];
             if (is_array($tmp_name)) {
                 foreach ($tmp_name as $index => $value) {
                     $model = P3Media::model()->findByAttributes(array('path' => Yii::app()->user->id . DIRECTORY_SEPARATOR . $upload['name'][$index]));
                     $model = new P3Media();
                     $attributes['path'] = Yii::app()->user->id . DIRECTORY_SEPARATOR . $upload['name'][$index];
                     #$attributes['title'] = $upload['name'][$index]; // TODO: fix title unique check
                     #var_dump($attributes['title']);exit;
                     $model->attributes = $attributes;
                     $model->validate(array('path'));
                     if ($model->hasErrors()) {
                         #throw new CHttpException(500, 'File exists.');
                         $file = new stdClass();
                         $file->error = "";
                         foreach ($model->getErrors() as $error) {
                             $file->error .= $error[0];
                         }
                         $info[] = $file;
                         echo CJSON::encode($info);
                         exit;
                     }
                 }
             }
             $upload_handler->post();
             $upload_handler_output = ob_get_contents();
             $result = CJSON::decode($upload_handler_output);
             #var_dump($result);exit;
             $savedMedia = $this->createMedia($result[0]['name'], $this->module->getDataPath() . DIRECTORY_SEPARATOR . $result[0]['name']);
             $result[0]['p3_media_id'] = $savedMedia->id;
             $contents = CJSON::encode($result);
             break;
         case 'DELETE':
             $upload_handler->delete();
             $contents = ob_get_contents();
             $result = $this->deleteMedia($_GET['path']);
             break;
         default:
             header('HTTP/1.0 405 Method Not Allowed');
             $contents = ob_get_contents();
     }
     ob_end_clean();
     return $contents;
 }