<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service;


/**
 * 存储路径服务
 * @package app\service
 */
class StorageService
{
     /**
     * 转换为公共URL
     * @param string $savename
     * @return string
     */
    public static function toPublicUrl(string $savename): string
    {
        $normalized = str_replace('\\', '/', (string)$savename);
        $normalized = ltrim($normalized, '/');
        if (str_starts_with($normalized, 'public/')) $normalized = substr($normalized, 7);
        if (str_starts_with($normalized, 'storage/')) $normalized = substr($normalized, 8);
        return '/storage/' . $normalized;
    }

     /**
     * 规范存储路径
     * @param string $url
     * @return string
     */
    public static function normalizeStoragePath(string $url): string
    {
        $raw = trim($url);
        if ($raw === '') return '';
        $raw = str_replace('\\', '/', $raw);
        $raw = str_replace('/storage/proof/proof/', '/storage/proof/', $raw);
        $raw = str_replace('/storage/verify/verify/', '/storage/verify/', $raw);
        $raw = str_replace('/storage/avatar/avatar/', '/storage/avatar/', $raw);
        return $raw;
    }

     /**
     * 转换为完整资产URL
     * @param \think\Request $request
     * @param string $url
     * @return string
     */
    public static function toFullAssetUrl(\think\Request $request, string $url): string
    {
        $raw = self::normalizeStoragePath($url);
        if ($raw === '') return '';
        if (str_starts_with($raw, 'data:') || str_starts_with($raw, 'blob:') || str_starts_with($raw, 'file:')) return $raw;
        $xfpRaw = (string)$request->header('x-forwarded-proto');
        $xfp = strtolower(trim(explode(',', $xfpRaw)[0] ?? ''));
        if ($xfp !== 'https' && $xfp !== 'http') $xfp = '';

        if (str_starts_with($raw, 'http://') || str_starts_with($raw, 'https://')) {
            if ($xfp === 'https' && str_starts_with($raw, 'http://')) return 'https://' . substr($raw, 7);
            if ($xfp === 'http' && str_starts_with($raw, 'https://')) return 'http://' . substr($raw, 8);
            return $raw;
        }
        if (preg_match('/^(https?:)?\/\//', $raw) === 1) return $raw;

        if (str_starts_with($raw, '/')) {
            $xfhRaw = (string)$request->header('x-forwarded-host');
            $xfh = trim(explode(',', $xfhRaw)[0] ?? '');
            if ($xfh !== '' && $xfp !== '') {
                return $xfp . '://' . $xfh . $raw;
            }

            $domain = (string)$request->domain();
            if ($xfp === 'https' && str_starts_with($domain, 'http://')) {
                $domain = 'https://' . substr($domain, 7);
            } elseif ($xfp === 'http' && str_starts_with($domain, 'https://')) {
                $domain = 'http://' . substr($domain, 8);
            }
            return rtrim($domain, '/') . $raw;
        }
        return $raw;
    }

     /**
     * 规范附件值
     * @param string $value
     * @return string
     */
    public static function normalizeAttachmentValue(string $value): string
    {
        $raw = trim($value);
        if ($raw === '') return '';
        $raw = str_replace('\\', '/', $raw);
        if (preg_match('/^https?:\/\//i', $raw) === 1) {
            $parts = parse_url($raw);
            $path = (string)($parts['path'] ?? '');
            $raw = $path !== '' ? $path : $raw;
        }
        $raw = self::normalizeStoragePath($raw);
        if (!str_starts_with($raw, '/')) $raw = '/' . ltrim($raw, '/');
        if (!str_starts_with($raw, '/storage/')) {
            $raw = '/storage/' . ltrim($raw, '/');
        }
        return $raw;
    }
}
