public function pic_upload() { $return_struct = array('status' => 0, 'code' => 501, 'msg' => 'Not Implemented', 'content' => array()); try { //* 初始化返回数据 */ $return_data = array(); $request_data = $this->input->post(); $request_product_id = $request_data['product_id']; // 请求的对应的图片说明 $request_pic_title_assoc = !empty($request_data) && isset($request_data['myPorductpicTitle']) && is_array($request_data['myPorductpicTitle']) && !empty($request_data['myPorductpicTitle']) ? $request_data['myPorductpicTitle'] : array(); //多附件上传 上传的表单域名字 $attach_field = 'myPorductpic'; // 附件应用类型 $attach_app_type = 'productPicAttach'; // 如果有上传请求 if (!page::issetFile($attach_field)) { throw new MyRuntimeException(Kohana::lang('o_global.bad_request'), 400); } //读取当前应用配置 $attach_setup = Kohana::config('attach.' . $attach_app_type); $mime_type2postfix = Kohana::config('mimemap.type2postfix'); $mime_postfix2type = Kohana::config('mimemap.postfix2type'); // 表单文件上传控件总数量 $file_upload_count = page::getFileCount($attach_field); // 初始化一些数据 // 本次文件上传总数量 $file_count_total = 0; // 本次文件上传总大小 $file_size_total = 0; // 上传文件meta信息 $file_meta_data = array(); // 遍历所有的上传域 //验证上传/采集上传信息 for ($index = 0; $index < $file_upload_count; $index++) { // 如果上传标志成功 if ((int) $_FILES[$attach_field]['error'][$index] === UPLOAD_ERR_OK) { if (!is_uploaded_file($_FILES[$attach_field]['tmp_name'][$index])) { throw new MyRuntimeException(Kohana::lang('o_product.file_upload_error'), 400); } $file_size_current = filesize($_FILES[$attach_field]['tmp_name'][$index]); if ($attach_setup['fileSizePreLimit'] > 0 && $file_size_current > $attach_setup['fileSizePreLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_not_flow') . $attach_setup['fileSizePreLimit'], 400); } $file_type_current = FALSE; $file_type_current === FALSE && ($file_type_current = page::getFileType($attach_field, $index)); // 尝试通过Mime类型判断 $file_type_current === FALSE && page::getImageType($_FILES[$attach_field]['tmp_name'][$index]); // 尝试通过图片类型判断 $file_type_current === FALSE && ($file_type_current = page::getPostfix($attach_field, $index)); // 尝试通过后缀截取 //array_walk($attach_setup['allowTypes'], 'strtolower'); foreach ($attach_setup['allowTypes'] as $idx => $item) { $attach_setup['allowTypes'][$idx] = strtolower($item); } if (!empty($attach_setup['allowTypes']) && !in_array(strtolower($file_type_current), $attach_setup['allowTypes'])) { throw new MyRuntimeException(Kohana::lang('o_product.pic_type_error'), 400); } // 当前文件mime类型 $file_mime_current = isset($_FILES[$attach_field]['type'][$index]) ? $_FILES[$attach_field]['type'][$index] : ''; // 检测规整mime类型 if (!array_key_exists($file_mime_current, $mime_type2postfix)) { if (array_key_exists($file_type_current, $mime_postfix2type)) { $file_mime_current = $mime_postfix2type[$file_type_current]; } else { $file_mime_current = 'application/octet-stream'; } } //存储文件meta信息 $file_meta_data[$index] = array('name' => strip_tags(trim($_FILES[$attach_field]['name'][$index])), 'size' => $file_size_current, 'type' => $file_type_current, 'mime' => $file_mime_current, 'tmpfile' => $_FILES[$attach_field]['tmp_name'][$index]); // 设置上传总数量 $file_count_total += 1; // 设置上传总大小 $file_size_total += $file_size_current; } else { throw new MyRuntimeException(Kohana::lang('o_product.pic_upload_failed'), 400); } } if ($attach_setup['fileCountLimit'] > 0 && $file_count_total > $attach_setup['fileCountLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_count_limit') . $attach_setup['fileCountLimit'], 400); } if ($attach_setup['fileSizeTotalLimit'] > 0 && $file_size_total > $attach_setup['fileSizeTotalLimit']) { throw new MyRuntimeException(Kohana::lang('o_product.file_size_total_limit') . $attach_setup['fileSizeTotalLimit'] . Kohana::lang('o_product.size') . $file_size_total, 400); } /* // 当前时间戳 $timestamp_current = time(); //预备一些数据 $src_ip_address = $this->input->ip_address(); $attach_meta = array( 'siteId'=>$site_id, 'siteDomain'=>$site_domain, ); // 调用附件服务 //$attachmentService = AttachmentService::get_instance(); require_once(Kohana::find_file('vendor', 'phprpc/phprpc_client',TRUE)); !isset($attachmentService) && $attachmentService = new PHPRPC_Client(Kohana::config('phprpc.remote.Attachment.host')); !isset($phprpcApiKey) && $phprpcApiKey = Kohana::config('phprpc.remote.Attachment.apiKey'); */ $route_prefix = Kohana::config('attach.routePrefix'); $route_mask_view = Kohana::config('attach.routeMaskViewProduct'); // 调用productpic服务 $productpicService = ProductpicService::get_instance(); // 遍历所有的上传meta域 foreach ($file_meta_data as $index => $file_meta) { $row_meta_struct = array(); /*$attachment_data_original = array( 'filePostfix'=>$file_meta['type'], 'fileMimeType'=>$file_meta['mime'], 'fileSize'=>$file_meta['size'], 'fileName'=>$file_meta['name'], 'srcIp'=>$src_ip_address, 'attachMeta'=>json_encode($attach_meta), 'createTimestamp'=>$timestamp_current, 'modifyTimestamp'=>$timestamp_current, ); // 调用后端添加附件信息,并调用存储服务存储文件 $args_org = array($attachment_data_original); $sign_org = md5(json_encode($args_org).$phprpcApiKey); $attachment_original_id = $attachmentService->phprpc_addAttachmentFileData($attachment_data_original,@file_get_contents($file_meta['tmpfile']),$sign_org); if (!is_numeric($attachment_original_id)) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 400); }*/ $att = AttService::get_instance($this->img_dir_name); $img_id = $att->save_default_img($file_meta['tmpfile']); if (!$img_id) { throw new MyRuntimeException(Kohana::lang('o_product.phprpc_pic_save_failed'), 500); } $productpic_data = array('product_id' => $request_product_id, 'is_default' => ProductpicService::PRODUCTPIC_IS_DEFAULT_FALSE, 'title' => isset($request_pic_title_assoc[$index]) ? strip_tags(trim($request_pic_title_assoc[$index])) : '', 'image_id' => $img_id); $productpic_row_id = $productpicService->add($productpic_data); $return_row_struct = array('id' => $productpic_row_id, 'is_default' => $productpic_data['is_default'], 'title' => $productpic_data['title'], 'image_id' => $img_id, 'picurl_o' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_ORIGINAL, $file_meta['type'], $route_mask_view), 'picurl_t' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_THUMBNAIL, $file_meta['type'], $route_mask_view), 'picurl_l' => ProductpicService::get_attach_url($route_prefix, $img_id, ProductpicService::PRODUCTPIC_STANDARDS_LARGE, $file_meta['type'], $route_mask_view)); $return_data[] = $return_row_struct; // 清理临时文件 @unlink($file_meta['thumb']['tmpfile']); } if (!empty($return_data) && $request_product_id > 0) { //检测是否存在默认图片,没有则设置商品默认图片 if ($productpicService->has_default_pic($request_product_id) == FALSE) { $default_productpic_id = $return_data[0]['id']; $productpicService->set_default_pic_by_productpic_id($default_productpic_id, $request_product_id); $return_data[0]['is_default'] = ProductpicService::PRODUCTPIC_IS_DEFAULT_TRUE; } } //Cache::remove('product_pictures.'.$request_product_id); //* 补充&修改返回结构体 */ $return_struct['status'] = 1; $return_struct['code'] = 200; $return_struct['msg'] = '上传成功!'; $return_struct['content'] = $return_data; //$pics = $productpicService->get_stand_pic_by_pic_id($productpic_row_id, ProductpicService::PRODUCTPIC_STANDARDS_COMMON); //* 请求类型 */ if ($this->is_ajax_request()) { // ajax 请求 // json 输出 $this->template->content = $return_struct; } else { // html 输出 $this->template = new View('layout/empty_html'); //$this->template->manager_data = $this->manager; //* 模板输出 */ //$this->template->return_struct = $return_struct; $content = new View($this->package_name . '/' . $this->class_name . '/' . __FUNCTION__); //* 变量绑定 */ //$this->template->title = Kohana::config('site.name'); $this->template->content = $content; //* 请求结构数据绑定 */ //$this->template->content->request_data = $request_data; //* 返回结构体绑定 */ //$this->template->content->return_struct = $return_struct; $this->template->content->picture = $return_struct['content']; } // end of request type determine } catch (MyRuntimeException $ex) { $this->template = new View('layout/default_html'); $this->_ex($ex, $return_struct, $request_data); } }