Example #1
0
 /**
  * 获得文件内容同时支持迷你存储
  * @param string $signature
  * @param string $fileName
  * @param string $contentType
  * @param bool $forceDownload
  * @throws MFilesException
  */
 public function getContentBySignature($signature, $fileName, $contentType, $forceDownload = true)
 {
     //下载文件的hook
     $data = array();
     $data["signature"] = $signature;
     $data["file_name"] = $fileName;
     $data["mime_type"] = $contentType;
     //对网页的处理分为2种逻辑,1种是直接显示内容,1种是文件直接下载
     $data["force_download"] = $forceDownload;
     $retData = apply_filters("file_download_url", $data);
     if ($retData !== $data && !empty($retData)) {
         header("HTTP/1.1 " . MConst::HTTP_CODE_301 . " Moved Permanently");
         header("Location: " . $retData);
         exit;
         return;
     }
     $filePath = MiniUtil::getPathBySplitStr($signature);
     $oldPath = $filePath . "/" . $signature;
     //data源处理对象
     $dataObj = Yii::app()->data;
     if ($dataObj->exists($oldPath)) {
         //兼容1.4老数据
         $filePath = $oldPath;
     } else {
         if ($dataObj->exists($filePath) === false) {
             throw new MFilesException(Yii::t('api', MConst::NOT_FOUND), MConst::HTTP_CODE_404);
         }
     }
     // 检查是否输出
     if (headers_sent()) {
         exit;
     }
     MiniUtil::outContent($filePath, $contentType, $fileName, $forceDownload);
     exit;
 }