import { fetch, post } from '@/api/http'
import { GLOBAL } from '@/static/Global.js';
//获取文件后缀和类型
function getExAndType(fileName) {
    let ex = fileName.substring(fileName.lastIndexOf('.') + 1)
    let type = 'other'
    ex = ex.toUpperCase()
    for (let key in GLOBAL.CONTENT_TYPES) {
        if (GLOBAL.CONTENT_TYPES[key].indexOf(ex) != -1) {
            type = key
            break
        }
    }
    return {
        ex, type
    }
}
export default {
    getBlobSAS: function (data) {
        return post('/api/File/getBlobSAS', data)
    },
    //最开始使用的blob授权API
    getContainerSAS: function (data) {
        return post('/api/File/getContainerSAS', data)
    },
    //获取blob容器读写创建
    blobSasRCW: function (data) {
        return post('/blob/sas-rcwld', data)
    },
    //获取blob容器只读创建
    blobSasR: function (data) {
        return post('/blob/sas-r', data)
    },
    //单文件只读权限
    urlSasR: function (data) {
        return post('/blob/sas-url-r', data)
    },
    //获取容器空间信息
    getContainerSize: function (data) {
        return post('/blob/used-space', data)
    },
    // 删除blob指定目录下的所有文件
    deletePrefix: function (data) {
        return post('/blob/delete-prefix', data)
    },
    // 内容模块批量删除blob上传记录和文件
    deleteBlobs: function (data) {
        return post('/blob/bloblog-delete', data)
    },
    /*** 新增 编辑接口
    {
        "periodId": "",
        "scope": "school",
        "name": "hbcn",
        "url": "video/xxx.png",
        "opt": "add",
    }
     {
        "scope": "school",
        "name": "hbcn",
        "opt": "del",
        "id": "19ccce98-c524-4ea7-aabc-887d1391e551"
    }
     */
    //新增或更新blob文件上传记录。
    BlobInfoMgt: function (data) {
        return post('/blob/bloblog-upsert', data)
    },
    /**
     * 查询资源信息
     * 这里统一将返回数据处理成原来前端直接listBlob返回的数据格式
     * @returns 
     */
    listBlobInfo: function (data, host) {
        return new Promise((r, j) => {
            post('/blob/bloblog-list', data).then(
                res => {
                    console.log(res)
                    let blobs = []
                    res.bloblogs.forEach(item => {
                        let i = {
                            url: host + '/' + data.name + '/' + item.url,
                            blob: '/' + item.url,
                            // name: blobName.substring(blobName.lastIndexOf('/') + 1),
                            size: item.size,
                            createTime: item.time,
                            // extension: info.ex,
                            // type: info.type,
                            periodId: item.periodId,
                            subjectId: item.subjectId,
                            gradeId: item.gradeId,
                            id: item.id
                        }
                        if (item.type == 'res') {
                            let b = item.url.replace('/index.json','.HTEX')
                            i.type = 'res',
                            i.extension = 'HTEX',
                            i.name = b.substring(b.lastIndexOf('/') + 1)
                        } else {
                            let blobName = item.url
                            let info = getExAndType(item.url)
                            i.name = blobName.substring(blobName.lastIndexOf('/') + 1)
                            i.extension = info.ex
                            i.type = info.type
                        }

                        blobs.push(i)
                    })
                    r(blobs)
                },
                err => {
                    j(err)
                }
            )

        })

        // return post('/blob/bloblog-list', data)
    },
    blobRename: function (data) {
        return post('/blob/bloblog-rename', data)
    },
}
