// @ts-ignore
/* eslint-disable */

// 该文件自动生成，请勿手动修改！

// --------------------------------------------------------------------------
// 资源文件管理服务
// --------------------------------------------------------------------------

import { request, RequestOptions } from '@umijs/max';
import contentDisposition from 'content-disposition';

/** 上传资源文件 POST /api/file/upload */
export async function upload(data: FormData, options?: { [key: string]: any }) {
    const url = '/api/file/upload';
    const config = { method: 'POST', data, ...(options || {}) };
    const res = await request<API.ResponseType<API.ResourceFileOutput>>(url, config);
    return res?.data;
}

/** 根据文件ID下载文件 GET /api/file/download */
export async function download(
    params: {
        /**  */
        id: string;
    },
    options?: { [key: string]: any },
) {
    const url = '/api/file/download';
    const config = {
        method: 'GET',
        params,
        ...(options || {}),
        responseType: 'blob',
        getResponse: true,
    } as RequestOptions;
    const res = await request(url, config);
    const hcd = res.request.getResponseHeader('Content-Disposition');
    if (!hcd) {
        return null;
    }
    let fileName = '';
    const cd = contentDisposition.parse(hcd);
    if (cd?.parameters?.filename) {
        fileName = cd?.parameters?.filename;
    }
    return { fileName, data: res.data as Blob } as API.FileResponse;
}

/** 通过URL GET /api/file/view */
export async function view(
    params: {
        /**  */
        id: string;
    },
    options?: { [key: string]: any },
) {
    const url = '/api/file/view';
    const config = { method: 'GET', params, ...(options || {}) };
    const res = await request<API.ResponseType<any>>(url, config);
    return res?.data;
}

export default {
    /** 上传资源文件 POST /api/file/upload */
    upload,
    /** 根据文件ID下载文件 GET /api/file/download */
    download,
    /** 通过URL GET /api/file/view */
    view,
};
