|
|
<?php
|
|
|
class OssTool {
|
|
|
|
|
|
/**
|
|
|
* 根据oss相对路径,返回所在idc的绝对路径
|
|
|
*
|
|
|
* @param string $ossPath 相对路径,如果以 #jc 结尾,就表示京东云的OSS地址
|
|
|
* @param string $idc aliyun 或 jcloud
|
|
|
* @param string $bucket
|
|
|
*/
|
|
|
public static function getPubOssUrlByOssPath($ossPath, $idc = null, $bucket = null) {
|
|
|
$hostname = AppConst::getDuoDuoCloudHost(true);
|
|
|
if (preg_match('#/open/common/get_dcloud_oss_content\?objectKey=([^&]*)#', $ossPath, $matches)) {
|
|
|
$ossPath = self::getRealDcloudOssPath($matches[1]);
|
|
|
return AppConst::getCurProtocol() . "://$hostname/open/common/get_dcloud_oss_content?objectKey=$ossPath";
|
|
|
}
|
|
|
|
|
|
if (!self::checkOssPathIsLegal($ossPath)) {
|
|
|
return $ossPath;
|
|
|
}
|
|
|
|
|
|
$idc = $idc ?: OssClient::getIdcByOssUrl($ossPath);
|
|
|
if ($idc == OssConst::idcDcloud) {
|
|
|
$ossPath = self::getRealDcloudOssPath($ossPath);
|
|
|
return AppConst::getCurProtocol() . "://$hostname/open/common/get_dcloud_oss_content?objectKey=$ossPath";
|
|
|
} else {
|
|
|
return self::getAliyunPubOssUrl($bucket, $ossPath);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
protected static function getAliyunPubOssUrl($bucket, $ossPath) {
|
|
|
if (empty($bucket)) {
|
|
|
return Zc::C('honor-pub.domain') . $ossPath;
|
|
|
}
|
|
|
|
|
|
if ($bucket == Zc::C('fc-pub.bucket')) {
|
|
|
return Zc::C('fc-pub.domain'). $ossPath;
|
|
|
}
|
|
|
return Zc::C('honor-pub.domain') . $ossPath;
|
|
|
}
|
|
|
|
|
|
public static function getInternalOssUrlByOssPath($ossPath, $bucket = null) {
|
|
|
if (!AppConst::isDuoDuoCloud()) {
|
|
|
return self::getPubOssUrlByOssPath($ossPath, null, $bucket);
|
|
|
}
|
|
|
$hostname = AppConst::getDcloudTimerInternalHostname();
|
|
|
if (preg_match('#/open/common/get_dcloud_oss_content\?objectKey=([^&]*)#', $ossPath, $matches)) {
|
|
|
$ossPath = self::getRealDcloudOssPath($matches[1]);
|
|
|
return "http://$hostname/open/common/get_dcloud_oss_content?objectKey=$ossPath";
|
|
|
}
|
|
|
if (!self::checkOssPathIsLegal($ossPath)) {
|
|
|
return str_ireplace('https:', 'http:', $ossPath);
|
|
|
}
|
|
|
$idc = OssClient::getIdcByOssUrl($ossPath);
|
|
|
if ($idc == OssConst::idcDcloud) {
|
|
|
$ossPath = self::getRealDcloudOssPath($ossPath);
|
|
|
return "http://$hostname/open/common/get_dcloud_oss_content?objectKey=$ossPath";
|
|
|
} else {
|
|
|
return str_ireplace('https:', 'http:', self::getAliyunPubOssUrl($bucket, $ossPath));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private static function checkOssPathIsLegal($ossPath) {
|
|
|
if (strncasecmp($ossPath, "http:", 5) == 0) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (strncasecmp($ossPath, "https:", 6) == 0) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (strncasecmp($ossPath, "//", 2) == 0) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public static function getRealDcloudOssPath($ossPath) {
|
|
|
$ddcOssPathPrefix = OssConst::getIdcDcloudOssPathPrefix();
|
|
|
if (stripos($ossPath, $ddcOssPathPrefix) !== 0) {
|
|
|
$ossPath = $ddcOssPathPrefix . $ossPath;
|
|
|
}
|
|
|
|
|
|
return $ossPath;
|
|
|
}
|
|
|
} |