
using Microsoft.AspNetCore.Mvc;
using QYZH.InteractiveMagazine.Infrastructure.OSS;
using QYZH.InteractiveMagazine.Models.Dto;
using Yitter.IdGenerator;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace QYZH.InteractiveMagazine.WebApi.Controllers.SDK
{
    public class OssController : SDKBaseController
    {
        private readonly OssService _ossService;

        public OssController(OssService ossService)
        {
            this._ossService = ossService;
        }

        /// <summary>
        /// 获取StsToken
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("sts")]
        public async Task<IActionResult> Credentials()
        {
            var data = await _ossService.GetSTSToken();
            return Ok(BaseResponse<Dictionary<string, string>>.Success(data));
        }

        /// <summary>
        /// 删除oss文件
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("delete")]
        public async Task<IActionResult> DelObject(string key)
        {
            // 查看用户是否有权删除
            var ok = _ossService.DeleteObject(key);
            return ApiResult(ok, "删除文件失败");
        }

        /// <summary>
        /// 获取上传路径前缀
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("getUploadPrefix")]
        public async Task<BaseResponse> GetUploadPrefix()
        {
            return BaseResponse<string>.Success($"temp/{YitIdHelper.NextId()}");
        }
    }
}
