'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
import {
  hasOwn
} from 'uni-shared'
 
/**
 * 暂存的文件对象
 */
const files = {}
/**
 * 从url读取File
 * @param {string} url
 * @param {boolean} local
 * @param {Promise}
 */
export function urlToFile (url, local) {
  var file = files[url]
  if (file) {
    return Promise.resolve(file)
  }
  if (/^data:[a-z-]+\/[a-z-]+;base64,/.test(url)) {
    return Promise.resolve(base64ToFile(url))
  }
  if (local) {
    return Promise.reject(new Error('not find'))
  }
  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()
  })
}
/**
 * base64转File
 * @param {string} base64
 * @return {File}
 */
export function base64ToFile (base64) {
  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)
  }
  return blobToFile(array, type)
}
/**
 * 简易获取扩展名
 * @param {string} type
 * @return {string}
 */
function getExtname (type) {
  const extname = type.split('/')[1]
  return extname ? `.${extname}` : ''
}
/**
 * 简易获取文件名
 * @param {*} url
 */
export function getFileName (url) {
  url = url.split('#')[0].split('?')[0]
  const array = url.split('/')
  return array[array.length - 1]
}
 
/**
 * 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
}
/**
 * 从本地file或者blob对象创建url
 * @param {Blob|File} file
 * @return {string}
 */
export function fileToUrl (file) {
  for (const key in files) {
    if (hasOwn(files, key)) {
      const oldFile = files[key]
      if (oldFile === file) {
        return key
      }
    }
  }
  var url = (window.URL || window.webkitURL).createObjectURL(file)
  files[url] = file
  return url
}
 
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)
}
 
export function revokeObjectURL (url) {
  (window.URL || window.webkitURL).revokeObjectURL(url)
  delete files[url]
}