public function uploadFile($group, File $file, $target = null)
 {
     $errors = FileToolkit::validateFileExtension($file);
     if ($errors) {
         @unlink($file->getRealPath());
         throw $this->createServiceException("该文件格式,不允许上传。");
     }
     $group = $this->getGroupDao()->findGroupByCode($group);
     $user = $this->getCurrentUser();
     if (!$user->isLogin()) {
         throw $this->createServiceException("用户尚未登录。");
     }
     $record = array();
     $record['userId'] = $user['id'];
     $record['groupId'] = $group['id'];
     // @todo fix it.
     $record['mime'] = '';
     // $record['mime'] = $file->getMimeType();
     $record['size'] = $file->getSize();
     $record['uri'] = $this->generateUri($group, $file);
     $record['createdTime'] = time();
     $record = $this->getFileDao()->addFile($record);
     $record['file'] = $this->saveFile($file, $record['uri']);
     return $record;
 }
 public function addFile(array $fileInfo = array(), UploadedFile $originalFile = null)
 {
     $errors = FileToolkit::validateFileExtension($originalFile);
     if ($errors) {
         @unlink($originalFile->getRealPath());
         throw $this->createServiceException("该文件格式,不允许上传。");
     }
     $uploadFile = array();
     $uploadFile['storage'] = 'local';
     $uploadFile['filename'] = $originalFile->getClientOriginalName();
     $uploadFile['ext'] = FileToolkit::getFileExtension($originalFile);
     $uploadFile['size'] = $originalFile->getSize();
     $filename = FileToolkit::generateFilename($uploadFile['ext']);
     $uploadFile['hashId'] = "course/{$filename}";
     $uploadFile['convertHash'] = "ch-{$uploadFile['hashId']}";
     $uploadFile['convertStatus'] = 'none';
     $uploadFile['type'] = FileToolkit::getFileTypeByExtension($uploadFile['ext']);
     $uploadFile['isPublic'] = empty($fileInfo['isPublic']) ? 0 : 1;
     $uploadFile['canDownload'] = empty($uploadFile['canDownload']) ? 0 : 1;
     $uploadFile['updatedUserId'] = $uploadFile['createdUserId'] = $this->getCurrentUser()->id;
     $uploadFile['updatedTime'] = $uploadFile['createdTime'] = time();
     $targetPath = $this->getFilePath('course', $uploadFile['isPublic']);
     $originalFile->move($targetPath, $filename);
     return $uploadFile;
 }
 public function validateExcelFile($file)
 {
     $errorMessage = '';
     if (!is_object($file)) {
         $errorMessage = '请选择上传的文件';
         return $errorMessage;
     }
     if (FileToolkit::validateFileExtension($file, 'xls xlsx')) {
         $errorMessage = 'Excel格式不正确!';
         return $errorMessage;
     }
     $this->excelAnalyse($file);
     if ($this->rowTotal > 1000) {
         $message = 'Excel超过1000行数据!';
         return $errorMessage;
     }
     if (!$this->checkNecessaryFields($this->excelFields)) {
         $message = '缺少必要的字段';
         return $errorMessage;
     }
     return $errorMessage;
 }
 public function uploadAction(Request $request)
 {
     try {
         $token = $request->query->get('token');
         $maker = new UploadToken();
         $token = $maker->parse($token);
         if (empty($token)) {
             throw new \RuntimeException("上传授权码已过期,请刷新页面后重试!");
         }
         $file = $request->files->get('upload');
         if ($token['type'] == 'image') {
             if (!FileToolkit::isImageFile($file)) {
                 throw new \RuntimeException("您上传的不是图片文件,请重新上传。");
             }
         } elseif ($token['type'] == 'flash') {
             $errors = FileToolkit::validateFileExtension($file, 'swf');
             if (!empty($errors)) {
                 throw new \RuntimeException("您上传的不是Flash文件,请重新上传。");
             }
         } else {
             throw new \RuntimeException("上传类型不正确!");
         }
         $record = $this->getFileService()->uploadFile($token['group'], $file);
         $funcNum = $request->query->get('CKEditorFuncNum');
         $url = $this->get('topxia.twig.web_extension')->getFilePath($record['uri']);
         if ($token['type'] == 'image') {
             $response = "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction({$funcNum}, '{$url}', function(){ this._.dialog.getParentEditor().insertHtml('<img src=\"{$url}\">'); this._.dialog.hide(); return false; });</script>";
         } else {
             $response = "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction({$funcNum}, '{$url}');</script>";
         }
         return new Response($response);
     } catch (\Exception $e) {
         $message = $e->getMessage();
         $funcNum = $request->query->get('CKEditorFuncNum');
         $response = "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction({$funcNum}, '', '{$message}');</script>";
         return new Response($response);
     }
 }
 public function addFile($targetType, $targetId, array $fileInfo = array(), UploadedFile $originalFile = null)
 {
     $errors = FileToolkit::validateFileExtension($originalFile);
     if ($errors) {
         @unlink($originalFile->getRealPath());
         throw $this->createServiceException("该文件格式,不允许上传。");
     }
     $uploadFile = array();
     $time = time();
     $uploadFile['storage'] = 'local';
     $uploadFile['targetId'] = $targetId;
     $uploadFile['targetType'] = $targetType;
     $uploadFile['filename'] = $originalFile->getClientOriginalName();
     //$uploadFile['filename'] = $time.$uploadFile['filename'];
     $uploadFile['ext'] = FileToolkit::getFileExtension($originalFile);
     $uploadFile['size'] = $originalFile->getSize();
     $filename = FileToolkit::generateFilename($uploadFile['ext']);
     $uploadFile['hashId'] = "{$uploadFile['targetType']}/{$uploadFile['targetId']}/{$filename}";
     $uploadFile['convertHash'] = "ch-{$uploadFile['hashId']}";
     $uploadFile['convertStatus'] = 'none';
     $uploadFile['type'] = FileToolkit::getFileTypeByExtension($uploadFile['ext']);
     $uploadFile['isPublic'] = empty($fileInfo['isPublic']) ? 0 : 1;
     $uploadFile['canDownload'] = empty($uploadFile['canDownload']) ? 0 : 1;
     $uploadFile['updatedUserId'] = $uploadFile['createdUserId'] = $this->getCurrentUser()->id;
     $uploadFile['updatedTime'] = $uploadFile['createdTime'] = time();
     //$uploadFile['filePath'] = $this->getFilePath();
     //throw new \RuntimeException(print_r('ssd'));
     //throw $this->createServiceException(print_r('ssd'));
     //$key = $uploadFile['filename'];
     //qiniu();
     //转存上传临时文件到资源文件夹(由于业务服务器硬盘较小,故不作备份,直接上传到七牛云。如需开启则删除下面注释符) 以下代码未包含删备份
     $targetPath = $this->getFilePath($targetType, $targetId, $uploadFile['isPublic']);
     $originalFile->move($targetPath, $filename);
     //todo 上传到七牛处理代码(存储、转码、拼接、水印)20151224
     //$config = $this->getQiniuService()->initQiniu($targetPath,$filename);
     return $uploadFile;
 }
 public function uploadAction(Request $request)
 {
     $group = $request->query->get('group');
     $file = $this->get('request')->files->get('file');
     if (!is_object($file)) {
         throw $this->createNotFoundException('上传文件不能为空!');
     }
     if (filesize($file) > 1024 * 1024 * 2) {
         throw $this->createNotFoundException('上传文件大小不能超过2MB!');
     }
     if (FileToolkit::validateFileExtension($file, 'png jpg gif doc xls txt rar zip')) {
         throw $this->createNotFoundException('文件类型不正确!');
     }
     $record = $this->getFileService()->uploadFile($group, $file);
     //$record['url'] = $this->get('topxia.twig.web_extension')->getFilePath($record['uri']);
     unset($record['uri']);
     $record['name'] = $file->getClientOriginalName();
     return new Response(json_encode($record));
 }