/** * * 分段上传接口 * 第一步: * 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); } } }
/** * * 分段上传接口 * 第一步: * POST bp_upload.json * { * "md5":"照片md5", * "size":"尺寸", * "category":"分类id", * "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('photo_max_size')) { $this->response(ResponseType::PHOTO_ERROR_IMAGESIZE); } $tempModel = new Models\Temp($this->user_id); $tempModel->md5 = strtolower($data['md5']); $tempModel->size = intval($data['size']); $tempModel->type = Uploader::FILETYPE_IMAGE; //图片应用 $tempModel->cid = intval($data['category']); $tempModel->filename = $data['filename']; $photoModel = new Models\Photo(); $r = $photoModel->geturiByMD5(array($tempModel->md5), 130, TRUE); //自己曾经上传过 if ($r[0] && $r[0]['id']) { $result['id'] = $r[0]['id']; $result['src'] = $r[0]['src']; $result['md5'] = $tempModel->md5; $result['uploaded'] = TRUE; $this->response(ResponseType::PHOTO_BPUPLOAD_OK, '', $result); } else { 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){ if ($tempModel->size == $tempModel->tmp_source->length) { $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 ($uploader->getType() !== Uploader::FILETYPE_IMAGE) { $tempModel->destroy(); $this->response(ResponseType::PHOTO_ERROR_IMAGETYPE); } //文件超过5M if ($uploader->getLength() > Core::config('photo_max_size')) { $tempModel->destroy(); $this->response(ResponseType::PHOTO_ERROR_IMAGESIZE); } //上传临时文件成功否 if ($result = $this->_processUpload($uploader, $tempModel->cid)) { $tempModel->destroy(); $result['uploaded'] = TRUE; $this->response(ResponseType::PHOTO_BPUPLOAD_OK, '', $result); } else { $tempModel->destroy(); $this->response(ResponseType::PHOTO_BPUPLOAD_ERROR_SERVER); } } else { //需要继续上传文件碎片 $result['offset'] = $tempModel->tmp_source->length; $result['uploaded'] = FALSE; $this->response(ResponseType::PHOTO_BPUPLOAD_OK_CONTINUE, '', $result); } } }