作者:liugang1989071
项目:www.zesow.co
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;
}
作者:stylin
项目:LeesPhar
public function addFile($targetType, $targetId, array $fileInfo = array(), UploadedFile $originalFile = null)
{
if (!ArrayToolkit::requireds($fileInfo, array('filename', 'key', 'size'))) {
throw $this->createServiceException('参数缺失,添加用户文件失败!');
}
$uploadFile = array();
$uploadFile['targetId'] = $targetId;
$uploadFile['targetType'] = $targetType;
$uploadFile['hashId'] = $fileInfo['key'];
$uploadFile['filename'] = $fileInfo['filename'];
$uploadFile['ext'] = pathinfo($uploadFile['filename'], PATHINFO_EXTENSION);
$uploadFile['size'] = (int) $fileInfo['size'];
$uploadFile['etag'] = empty($fileInfo['etag']) ? '' : $fileInfo['etag'];
$uploadFile['length'] = empty($fileInfo['length']) ? 0 : intval($fileInfo['length']);
$uploadFile['metas'] = $this->encodeMetas(empty($fileInfo['metas']) ? array() : $fileInfo['metas']);
$uploadFile['metas2'] = $this->encodeMetas(empty($fileInfo['metas2']) ? array() : $fileInfo['metas2']);
if (empty($fileInfo['convertHash'])) {
$uploadFile['convertHash'] = "ch-{$uploadFile['hashId']}";
$uploadFile['convertStatus'] = 'none';
$uploadFile['convertParams'] = '';
} else {
$uploadFile['convertHash'] = "{$fileInfo['convertHash']}";
$uploadFile['convertStatus'] = 'waiting';
$uploadFile['convertParams'] = $fileInfo['convertParams'];
}
$uploadFile['type'] = FileToolkit::getFileTypeByMimeType($fileInfo['mimeType']);
$uploadFile['canDownload'] = empty($uploadFile['canDownload']) ? 0 : 1;
$uploadFile['storage'] = 'cloud';
$uploadFile['createdUserId'] = $this->getCurrentUser()->id;
$uploadFile['updatedUserId'] = $uploadFile['createdUserId'];
$uploadFile['updatedTime'] = $uploadFile['createdTime'] = time();
return $uploadFile;
}
作者:lattice
项目:EduSoho_jb5
protected function createPrivateFileDownloadResponse(Request $request, $file)
{
$response = BinaryFileResponse::create($file['fullpath'], 200, array(), false);
$response->trustXSendfileTypeHeader();
$file['filename'] = urlencode($file['filename']);
if (preg_match("/MSIE/i", $request->headers->get('User-Agent'))) {
$response->headers->set('Content-Disposition', 'attachment; filename="' . $file['filename'] . '"');
} else {
$response->headers->set('Content-Disposition', "attachment; filename*=UTF-8''" . $file['filename']);
}
$mimeType = FileToolkit::getMimeTypeByExtension($file['ext']);
if ($mimeType) {
$response->headers->set('Content-Type', $mimeType);
}
return $response;
}
作者:bqx61
项目:edusoho_de
public function approvalSubmitAction(Request $request)
{
$user = $this->getCurrentUser();
if ($request->getMethod() == 'POST') {
$faceImg = $request->files->get('faceImg');
$backImg = $request->files->get('backImg');
if (!FileToolkit::isImageFile($backImg) || !FileToolkit::isImageFile($faceImg)) {
return $this->createMessageResponse('error', '上传图片格式错误,请上传jpg, bmp,gif, png格式的文件。');
}
$directory = $this->container->getParameter('topxia.upload.private_directory') . '/approval';
$this->getUserService()->applyUserApproval($user['id'], $request->request->all(), $faceImg, $backImg, $directory);
$this->setFlashMessage('success', '实名认证提交成功!');
return $this->redirect($this->generateUrl('settings'));
}
return $this->render('TopxiaWebBundle:Settings:approval.html.twig', array());
}
作者:ccq1
项目:EduSoh
public function consultUploadAction(Request $request)
{
$fileId = $request->request->get('id');
$objectFile = $this->getFileService()->getFileObject($fileId);
if (!FileToolkit::isImageFile($objectFile)) {
throw $this->createAccessDeniedException('图片格式不正确!');
}
$file = $this->getFileService()->getFile($fileId);
$parsed = $this->getFileService()->parseFileUri($file["uri"]);
$consult = $this->getSettingService()->get('consult', array());
$consult['webchatURI'] = "{$this->container->getParameter('topxia.upload.public_url_path')}/" . $parsed["path"];
$consult['webchatURI'] = ltrim($consult['webchatURI'], '/');
$this->getSettingService()->set('consult', $consult);
$this->getLogService()->info('system', 'update_settings', "更新微信二维码", array('webchatURI' => $consult['webchatURI']));
$response = array('path' => $consult['webchatURI'], 'url' => $this->container->get('templating.helper.assets')->getUrl($consult['webchatURI']));
return $this->createJsonResponse($response);
}
作者:Loyalsoldie
项目:8000wei-v
public function consultUploadAction(Request $request)
{
$file = $request->files->get('consult');
if (!FileToolkit::isImageFile($file)) {
throw $this->createAccessDeniedException('图片格式不正确!');
}
$filename = 'webchat.' . $file->getClientOriginalExtension();
$directory = "{$this->container->getParameter('topxia.upload.public_directory')}/system";
$file = $file->move($directory, $filename);
$consult = $this->getSettingService()->get('consult', array());
$consult['webchatURI'] = "{$this->container->getParameter('topxia.upload.public_url_path')}/system/{$filename}";
$consult['webchatURI'] = ltrim($consult['webchatURI'], '/');
$this->getSettingService()->set('consult', $consult);
$this->getLogService()->info('system', 'update_settings', "更新微信二维码", array('webchatURI' => $consult['webchatURI']));
$response = array('path' => $consult['webchatURI'], 'url' => $this->container->get('templating.helper.assets')->getUrl($consult['webchatURI']));
return new Response(json_encode($response));
}
作者:ZHONG-GU
项目:EduSoh
public function mobilePictureUploadAction(Request $request, $type)
{
$file = $request->files->get($type);
if (!FileToolkit::isImageFile($file)) {
throw $this->createAccessDeniedException('图片格式不正确!');
}
$filename = 'mobile_picture' . time() . '.' . $file->getClientOriginalExtension();
$directory = "{$this->container->getParameter('topxia.upload.public_directory')}/system";
$file = $file->move($directory, $filename);
$mobile = $this->getSettingService()->get('mobile', array());
$mobile[$type] = "{$this->container->getParameter('topxia.upload.public_url_path')}/system/{$filename}";
$mobile[$type] = ltrim($mobile[$type], '/');
$this->getSettingService()->set('mobile', $mobile);
$this->getLogService()->info('system', 'update_settings', "更新网校{$type}图片", array($type => $mobile[$type]));
$response = array('path' => $mobile[$type], 'url' => $this->container->get('templating.helper.assets')->getUrl($mobile[$type]));
return new Response(json_encode($response));
}
作者:ccq1
项目:EduSoh
public function uploadAction(Request $request)
{
list($groupCode, $type) = $this->tryUploadFile($request);
if (!$this->isGroup($groupCode)) {
return $this->createMessageResponse("error", "参数不正确");
}
$file = $request->files->get('file');
if ($type == 'image') {
if (!FileToolkit::isImageFile($file)) {
throw new \RuntimeException("您上传的不是图片文件,请重新上传。");
}
} else {
throw new \RuntimeException("上传类型不正确!");
}
$record = $this->getFileService()->uploadFile($groupCode, $file);
$record['url'] = $this->get('topxia.twig.web_extension')->getFilePath($record['uri']);
$request->getSession()->set("fileId", $record["id"]);
return $this->createJsonResponse($record);
}
作者:stylin
项目:LeesPhar
public function pictureAction(Request $request, $id)
{
$course = $this->getCourseService()->tryManageCourse($id);
if ($request->getMethod() == 'POST') {
$file = $request->files->get('picture');
if (!FileToolkit::isImageFile($file)) {
return $this->createMessageResponse('error', '上传图片格式错误,请上传jpg, gif, png格式的文件。');
}
$filenamePrefix = "course_{$course['id']}_";
$hash = substr(md5($filenamePrefix . time()), -8);
$ext = $file->getClientOriginalExtension();
$filename = $filenamePrefix . $hash . '.' . $ext;
$directory = $this->container->getParameter('topxia.upload.public_directory') . '/tmp';
$file = $file->move($directory, $filename);
$fileName = str_replace('.', '!', $file->getFilename());
return $this->redirect($this->generateUrl('course_manage_picture_crop', array('id' => $course['id'], 'file' => $fileName)));
}
return $this->render('TopxiaWebBundle:CourseManage:picture.html.twig', array('course' => $course));
}
作者:stylin
项目:LeesPhar
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();
$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;
}
作者:ccq1
项目:EduSoh
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;
}
作者:liugang1989071
项目:www.zesow.co
public function pictureAction(Request $request, $id)
{
$gift = $this->getGiftService()->tryManageGift($id);
if ($request->getMethod() == 'POST') {
$reponse = array();
$file = $request->files->get('picture');
if (!FileToolkit::isImageFile($file)) {
return $this->createMessageResponse('error', '上传图片格式错误,请上传jpg, gif, png格式的文件。');
}
$filenamePrefix = "gift_{$gift['id']}_";
$hash = substr(md5($filenamePrefix . time()), -8);
$ext = $file->getClientOriginalExtension();
$filename = $filenamePrefix . $hash . '.' . $ext;
$directory = $this->container->getParameter('topxia.upload.public_directory') . '/tmp';
$file = $file->move($directory, $filename);
$fileName = str_replace('.', '!', $file->getFilename());
$pictureFilePath = $this->container->getParameter('topxia.upload.public_directory') . '/tmp/' . $filename;
$this->getGiftService()->changeGiftPicture($gift['id'], $pictureFilePath);
return $this->createJsonResponse($pictureFilePath);
}
return $this->render('TopxiaAdminBundle:Gift:picture.html.twig', array('gift' => $gift));
}
作者:liugang1989071
项目:www.zesow.co
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);
}
}
作者:stylin
项目:LeesPhar
public function defaultCoursePictureAction(Request $request)
{
$file = $request->files->get('picture');
if (!FileToolkit::isImageFile($file)) {
return $this->createMessageResponse('error', '上传图片格式错误,请上传jpg, gif, png格式的文件。');
}
$filenamePrefix = "coursePicture";
$hash = substr(md5($filenamePrefix . time()), -8);
$ext = $file->getClientOriginalExtension();
$filename = $filenamePrefix . $hash . '.' . $ext;
$defaultSetting = $this->getSettingService()->get('default', array());
$defaultSetting['defaultCoursePictureFileName'] = $filename;
$this->getSettingService()->set("default", $defaultSetting);
$directory = $this->container->getParameter('topxia.upload.public_directory') . '/tmp';
$file = $file->move($directory, $filename);
$pictureFilePath = $directory . '/' . $filename;
$imagine = new Imagine();
$image = $imagine->open($pictureFilePath);
$naturalSize = $image->getSize();
$scaledSize = $naturalSize->widen(480)->heighten(270);
$pictureUrl = $this->container->getParameter('topxia.upload.public_url_path') . '/tmp/' . $filename;
return $this->render('TopxiaAdminBundle:System:default-course-picture-crop.html.twig', array('pictureUrl' => $pictureUrl, 'naturalSize' => $naturalSize, 'scaledSize' => $scaledSize));
}
作者:Loyalsoldie
项目:8000wei-v
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;
}
作者:nany
项目:EduSoh
public function uploadAvatar()
{
$groupCode = 'tmp';
$user = $this->controller->getUserByToken($this->request);
if (empty($user)) {
return $this->createErrorResponse('not_login', "您尚未登录!");
}
$file = $this->request->files->get('file');
if (empty($file)) {
return $this->createErrorResponse('error', "没有选择上传文件!");
}
if (!FileToolkit::isImageFile($file)) {
return $this->createErrorResponse('error', "您上传的不是图片文件,请重新上传。");
}
$record = $this->getFileService()->uploadFile($groupCode, $file);
if (empty($record)) {
return $this->createErrorResponse('error', "上传失败 ~请重新尝试!");
}
$host = $this->request->getSchemeAndHttpHost();
$record['url'] = $host . $this->controller->get('topxia.twig.web_extension')->getFilePath($record['uri']);
$record['createdTime'] = date('c', $record['createdTime']);
unset($record['uri']);
return $record;
}
作者:robert-li-201
项目:EduSoh
public function getImgFileMetaInfo($fileId, $scaledWidth, $scaledHeight)
{
if (empty($fileId)) {
throw $this->createServiceException("参数不正确");
}
$file = $this->getFile($fileId);
if (empty($file)) {
throw $this->createServiceException("文件不存在");
}
$parsed = $this->parseFileUri($file["uri"]);
list($naturalSize, $scaledSize) = FileToolkit::getImgInfo($parsed['fullpath'], $scaledWidth, $scaledHeight);
return array($parsed["path"], $naturalSize, $scaledSize);
}
作者:keva
项目:EduSoh
public function videoEmbedWatermarkUploadAction(Request $request)
{
$file = $request->files->get('watermark');
if (!FileToolkit::isImageFile($file)) {
throw $this->createAccessDeniedException('图片格式不正确!');
}
$filename = 'watermarkembed_' . time() . '.' . $file->getClientOriginalExtension();
$directory = "{$this->container->getParameter('topxia.upload.public_directory')}/system";
$file = $file->move($directory, $filename);
$path = "system/{$filename}";
$originFileInfo = getimagesize($file);
$filePath = $this->container->getParameter('topxia.upload.public_directory') . "/" . $path;
$imagine = new Imagine();
$rawImage = $imagine->open($filePath);
$pathinfo = pathinfo($filePath);
$specification['240'] = 20;
$specification['360'] = 30;
$specification['480'] = 40;
$specification['720'] = 60;
$specification['1080'] = 90;
foreach ($specification as $key => $value) {
$width = $originFileInfo[0] * $value / $originFileInfo[1];
$specialImage = $rawImage->copy();
$specialImage->resize(new Box($width, $value));
$filePath = "{$pathinfo['dirname']}/{$pathinfo['filename']}-{$key}.{$pathinfo['extension']}";
$specialImage->save($filePath);
}
$response = array('path' => $path, 'url' => $this->get('topxia.twig.web_extension')->getFileUrl($path));
return new Response(json_encode($response));
}
作者:liugang1989071
项目:www.zesow.co
protected function createFilesJsonResponse($files)
{
foreach ($files as &$file) {
$file['updatedTime'] = date('Y-m-d H:i', $file['updatedTime']);
$file['size'] = FileToolkit::formatFileSize($file['size']);
// Delete some file attributes to redunce the json response size
unset($file['hashId']);
unset($file['convertHash']);
unset($file['etag']);
unset($file['convertParams']);
unset($file);
}
return $this->createJsonResponse($files);
}
作者:robert-li-201
项目:EduSoh
public function getLocalVideo()
{
$fileId = $this->getParam("targetId");
$user = $this->controller->getuserByToken($this->request);
if (!$user->isLogin()) {
return $this->createErrorResponse('not_login', "您尚未登录!");
}
$file = $this->getUploadFileService()->getFile($fileId);
if (empty($file)) {
return $this->createErrorResponse('error', "视频文件不存在!");
}
$response = BinaryFileResponse::create($file['fullpath'], 200, array(), false);
$response->trustXSendfileTypeHeader();
$mimeType = FileToolkit::getMimeTypeByExtension($file['ext']);
if ($mimeType) {
$response->headers->set('Content-Type', $mimeType);
}
return $response;
}