Ejemplo n.º 1
0
 /**
  *
  * 分段上传接口
  * 第一步:
  * POST bp_upload.json
  * {
  * 	"md5":"文件md5",
  * 	"size":"文件大小",
  * 	"basedir":"文件夹路径",
  * 	"type":"文件解析类型 0文件,1音频,4视频",
  * 	"filename":"文件名"
  * }
  *
  * 第二部:
  * GET bp_upload.json?upload_id={临时上传id}&offset={碎片偏移量}&length={分段大小}
  * FILES
  */
 public function bp_upload()
 {
     $upload_id = isset($_GET['upload_id']) ? $_GET['upload_id'] : 0;
     if (!$upload_id) {
         //创建临时文件
         $data = $this->get_data();
         if (isset($data['md5']) && isset($data['size'])) {
         } else {
             $this->response(ResponseType::ERROR_LACKPARAMS);
         }
         if ($data['size'] > Core::config('file_max_size')) {
             $this->response(ResponseType::FILE_ERROR_SIZELIMIT);
         }
         //存放路径是否存在
         $basedir = $data['basedir'];
         if (!$basedir) {
             $basedir = '/home';
         }
         $basedirModel = new Models\FileDirectory();
         if (!$basedirModel->findOne($basedir)) {
             $this->response(ResponseType::FILE_ERROR_DIR_INVALID);
         }
         $tempModel = new Models\Temp($this->user_id);
         $tempModel->md5 = strtolower($data['md5']);
         $tempModel->size = intval($data['size']);
         $tempModel->type = intval($data['type']);
         $tempModel->cid = intval($data['category']);
         $tempModel->basedir = $basedir;
         if (!$data['filename']) {
             $tempModel->filename = sprintf('%.0f', microtime(TRUE) * 1000);
         } else {
             $tempModel->filename = $data['filename'];
         }
         if ($tempModel->create()) {
             $result['upload_id'] = $tempModel->get_upload_id();
             $result['offset'] = 0;
             $result['uploaded'] = FALSE;
             $this->response(ResponseType::PHOTO_BPUPLOAD_OK_CREATETEMP, '', $result);
         }
         $this->response(ResponseType::PHOTO_BPUPLOAD_ERROR_CREATETEMP);
     } else {
         //上传第二步
         $offset = isset($_GET['offset']) ? intval($_GET['offset']) : NULL;
         $length = isset($_GET['length']) ? intval($_GET['length']) : NULL;
         /*
         if(isset($_GET['offset'])) {
         $offset=intval($_GET['offset']);
         }else{
         $this->response(ResponseType::ERROR_LACKPARAMS);
         }
         */
         $tempModel = new Models\Temp($this->user_id);
         if (!$tempModel->getTemp($upload_id)) {
             $this->response(ResponseType::PHOTO_BPUPLOAD_ERROR_NOTEMP);
         }
         //追加文件碎片
         if ($tempModel->tmp_source->length < $tempModel->size) {
             if ($_FILES) {
                 foreach ($_FILES as $upfile) {
                     $in_path = $upfile['tmp_name'];
                 }
             } else {
                 $in_path = 'php://input';
             }
             $tempModel->append($in_path, $offset, $length);
         }
         //验证临时文件完整性
         $finish = FALSE;
         if ($tempModel->tmp_source->length >= $tempModel->size) {
             if ($tempModel->md5 && $tempModel->md5 == $tempModel->tmp_source->md5) {
                 $finish = TRUE;
             } else {
                 $tempModel->destroy();
                 $this->response(ResponseType::PHOTO_BPUPLOAD_ERROR_DAMAGETEMP);
             }
         }
         if ($finish) {
             $tmp_file = $tempModel->tmp_source->downBuffer();
             //临时文件是否可访问
             if ($tmp_file) {
                 $uploader = new Uploader();
                 $uploader->process($tmp_file, '', $tempModel->size, $tempModel->md5);
             } else {
                 $tempModel->destroy();
                 $this->response(ResponseType::PHOTO_BPUPLOAD_ERROR_NOTEMPSOURCE);
             }
             //不是音频类型
             /*
             if($tempModel->type==Uploader::FILETYPE_AUDIO && $uploader->getType() != Uploader::FILETYPE_AUDIO){
             $tempModel->destroy();
             $this->response(ResponseType::FILE_ERROR_AUDIOTYPE);
             }
             */
             //文件超过限制
             if ($uploader->getLength() > Core::config('file_max_size')) {
                 $tempModel->destroy();
                 $this->response(ResponseType::FILE_ERROR_SIZELIMIT);
             }
             //上传临时文件成功否
             $basedirModel = new Models\FileDirectory();
             if (!$basedirModel->findOne($tempModel->basedir)) {
                 $this->response(ResponseType::FILE_ERROR_DIR_INVALID);
             }
             if ($tempModel->type == Uploader::FILETYPE_VIDEO) {
                 $ctrl_type = 2;
             } elseif ($tempModel->type == Uploader::FILETYPE_AUDIO) {
                 $ctrl_type = 1;
             } else {
                 $ctrl_type = 0;
             }
             $uploader->ctrl_type = $ctrl_type;
             $uploader->filename = $tempModel->filename;
             if ($fileModel = $basedirModel->makeFile($uploader)) {
                 $result['id'] = $fileModel->get_fid();
                 $result['src'] = $fileModel->geturi($result['id']);
                 $result['mime'] = $fileModel->mime;
                 $result['size'] = $fileModel->size;
                 $result['name'] = $fileModel->getFilename();
                 $result['uploaded'] = TRUE;
                 $tempModel->destroy();
                 if ($fileModel->thumb_id) {
                     $photoModel = new Models\Photo();
                     if ($photoModel->findOne($fileModel->thumb_id, array('width', 'height'))) {
                         $result['thumb']['id'] = $photoModel->get_pid();
                         $result['thumb']['md5'] = $photoModel->md5;
                         $result['thumb']['width'] = $photoModel->width;
                         $result['thumb']['height'] = $photoModel->height;
                         $result['thumb']['mime'] = $photoModel->mime;
                         $imgurls = $photoModel->geturi($fileModel->thumb_id, 130);
                         $result['thumb']['src'] = $imgurls[0];
                     }
                 }
                 $this->response(ResponseType::FILE_OK, '', $result);
             } else {
                 $tempModel->destroy();
                 $this->response(ResponseType::FILE_ERROR_SERVER);
             }
         } else {
             //需要继续上传文件碎片
             $result['offset'] = $tempModel->tmp_source->length;
             $result['uploaded'] = FALSE;
             $this->response(ResponseType::PHOTO_BPUPLOAD_OK_CONTINUE, '', $result);
         }
     }
 }
Ejemplo n.º 2
0
 private function show_image($pid, $type, $is_animate = FALSE)
 {
     $this->set_browsercache();
     if ($type > 0) {
         //如果是缩略图
         /*
         $client= new GearmanClient();
         $client->addServers(Core::config('job_servers'));
         $client->setTimeout(3000);
         $result = @ $client->doHigh("thumbnail", serialize(array($pid, $type)));
         $result && $sid = @ unserialize($result);
         */
         if ($sid) {
             $source = new Models\PhotoSource();
             $source->findOne($sid);
             $source->output();
             //异步缩略图
         } else {
             $thumb = new Thumb();
             if ($thumb->resize(NULL, $pid, $type, $is_animate)) {
                 $thumb->output();
                 //同步缩略图
             }
         }
     } else {
         $photoModel = new Models\Photo();
         $photoModel->findOne($pid);
         $photosource = $photoModel->getSource();
         if ($photosource) {
             $photosource->output();
         }
     }
 }
Ejemplo n.º 3
0
 public function upload_by_url()
 {
     $data = $this->get_data();
     $url = $data['url'];
     $scale_width = intval($data['scale_width']);
     $x = intval($data['x']);
     $y = intval($data['y']);
     $w = intval($data['w']);
     $h = intval($data['h']);
     if ($scale_width < 1 || $x < 0 || $y < 0 || $w < 1 || $h < 1) {
         $this->response(ResponseType::PHOTO_UPLOAD_ERROR_SERVER);
     }
     $tmpfile = Core::tempname();
     if (file_put_contents($tmpfile, file_get_contents($url))) {
     } else {
         $this->response(ResponseType::PHOTO_UPLOAD_ERROR_SERVER);
     }
     $thumb = new Thumb();
     $uploader = $thumb->crop_by_url($tmpfile, $scale_width, $x, $y, $w, $h);
     $photoModel = new Models\Photo();
     $updata['cid'] = 3;
     $updata['is_animated'] = 0;
     $updata['mtime'] = time();
     if ($photoModel->create($uploader, $updata)) {
         $result['id'] = $photoModel->get_pid();
         $result['md5'] = $photoModel->md5;
         $imgurls = $photoModel->geturi($result['id']);
         $result['src'] = $imgurls[0];
         unlink($tmpfile);
         $this->response(ResponseType::PHOTO_GET_OK, '', $result);
     } else {
         $this->response(ResponseType::PHOTO_UPLOAD_ERROR_SERVER);
     }
 }