/** * */ public function delete($filename) { $abs = $this->rootDir . '/' . TMS_MODEL::toLocalEncoding($filename); if (file_exists($abs)) { return unlink($abs); } else { return false; } }
/** * 处理分段上传的请求 */ public function handleRequest($resumabled) { $chunkNumber = $resumabled['resumableChunkNumber']; $filename = str_replace(' ', '_', $resumabled['resumableFilename']); $chunkDir = $resumabled['resumableIdentifier'] . '_part'; $chunkFile = \TMS_MODEL::toLocalEncoding($filename) . '.part' . $chunkNumber; $content = base64_decode(preg_replace('/data:(.*?)base64\\,/', '', $resumabled['resumableChunkContent'])); $ret = $this->modelFs->write($chunkDir . '/' . $chunkFile, $content); if (false === $ret) { $this->_log('Error saving chunk ' . $chunkNumber . ' for file ' . $filename); } else { // check if all the parts present, and create the final destination file $absChunkDir = $this->modelFs->rootDir . '/' . $chunkDir; $this->createFileFromChunks($absChunkDir, $filename, $resumabled['resumableTotalChunks']); } }
/** * 上传文件并创建图文 */ public function articleUpload_action($mpid, $entry = null, $state = null) { if ($state === 'done') { $fan = $this->model('user/fans')->byId($this->user->fid, 'nickname'); $posted = $this->getPostJson(); $file = $posted->file; $modelFs = \TMS_APP::M('fs/local', $mpid, '_resumable'); $fileUploaded = $modelFs->rootDir . '/article_' . $file->uniqueIdentifier; $current = time(); $filename = str_replace(' ', '_', $file->name); $d = array(); $d['mpid'] = $mpid; $d['entry'] = $entry; $d['creater'] = $this->user->mid; $d['creater_name'] = $fan->nickname; $d['creater_src'] = 'M'; $d['create_at'] = $current; $d['modifier'] = $this->user->mid; $d['modifier_src'] = 'M'; $d['modifier_name'] = $fan->nickname; $d['modify_at'] = $current; $d['title'] = substr($filename, 0, strrpos($filename, '.')); $d['author'] = $fan->nickname; $d['url'] = ''; $d['hide_pic'] = 'Y'; $d['can_picviewer'] = 'Y'; $d['has_attachment'] = 'Y'; $d['pic'] = ''; $d['summary'] = ''; $d['body'] = ''; $d['finished'] = 'N'; $d['approved'] = 'N'; $d['public_visible'] = 'Y'; $d['remark_notice'] = 'Y'; $id = $this->model()->insert('xxt_article', $d, true); /** * 设置频道 */ list($entryType, $entryId) = explode(',', $entry); $entry = $this->model('matter\\' . $entryType)->byId($entryId, 'params'); $params = json_decode($entry->params); if (!empty($params->channel)) { $channelId = $params->channel; $this->model('matter\\channel')->addMatter($channelId, array('id' => $id, 'type' => 'article'), $this->user->mid, $fan->nickname, 'M'); } /** * 保存附件 */ $att = array(); $att['article_id'] = $id; $att['name'] = $filename; $att['type'] = $file->type; $att['size'] = $file->size; $att['last_modified'] = $file->lastModified; $att['url'] = 'local://article_' . $id . '_' . $filename; $this->model()->insert('xxt_article_attachment', $att, true); $modelFs = \TMS_APP::M('fs/local', $mpid, '附件'); $attachment = $modelFs->rootDir . '/article_' . $id . '_' . \TMS_MODEL::toLocalEncoding($filename); rename($fileUploaded, $attachment); /** * 获取附件的内容 */ $appRoot = $_SERVER['DOCUMENT_ROOT']; $ext = explode('.', $filename); $ext = array_pop($ext); $attAbs = $appRoot . '/' . $attachment; if (in_array($ext, array('doc', 'docx', 'ppt', 'pptx'))) { /* 存放附件转换结果 */ $attDir = str_replace('.' . $ext, '', $attachment); mkdir($appRoot . '/' . $attDir); /* 执行转换操作 */ $output = array(); $attPng = $appRoot . '/' . $attDir . '/%d.jpg'; $cmd = $appRoot . '/cus/conv2pdf2img ' . $attAbs . ' ' . $attPng; $rsp = exec($cmd, $output, $status); if ($status == 1) { return new \ResponseError('转换文件失败:' . $rsp); } $this->setBodyByAtt($id, $attDir); if (in_array($ext, array('ppt', 'pptx'))) { $this->setCoverByAtt($id, $attDir); } } else { if ($ext === 'pdf') { /* 存放附件转换结果 */ $attDir = str_replace('.' . $ext, '', $attachment); mkdir($appRoot . '/' . $attDir); $attPng = $appRoot . '/' . $attDir . '/%d.jpg'; $cmd = $appRoot . '/cus/conv2img ' . $attAbs . ' ' . $attPng; $rsp = exec($cmd); $this->setBodyByAtt($id, $attDir); } } return new \ResponseData($id); } else { /** * 分块上传文件 */ $modelFs = \TMS_APP::M('fs/local', $mpid, '_resumable'); $dest = '/article_' . $_POST['resumableIdentifier']; $resumable = new resumable($mpid, $dest, $modelFs); $resumable->handleRequest(); exit; } }
/** * 上传文件并创建图文 */ public function uploadAndCreate_action($state = null) { if ($state === 'done') { $account = \TMS_CLIENT::account(); if ($account === false) { return new \ResponseError('长时间未操作,请重新登陆!'); } $posted = $this->getPostJson(); $file = $posted->file; $current = time(); $uid = \TMS_CLIENT::get_client_uid(); $uname = $account->nickname; $filename = str_replace(' ', '_', $file->name); /* 生成图文*/ $d = array(); $d['mpid'] = $this->mpid; $d['creater'] = $uid; $d['creater_src'] = 'A'; $d['creater_name'] = $uname; $d['create_at'] = $current; $d['modifier'] = $uid; $d['modifier_src'] = 'A'; $d['modifier_name'] = $uname; $d['modify_at'] = $current; $d['title'] = substr($filename, 0, strrpos($filename, '.')); $d['author'] = $uname; $d['url'] = ''; $d['hide_pic'] = 'Y'; $d['can_picviewer'] = 'Y'; $d['has_attachment'] = 'Y'; $d['pic'] = ''; $d['summary'] = ''; $d['body'] = ''; $id = $this->model()->insert('xxt_article', $d, true); /** * 保存附件 */ $att = array(); $att['article_id'] = $id; $att['name'] = $filename; $att['type'] = $file->type; $att['size'] = $file->size; $att['last_modified'] = $file->lastModified; $att['url'] = 'local://article_' . $id . '_' . $filename; $this->model()->insert('xxt_article_attachment', $att, true); /* 处理附件 */ $modelRes = $this->model('fs/local', $this->mpid, '_resumable'); $modelAtt = $this->model('fs/local', $this->mpid, '附件'); $fileUploaded = $modelRes->rootDir . '/article_' . $file->uniqueIdentifier; $attachment = $modelAtt->rootDir . '/article_' . $id . '_' . \TMS_MODEL::toLocalEncoding($filename); if (false === rename($fileUploaded, $attachment)) { return new ResponseError('移动上传文件失败'); } /** * 获取附件的内容 */ $appRoot = $_SERVER['DOCUMENT_ROOT']; $ext = explode('.', $filename); $ext = array_pop($ext); $attAbs = $appRoot . '/' . $attachment; if (in_array($ext, array('doc', 'docx', 'ppt', 'pptx'))) { /* 存放附件转换结果 */ $attDir = str_replace('.' . $ext, '', $attachment); mkdir($appRoot . '/' . $attDir); /* 执行转换操作 */ $output = array(); $attPng = $appRoot . '/' . $attDir . '/%d.jpg'; $cmd = $appRoot . '/cus/conv2pdf2img ' . $attAbs . ' ' . $attPng; $rsp = exec($cmd, $output, $status); if ($status == 1) { return new \ResponseError('转换文件失败:' . $rsp); } $this->setBodyByAtt($id, $attDir); if (in_array($ext, array('ppt', 'pptx'))) { $this->setCoverByAtt($id, $attDir); } } else { if ($ext === 'pdf') { /* 存放附件转换结果 */ $attDir = str_replace('.' . $ext, '', $attachment); mkdir($appRoot . '/' . $attDir); $attPng = $appRoot . '/' . $attDir . '/%d.jpg'; $cmd = $appRoot . '/cus/conv2img ' . $attAbs . ' ' . $attPng; $rsp = exec($cmd); $this->setBodyByAtt($id, $attDir); } } return new \ResponseData($id); } else { /** * 分块上传文件 */ $modelFs = $this->model('fs/local', $this->mpid, '_resumable'); $dest = '/article_' . $_POST['resumableIdentifier']; $resumable = $this->model('fs/resumable', $this->mpid, $dest, $modelFs); $resumable->handleRequest($_POST); return new \ResponseData('ok'); } }