FileService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service;
  15. use think\facade\Cache;
  16. class FileService
  17. {
  18. /**
  19. * @notes 补全路径
  20. * @param string $uri
  21. * @param string $type
  22. * @return string
  23. * @author 段誉
  24. * @date 2021/12/28 15:19
  25. * @remark
  26. * 场景一:补全域名路径,仅传参$uri;
  27. * 例: FileService::getFileUrl('uploads/img.png');
  28. * 返回 http://www.likeadmin.localhost/uploads/img.png
  29. *
  30. * 场景二:补全获取web根目录路径, 传参$uri 和 $type = public_path;
  31. * 例: FileService::getFileUrl('uploads/img.png', 'public_path');
  32. * 返回 /project-services/likeadmin/server/public/uploads/img.png
  33. *
  34. * 场景三:获取当前储存方式的域名
  35. * 例: FileService::getFileUrl();
  36. * 返回 http://www.likeadmin.localhost/
  37. */
  38. public static function getFileUrl(string $uri = '', string $type = '') : string
  39. {
  40. if (strstr($uri, 'http://')) return $uri;
  41. if (strstr($uri, 'https://')) return $uri;
  42. $default = Cache::get('STORAGE_DEFAULT');
  43. if (!$default) {
  44. $default = ConfigService::get('storage', 'default', 'local');
  45. Cache::set('STORAGE_DEFAULT', $default);
  46. }
  47. if ($default === 'local') {
  48. if($type == 'public_path') {
  49. return public_path(). $uri;
  50. }
  51. $domain = request()->domain();
  52. } else {
  53. $storage = Cache::get('STORAGE_ENGINE');
  54. if (!$storage) {
  55. $storage = ConfigService::get('storage', $default);
  56. Cache::set('STORAGE_ENGINE', $storage);
  57. }
  58. $domain = $storage ? $storage['domain'] : '';
  59. }
  60. return self::format($domain, $uri);
  61. }
  62. /**
  63. * @notes 转相对路径
  64. * @param $uri
  65. * @return mixed
  66. * @author 张无忌
  67. * @date 2021/7/28 15:09
  68. */
  69. public static function setFileUrl($uri)
  70. {
  71. $default = ConfigService::get('storage', 'default', 'local');
  72. if ($default === 'local') {
  73. $domain = request()->domain();
  74. return str_replace($domain.'/', '', $uri);
  75. } else {
  76. $storage = ConfigService::get('storage', $default);
  77. return str_replace($storage['domain'].'/', '', $uri);
  78. }
  79. }
  80. /**
  81. * @notes 格式化url
  82. * @param $domain
  83. * @param $uri
  84. * @return string
  85. * @author 段誉
  86. * @date 2022/7/11 10:36
  87. */
  88. public static function format($domain, $uri)
  89. {
  90. // 处理域名
  91. $domainLen = strlen($domain);
  92. $domainRight = substr($domain, $domainLen -1, 1);
  93. if ('/' == $domainRight) {
  94. $domain = substr_replace($domain,'',$domainLen -1, 1);
  95. }
  96. // 处理uri
  97. $uriLeft = substr($uri, 0, 1);
  98. if('/' == $uriLeft) {
  99. $uri = substr_replace($uri,'',0, 1);
  100. }
  101. return trim($domain) . '/' . trim($uri);
  102. }
  103. }