public function getFileType($filename)
 {
     $re = "1";
     if (preg_match(\Justsy\BaseBundle\Common\MIME::getMIMEImgReg(), strtolower($filename))) {
         $re = "2";
     } else {
         if (preg_match(\Justsy\BaseBundle\Common\MIME::getMIMEMediaReg(), strtolower($filename))) {
             $re = "3";
         }
     }
     return $re;
 }
 private function getFileId($upfile, $maxwidth = 2100, $maxheight = 1600)
 {
     $fileid = '';
     if (empty($upfile)) {
         return '';
     }
     $isimage = preg_match(\Justsy\BaseBundle\Common\MIME::getMIMEImgReg(), strtolower($upfile->getClientOriginalName()));
     if ($isimage) {
         if ($maxwidth > 0 && $maxheight > 0) {
             $im = new \Imagick($upfile->getPathname());
             if (!empty($maxwidth) && $im->getImageWidth() > $maxwidth) {
                 unlink($upfile->getPathname());
                 return '';
             }
             if (!empty($maxheight) && $im->getImageHeight() > $maxheight) {
                 unlink($upfile->getPathname());
                 return '';
             }
         }
         //开始上传图片
         $dm = $this->get('doctrine.odm.mongodb.document_manager');
         $doc = new \Justsy\MongoDocBundle\Document\WeDocument();
         $filename = $upfile->getClientOriginalName();
         $doc->setName($filename);
         $doc->setFile($upfile->getPathname());
         $dm->persist($doc);
         $dm->flush();
         $fileid = $doc->getId();
     }
     unlink($upfile->getPathname());
     return $fileid;
 }
 public function fileuploadAction()
 {
     $re = array("returncode" => ReturnCode::$SUCCESS);
     $request = $this->getRequest();
     $user = $this->get('security.context')->getToken()->getUser();
     $filename = $request->get("filename");
     $circle_id = $request->get("circle_id");
     $group_id = $request->get("group_id");
     // multipart/form-data
     try {
         if (empty($filename) || empty($circle_id) || empty($group_id)) {
             throw new \Exception("param is null");
         }
         if (!isset($_FILES['userfile'])) {
             $upfile = tempnam(sys_get_temp_dir(), "we");
             unlink($upfile);
             $somecontent1 = base64_decode($request->get('filedata'));
             if ($handle = fopen($upfile, "w+")) {
                 if (!fwrite($handle, $somecontent1) == FALSE) {
                     fclose($handle);
                 }
             }
         } else {
             $upfile = $_FILES['userfile']['tmp_name'];
         }
         $isimage = preg_match(\Justsy\BaseBundle\Common\MIME::getMIMEImgReg(), strtolower($filename));
         $imagefilename_small = preg_replace("/(\\.[^\\.]*)\$/", '_small\\1', $filename);
         $imagefilename_middle = preg_replace("/(\\.[^\\.]*)\$/", '_middle\\1', $filename);
         $imagefilepath_small = $upfile . "." . $imagefilename_small;
         $imagefilepath_middle = $upfile . "." . $imagefilename_middle;
         $dm = $this->get('doctrine.odm.mongodb.document_manager');
         $doc = new \Justsy\MongoDocBundle\Document\WeDocument();
         $doc->setName($filename);
         $doc->setFile($upfile);
         $dm->persist($doc);
         $dm->flush();
         $fileid = $doc->getId();
         $fileid_small = "";
         $fileid_middle = "";
         if ($isimage) {
             $im = new \Imagick($upfile);
             if ($im->getImageWidth() > 100) {
                 if ($im->getImageWidth() >= $im->getImageHeight()) {
                     $im->scaleImage(100, 0);
                 } else {
                     $im->scaleImage(0, 100);
                 }
                 $im->writeImage($imagefilepath_small);
                 $im->destroy();
             } else {
                 copy($upfile, $imagefilepath_small);
             }
             $im = new \Imagick($upfile);
             if ($im->getImageWidth() > 400) {
                 $im->scaleImage(400, 0);
                 $im->writeImage($imagefilepath_middle);
                 $im->destroy();
             } else {
                 copy($upfile, $imagefilepath_middle);
             }
             $doc = new \Justsy\MongoDocBundle\Document\WeDocument();
             $doc->setName($imagefilename_small);
             $doc->setFile($imagefilepath_small);
             $dm->persist($doc);
             $dm->flush();
             $fileid_small = $doc->getId();
             unlink($imagefilepath_small);
             $doc = new \Justsy\MongoDocBundle\Document\WeDocument();
             $doc->setName($imagefilename_middle);
             $doc->setFile($imagefilepath_middle);
             $dm->persist($doc);
             $dm->flush();
             $fileid_middle = $doc->getId();
             unlink($imagefilepath_middle);
         }
         unlink($upfile);
         //д��ݿ⣺we_files
         $da = $this->get("we_data_access");
         $fixs = explode(".", strtolower($filename));
         $sqls = array();
         $all_params = array();
         $sqls[] = "insert into we_files(`circle_id`,`file_ext`,`file_id`,`file_name`,`post_to_group`,`up_by_staff`,`up_date`)values(?,?,?,?,?,?,now())";
         $all_params[] = array((string) $circle_id, (string) $fixs[count($fixs) - 1], (string) $fileid, (string) $filename, (string) $group_id, (string) $user->getUsername());
         if ($isimage) {
             $sqls[] = "insert into we_files_image(file_id, file_id_small, file_id_middle) values(?, ?, ?)";
             $all_params[] = array((string) $fileid, (string) $fileid_small, (string) $fileid_middle);
         }
         $da->ExecSQLs($sqls, $all_params);
         $re["returncode"] = ReturnCode::$SUCCESS;
         $re["file_id"] = $fileid;
     } catch (\Exception $e) {
         $re["returncode"] = ReturnCode::$SYSERROR;
         $this->get('logger')->err($e);
     }
     $response = new Response($request->get('jsoncallback') ? $request->get('jsoncallback') . "(" . json_encode($re) . ");" : json_encode($re));
     $response->headers->set('Content-Type', 'text/json');
     return $response;
 }
 public function uploadAction($network_domain)
 {
     try {
         $request = $this->get("request");
         $user = $this->get('security.context')->getToken()->getUser();
         $circleId = $user->get_circle_id($network_domain);
         $uploadSourcePage = $request->get("uploadSourcePage");
         $upfile = $request->files->get("filedata");
         $isimage = preg_match(\Justsy\BaseBundle\Common\MIME::getMIMEImgReg(), strtolower($upfile->getClientOriginalName()));
         $imagefilename_small = preg_replace("/(\\.[^\\.]*)\$/", '_small\\1', $upfile->getClientOriginalName());
         $imagefilename_middle = preg_replace("/(\\.[^\\.]*)\$/", '_middle\\1', $upfile->getClientOriginalName());
         $imagefilepath_small = $upfile->getPathname() . "." . $imagefilename_small;
         $imagefilepath_middle = $upfile->getPathname() . "." . $imagefilename_middle;
         $dm = $this->get('doctrine.odm.mongodb.document_manager');
         $doc = new \Justsy\MongoDocBundle\Document\WeDocument();
         $filename = $upfile->getClientOriginalName();
         $doc->setName($filename);
         $doc->setFile($upfile->getPathname());
         $dm->persist($doc);
         $dm->flush();
         $fileid = $doc->getId();
         $fileid_small = "";
         $fileid_middle = "";
         if ($isimage) {
             $im = new \Imagick($upfile->getPathname());
             if ($im->getImageWidth() > 100) {
                 if ($im->getImageWidth() >= $im->getImageHeight()) {
                     $im->scaleImage(100, 0);
                 } else {
                     $im->scaleImage(0, 100);
                 }
                 $im->writeImage($imagefilepath_small);
                 $im->destroy();
             } else {
                 copy($upfile->getPathname(), $imagefilepath_small);
             }
             $im = new \Imagick($upfile->getPathname());
             if ($im->getImageWidth() > 400) {
                 $im->scaleImage(400, 0);
                 $im->writeImage($imagefilepath_middle);
                 $im->destroy();
             } else {
                 copy($upfile->getPathname(), $imagefilepath_middle);
             }
             $doc = new \Justsy\MongoDocBundle\Document\WeDocument();
             $doc->setName($imagefilename_small);
             $doc->setFile($imagefilepath_small);
             $dm->persist($doc);
             $dm->flush();
             $fileid_small = $doc->getId();
             unlink($imagefilepath_small);
             $doc = new \Justsy\MongoDocBundle\Document\WeDocument();
             $doc->setName($imagefilename_middle);
             $doc->setFile($imagefilepath_middle);
             $dm->persist($doc);
             $dm->flush();
             $fileid_middle = $doc->getId();
             unlink($imagefilepath_middle);
         }
         unlink($upfile->getPathname());
         //写数据库:we_files
         $da = $this->get("we_data_access");
         $fixs = explode(".", strtolower($filename));
         $gorupid = $request->get("hpost_to_group");
         $hpost_to_dir = $request->get("hpost_to_dir");
         if ("ALL" == $gorupid) {
             $hpost_to_dir = "c" . $circleId;
         }
         //如果是通过发动态上传文件,且发布到圈子时,存储目录默认为圈子目录
         if (empty($hpost_to_dir)) {
             $hpost_to_dir = strlen($gorupid) == 0 ? "c" . $circleId : 'g' . $gorupid;
             //如果没指定上传目录时,默认为当前圈子或者群组
         }
         $firstChar = substr($hpost_to_dir, 0, 1);
         if ($uploadSourcePage == "dir") {
             //直接上传文档
             //直接上传文档(uploadSourcePage为dir)到圈子/群组时,需要自动发布一条动态
             if ($firstChar != "c" && $firstChar != "g") {
                 $uploadSourcePage = "home";
             }
             //上传到子目录内时,不发布动态
         }
         $gorupid = strlen($gorupid) == 0 ? "ALL" : $gorupid;
         $sqls = array();
         $all_params = array();
         $sqls[] = "insert into we_files(`circle_id`,`file_ext`,`file_id`,`file_name`,`post_to_group`,`up_by_staff`,`up_date`,`dir`)values(?,?,?,?,?,?,now(),?)";
         $all_params[] = array((string) $circleId, (string) $fixs[count($fixs) - 1], (string) $fileid, (string) $filename, (string) $gorupid, (string) $user->getUsername(), (string) $hpost_to_dir);
         if ($uploadSourcePage == null || $uploadSourcePage != "home") {
             $conv_id = \Justsy\BaseBundle\DataAccess\SysSeq::GetSeqNextValue($da, "we_convers_list", "conv_id");
             $sqlInsert = 'insert into we_convers_list (conv_id, login_account, post_date, conv_type_id, conv_root_id, conv_content, post_to_group, post_to_circle, copy_num, reply_num) values (?, ?, CURRENT_TIMESTAMP(), ?, ?, ?, ?, ?, 0, 0)';
             $params = array();
             $params[] = (string) $conv_id;
             $params[] = (string) $user->getUserName();
             $params[] = (string) '00';
             $params[] = (string) $conv_id;
             $params[] = "上传了新文件";
             $params[] = (string) $gorupid;
             $params[] = (string) $circleId;
             $sqls[] = $sqlInsert;
             $all_params[] = $params;
             $sqlInsert = "insert into we_convers_attach (conv_id, attach_type, attach_id) values (?, '0', ?)";
             $params = array();
             $params[] = (string) $conv_id;
             $params[] = (string) $fileid;
             $sqls[] = $sqlInsert;
             $all_params[] = $params;
         }
         if ($isimage) {
             $sqls[] = "insert into we_files_image(file_id, file_id_small, file_id_middle) values(?, ?, ?)";
             $all_params[] = array((string) $fileid, (string) $fileid_small, (string) $fileid_middle);
         }
         $da->ExecSQLs($sqls, $all_params);
         //当上传到圈子/群组目录时,默认共享到该圈子/群组中
         if ($firstChar == 'c' || $firstChar == 'g') {
             $this->saveShare($fileid, "1", substr($hpost_to_dir, 1), $firstChar, 'r');
         }
         $response = new Response("{\"succeed\":1,\"fileid\":\"{$fileid}\"}");
         $response->headers->set('Content-Type', 'text/json');
         return $response;
     } catch (\Exception $e) {
         $this->get('logger')->err($e);
         $response = new Response("{\"succeed\":0}");
         $response->headers->set('Content-Type', 'text/json');
         return $response;
     }
 }