Пример #1
0
 public function getAssetUrl($moduleUniqueId, $relativeAssetPath)
 {
     $targetPath = $this->assetBasePath . DIRECTORY_SEPARATOR . $this->getModulePublishRelativeDir($moduleUniqueId) . DIRECTORY_SEPARATOR . $relativeAssetPath;
     // 简单的取根目录的地址
     if (empty($relativeAssetPath)) {
         goto out;
     }
     // 智能发布资源
     $this->publishAsset($moduleUniqueId, $relativeAssetPath);
     $targetModifyTime = filemtime($targetPath);
     out:
     $hashParam = '';
     if ($this->fileHashUrl && is_file($targetPath)) {
         if (!$this->fileHashName) {
             // 只有文件才启用 hash 参数,其它不启用
             $hashParam = $relativeAssetPath . '?hash=' . $targetModifyTime;
         } else {
             // hash 文件名,而不是在后面添加参数
             $pathInfo = pathinfo($relativeAssetPath);
             $hashFile = $this->assetBasePath . DIRECTORY_SEPARATOR . $this->getModulePublishRelativeDir($moduleUniqueId) . DIRECTORY_SEPARATOR . $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $targetModifyTime . '.' . $pathInfo['basename'];
             // hash 文件不存在,复制一个
             if (!is_file($hashFile)) {
                 Utils::copyFile($targetPath, $hashFile);
             }
             // 指向 hash 文件名
             $hashParam = $pathInfo['dirname'] . '/' . $targetModifyTime . '.' . $pathInfo['basename'];
         }
     } else {
         $hashParam = $relativeAssetPath;
     }
     return $this->assetUrlPrefix . '/' . $this->getModulePublishRelativeDir($moduleUniqueId) . '/' . $hashParam;
 }
Пример #2
0
 /**
  * 复制一个文件,返回复制后文件的相对路径
  *
  * @param string $dataPathRoot
  * @param string $fileRelativePath
  *
  * @return string
  */
 public static function duplicateFile($dataPathRoot, $fileRelativePath)
 {
     $sourceFilePath = $dataPathRoot . '/' . $fileRelativePath;
     if (!is_file($sourceFilePath) || !file_exists($sourceFilePath)) {
         return '';
     }
     $pathInfoArray = pathinfo($sourceFilePath);
     $baseFileName = $pathInfoArray['basename'];
     // 截断文件名,防止文件名太长了
     if (strlen($baseFileName) > 12) {
         $baseFileName = substr($baseFileName, strlen($baseFileName) - 12);
     }
     $targetFilePath = $pathInfoArray['dirname'] . '/' . date("YmdHis") . '_' . rand(1, 10000) . '_' . $baseFileName;
     // 复制文件
     Utils::copyFile($sourceFilePath, $targetFilePath);
     // 去掉 dataRootPath,返回相对路径
     return str_replace($dataPathRoot . '/', '', $targetFilePath);
 }