/*
 * @Author: Gui
 * @Date: 2023-03-01 19:20:44
 * @LastEditors: guicheng 1625811865@qq.com
 * @LastEditTime: 2024-06-20 14:43:25
 * @Description: tel files
 * @filePath:
 */
import { http } from "@/utils/http";
import OSS from "@/assets/uploadOss/uploadfile.js";
import md5 from "md5";
import { getConfig } from "@/config";

type Res = {
  status?: string;
  info?: string;
  other?: any;
  data?: Data;
};
type Data = {
  AccessId?: string;
  AccessKey?: string;
};

const Ossupload = function (data, Filename = "TelAdmin/upload") {
  // eslint-disable-next-line no-async-promise-executor
  return new Promise(async (r, j) => {
    const res: Res = await http.post(
      "http://localhost:5800/api/ossparam?t=",
      {}
    );
    const client = new OSS({
      region: "oss-cn-chengdu",
      accessKeyId: res.data.AccessId,
      accessKeySecret: res.data.AccessKey,
      bucket: "telaidian-oss"
    });
    const headers = {};
    const filetype = data.name.split(".")[data.name.split(".").length - 1];
    const filename =
      md5(new Date().getTime() + data.size).substring(8, 24) + "." + filetype;
    try {
      const result = await client.put(
        `${Filename}/${filetype}/${new Date().getFullYear()}/${new Date().getMonth() + 1
        }/${new Date().getDate()}/${filename}`,
        data,
        { headers }
      );
      r({
        url: getConfig().UploadUrl + "/" + result.name,
        uploadurl: "/" + result.name,
        describe: data.name
      });
    } catch (e) {
      j(e);
    }
  });
};
export default Ossupload;
