# All files / src api.ts

63.89% Statements 23/36
50% Branches 6/12
28.57% Functions 2/7
62.86% Lines 22/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    1521x
    1x
    1x

    1x
    1x
    1x
    1x

    1x
    5x

    5x
    1x

    4x
    3x
    3x
    3x

    1x
    1x
    1x

    1x

    1x

    1x

    1x

     import * as utils from './utils'
    import { regionUphostMap } from './config'
    import { urlSafeBase64Encode } from './base64'
    import { Config, UploadInfo } from './upload'

    interface UpHosts {
      data: {
        up: {
          acc: {
            main: string[]
          }
        }
      }
    }

    export async function getUpHosts(token: string, protocol: 'https:' | 'http:'): Promise<UpHosts> {
      const putPolicy = utils.getPutPolicy(token)
      const url = protocol + '//api.qiniu.com/v2/query?ak=' + putPolicy.ak + '&bucket=' + putPolicy.bucket
      return utils.request(url, { method: 'GET' })
    }

    export type UploadUrlConfig = Partial<Pick<Config, 'upprotocol' | 'uphost' | 'region' | 'useCdnDomain'>>

    /** 获取上传url */
    export async function getUploadUrl(config: UploadUrlConfig, token: string): Promise<string> {
      const protocol = config.upprotocol || 'https:'

      if (config.uphost) {
        return `${protocol}//${config.uphost}`
      }

      if (config.region) {
        const upHosts = regionUphostMap[config.region]
        const host = config.useCdnDomain ? upHosts.cdnUphost : upHosts.srcUphost
        return `${protocol}//${host}`
      }

      const res = await getUpHosts(token, protocol)
      const hosts = res.data.up.acc.main
      return `${protocol}//${hosts[0]}`
    }

    /**
     * @param bucket 空间名
     * @param key 目标文件名
     * @param uploadInfo 上传信息
     */
    function getBaseUrl(bucket: string, key: string | null | undefined, uploadInfo: UploadInfo) {
      const { url, id } = uploadInfo
      return `${url}/buckets/${bucket}/objects/${key != null ? urlSafeBase64Encode(key) : '~'}/uploads/${id}`
    }

    export interface InitPartsData {
      /** 该文件的上传 id， 后续该文件其他各个块的上传，已上传块的废弃，已上传块的合成文件，都需要该 id */
      uploadId: string
      /** uploadId 的过期时间 */
      expireAt: number
    }

    /**
     * @param token 上传鉴权凭证
     * @param bucket 上传空间
     * @param key 目标文件名
     * @param uploadUrl 上传地址
     */
    export function initUploadParts(
      token: string,
      bucket: string,
      key: string | null | undefined,
      uploadUrl: string
    ): utils.Response<InitPartsData> {
      const url = `${uploadUrl}/buckets/${bucket}/objects/${key != null ? urlSafeBase64Encode(key) : '~'}/uploads`
      return utils.request<InitPartsData>(
        url,
        {
          method: 'POST',
          headers: utils.getAuthHeaders(token)
        }
      )
    }

    export interface UploadChunkData {
      etag: string
      md5: string
    }

    /**
     * @param token 上传鉴权凭证
     * @param index 当前 chunk 的索引
     * @param uploadInfo 上传信息
     * @param options 请求参数
     */
    export function uploadChunk(
      token: string,
      key: string | null | undefined,
      index: number,
      uploadInfo: UploadInfo,
      options: Partial<utils.RequestOptions>
    ): utils.Response<UploadChunkData> {
      const bucket = utils.getPutPolicy(token).bucket
      const url = getBaseUrl(bucket, key, uploadInfo) + `/${index}`
      return utils.request<UploadChunkData>(url, {
        ...options,
        method: 'PUT',
        headers: utils.getHeadersForChunkUpload(token)
      })
    }

    export type UploadCompleteData = any

    /**
     * @param token 上传鉴权凭证
     * @param key 目标文件名
     * @param uploadInfo 上传信息
     * @param options 请求参数
     */
    export function uploadComplete(
      token: string,
      key: string | null | undefined,
      uploadInfo: UploadInfo,
      options: Partial<utils.RequestOptions>
    ): utils.Response<UploadCompleteData> {
      const bucket = utils.getPutPolicy(token).bucket
      const url = getBaseUrl(bucket, key, uploadInfo)
      return utils.request<UploadCompleteData>(url, {
        ...options,
        method: 'POST',
        headers: utils.getHeadersForMkFile(token)
      })
    }

    /**
     * @param token 上传鉴权凭证
     * @param key 目标文件名
     * @param uploadInfo 上传信息
     */
    export function deleteUploadedChunks(
      token: string,
      key: string | null | undefined,
      uploadinfo: UploadInfo
    ): utils.Response<void> {
      const bucket = utils.getPutPolicy(token).bucket
      const url = getBaseUrl(bucket, key, uploadinfo)
      return utils.request(
        url,
        {
          method: 'DELETE',
          headers: utils.getAuthHeaders(token)
        }
      )
    }