import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { FileInfo } from "./types";
import { v4 as uuidv4 } from "uuid";

/**
 * 上传文件
 *
 * @param file
 */
export function uploadFileApi(file: File): AxiosPromise<FileInfo> {
  const formData = new FormData();
  formData.append("file", file);

  formData.append("fileName", `${uuidv4()}.` + file.name.split(".").pop());

  return request({
    url: "/api/v1/base/upload",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}

/**
 * 删除文件
 *
 * @param filePath 文件完整路径
 */
export function deleteFileApi(filePath?: string) {
  return request({
    url: "/api/v1/files",
    method: "delete",
    params: { filePath: filePath },
  });
}
