All files / src image.ts

56.57% Statements 56/99
42.55% Branches 40/94
57.14% Functions 4/7
57.73% Lines 56/97

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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 1971x 1x                                                                                           3x 3x 3x     3x     1x 1x       5x   1x       1x 1x 1x 1x 1x 1x 1x   1x       1x 1x 8x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x       1x 2x 2x       2x 2x 1x     1x 1x 1x     1x     1x                     4x   1x 1x 1x 1x 1x 1x   1x       1x           1x         1x                                                                                    
import { request } from './utils'
import { urlSafeBase64Encode } from './base64'
 
export interface ImageViewOptions {
  mode: number
  format?: string
  w?: number
  h?: number
  q?: number
}
 
export interface ImageWatermark {
  image: string
  mode: number
  fontsize?: number
  dissolve?: number
  dx?: number
  dy?: number
  gravity?: string
  text?: string
  font?: string
  fill?: string
}
 
export interface ImageMogr2 {
  'auto-orient'?: boolean
  strip?: boolean
  thumbnail?: number
  crop?: number
  gravity?: number
  format?: number
  blur?: number
  quality?: number
  rotate?: number
}
 
type Pipeline =
  | (ImageWatermark & { fop: 'watermark' })
  | (ImageViewOptions & { fop: 'imageView2' })
  | (ImageMogr2 & { fop: 'imageMogr2' })
 
export interface Entry {
  domain: string
  key: string
}
 
function getImageUrl(key: string, domain: string) {
  key = encodeURIComponent(key)
  Eif (domain.slice(domain.length - 1) !== '/') {
    domain += '/'
  }
 
  return domain + key
}
 
export function imageView2(op: ImageViewOptions, key?: string, domain?: string) {
  Iif (!/^\d$/.test(String(op.mode))) {
    throw 'mode should be number in imageView2'
  }
 
  const { mode, w, h, q, format } = op
 
  Iif (!w && !h) {
    throw 'param w and h is empty in imageView2'
  }
 
  let imageUrl = 'imageView2/' + encodeURIComponent(mode)
  imageUrl += w ? '/w/' + encodeURIComponent(w) : ''
  imageUrl += h ? '/h/' + encodeURIComponent(h) : ''
  imageUrl += q ? '/q/' + encodeURIComponent(q) : ''
  imageUrl += format ? '/format/' + encodeURIComponent(format) : ''
  Eif (key && domain) {
    imageUrl = getImageUrl(key, domain) + '?' + imageUrl
  }
  return imageUrl
}
 
// invoke the imageMogr2 api of Qiniu
export function imageMogr2(op: ImageMogr2, key?: string, domain?: string) {
  const autoOrient = op['auto-orient']
  const { thumbnail, strip, gravity, crop, quality, rotate, format, blur } = op
 
  let imageUrl = 'imageMogr2'
 
  imageUrl += autoOrient ? '/auto-orient' : ''
  imageUrl += thumbnail ? '/thumbnail/' + encodeURIComponent(thumbnail) : ''
  imageUrl += strip ? '/strip' : ''
  imageUrl += gravity ? '/gravity/' + encodeURIComponent(gravity) : ''
  imageUrl += quality ? '/quality/' + encodeURIComponent(quality) : ''
  imageUrl += crop ? '/crop/' + encodeURIComponent(crop) : ''
  imageUrl += rotate ? '/rotate/' + encodeURIComponent(rotate) : ''
  imageUrl += format ? '/format/' + encodeURIComponent(format) : ''
  imageUrl += blur ? '/blur/' + encodeURIComponent(blur) : ''
  Eif (key && domain) {
    imageUrl = getImageUrl(key, domain) + '?' + imageUrl
  }
  return imageUrl
}
 
// invoke the watermark api of Qiniu
export function watermark(op: ImageWatermark, key?: string, domain?: string) {
  const mode = op.mode
  Iif (!mode) {
    throw "mode can't be empty in watermark"
  }
 
  let imageUrl = 'watermark/' + mode
  if (mode !== 1 && mode !== 2) {
    throw 'mode is wrong'
  }
 
  Eif (mode === 1) {
    const image = op.image
    Iif (!image) {
      throw "image can't be empty in watermark"
    }
    imageUrl += image ? '/image/' + urlSafeBase64Encode(image) : ''
  }
 
  Iif (mode === 2) {
    const { text, font, fontsize, fill } = op
    if (!text) {
      throw "text can't be empty in watermark"
    }
    imageUrl += text ? '/text/' + urlSafeBase64Encode(text) : ''
    imageUrl += font ? '/font/' + urlSafeBase64Encode(font) : ''
    imageUrl += fontsize ? '/fontsize/' + fontsize : ''
    imageUrl += fill ? '/fill/' + urlSafeBase64Encode(fill) : ''
  }
 
  const { dissolve, gravity, dx, dy } = op
 
  imageUrl += dissolve ? '/dissolve/' + encodeURIComponent(dissolve) : ''
  imageUrl += gravity ? '/gravity/' + encodeURIComponent(gravity) : ''
  imageUrl += dx ? '/dx/' + encodeURIComponent(dx) : ''
  imageUrl += dy ? '/dy/' + encodeURIComponent(dy) : ''
  Eif (key && domain) {
    imageUrl = getImageUrl(key, domain) + '?' + imageUrl
  }
  return imageUrl
}
 
// invoke the imageInfo api of Qiniu
export function imageInfo(key: string, domain: string) {
  const url = getImageUrl(key, domain) + '?imageInfo'
  return request(url, { method: 'GET' })
}
 
// invoke the exif api of Qiniu
export function exif(key: string, domain: string) {
  const url = getImageUrl(key, domain) + '?exif'
  return request(url, { method: 'GET' })
}
 
export function pipeline(arr: Pipeline[], key?: string, domain?: string) {
  const isArray = Object.prototype.toString.call(arr) === '[object Array]'
  let option: Pipeline
  let errOp = false
  let imageUrl = ''
  if (isArray) {
    for (let i = 0, len = arr.length; i < len; i++) {
      option = arr[i]
      if (!option.fop) {
        throw "fop can't be empty in pipeline"
      }
      switch (option.fop) {
        case 'watermark':
          imageUrl += watermark(option) + '|'
          break
        case 'imageView2':
          imageUrl += imageView2(option) + '|'
          break
        case 'imageMogr2':
          imageUrl += imageMogr2(option) + '|'
          break
        default:
          errOp = true
          break
      }
      if (errOp) {
        throw 'fop is wrong in pipeline'
      }
    }
 
    if (key && domain) {
      imageUrl = getImageUrl(key, domain) + '?' + imageUrl
      const length = imageUrl.length
      if (imageUrl.slice(length - 1) === '|') {
        imageUrl = imageUrl.slice(0, length - 1)
      }
    }
    return imageUrl
  }
 
  throw "pipeline's first param should be array"
}