file.js 2.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import {
  hasOwn
} from 'uni-shared'

fxy060608's avatar
fxy060608 已提交
5
/**
6 7 8 9 10
 * 暂存的文件对象
 */
const files = {}
/**
 * 从url读取File
fxy060608's avatar
fxy060608 已提交
11
 * @param {string} url
12
 * @param {Promise}
fxy060608's avatar
fxy060608 已提交
13
 */
14 15 16 17
export function urlToFile (url) {
  var file = files[url]
  if (file) {
    return Promise.resolve(file)
fxy060608's avatar
fxy060608 已提交
18
  }
19
  if (/^data:[a-z-]+\/[a-z-]+;base64,/.test(url)) {
20
    return Promise.resolve(base64ToFile(url))
21 22 23 24 25 26 27 28 29 30 31
  }
  return new Promise((resolve, reject) => {
    var xhr = new XMLHttpRequest()
    xhr.open('GET', url, true)
    xhr.responseType = 'blob'
    xhr.onload = function () {
      resolve(this.response)
    }
    xhr.onerror = reject
    xhr.send()
  })
fxy060608's avatar
fxy060608 已提交
32 33
}
/**
34
 * base64转File
fxy060608's avatar
fxy060608 已提交
35
 * @param {string} base64
36
 * @return {File}
fxy060608's avatar
fxy060608 已提交
37
 */
38
export function base64ToFile (base64) {
fxy060608's avatar
fxy060608 已提交
39 40 41 42 43 44 45 46
  base64 = base64.split(',')
  var type = base64[0].match(/:(.*?);/)[1]
  var str = atob(base64[1])
  var n = str.length
  var array = new Uint8Array(n)
  while (n--) {
    array[n] = str.charCodeAt(n)
  }
47 48 49 50 51 52 53 54 55 56 57
  return blobToFile(array, type)
}
/**
 * 简易获取扩展名
 * @param {string} type
 * @return {string}
 */
function getExtname (type) {
  const extname = type.split('/')[1]
  return extname ? `.${extname}` : ''
}
58 59 60 61 62 63 64 65 66 67
/**
 * 简易获取文件名
 * @param {*} url
 */
export function getFileName (url) {
  url = url.split('#')[0].split('?')[0]
  const array = url.split('/')
  return array[array.length - 1]
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/**
 * blob转File
 * @param {Blob} blob
 * @param {string} type
 * @return {File}
 */
export function blobToFile (blob, type) {
  if (!(blob instanceof File)) {
    type = type || blob.type || ''
    const filename = `${Date.now()}${getExtname(type)}`
    try {
      blob = new File([blob], filename, { type })
    } catch (error) {
      blob = blob instanceof Blob ? blob : new Blob([blob], { type })
      blob.name = blob.name || filename
    }
  }
  return blob
fxy060608's avatar
fxy060608 已提交
86 87 88
}
/**
 * 从本地file或者blob对象创建url
89
 * @param {Blob|File} file
fxy060608's avatar
fxy060608 已提交
90 91
 * @return {string}
 */
92 93
export function fileToUrl (file) {
  for (const key in files) {
fxy060608's avatar
fxy060608 已提交
94
    if (hasOwn(files, key)) {
95 96 97 98 99 100 101 102 103
      const oldFile = files[key]
      if (oldFile === file) {
        return key
      }
    }
  }
  var url = (window.URL || window.webkitURL).createObjectURL(file)
  files[url] = file
  return url
fxy060608's avatar
fxy060608 已提交
104
}
105

106 107 108 109 110 111 112 113 114
export function getSameOriginUrl (url) {
  const a = document.createElement('a')
  a.href = url
  if (a.origin === location.origin) {
    return Promise.resolve(url)
  }
  return urlToFile(url).then(fileToUrl)
}

115 116 117 118
export function revokeObjectURL (url) {
  (window.URL || window.webkitURL).revokeObjectURL(url)
  delete files[url]
}