using CrazyStudio.Core.Common.AliyunExt.Oss.Tools;
using CrazyStudio.Core.Common.Eitities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace XPrintServer.Admin.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OssController : ControllerBase
    {

        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly AliyunInfos _aliyunInfos;
        public OssController(IHttpContextAccessor httpContextAccessor, AliyunInfos aliyunInfos)
        {
            _httpContextAccessor = httpContextAccessor;
            _aliyunInfos = aliyunInfos;
        }

        /// <summary>
        /// 获取OSS加密信息
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public object? GetOssInfo()
        {
            var result = StsTool.GetSTS(_aliyunInfos!, _httpContextAccessor.HttpContext!.Session.Id);
            return new
            {
                Token = result,
                Bucket = _aliyunInfos!.Oss.Bucket,
                Endpoint = _aliyunInfos.Oss.Region + ".aliyuncs.com"
            };
        }


        [HttpPost()]
        [Authorize]
        public string GetOssObjectName([FromBody]string url)
        {
            string objectName = url.Replace($"https://{_aliyunInfos.Oss.Bucket}.{_aliyunInfos.Oss.Region.Replace("-internal", "")}.aliyuncs.com/", string.Empty)
                .Replace($"http://{_aliyunInfos.Oss.Bucket}.{_aliyunInfos.Oss.Region.Replace("-internal", "")}.aliyuncs.com/", string.Empty);

            return objectName;
        }
    }
}
