forked from xzag/vue-auth-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-auth-image.js
50 lines (46 loc) · 1.29 KB
/
vue-auth-image.js
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
;(function () {
var vueAuthImage = {};
var axios = typeof require === 'function'
? require('axios')
: window.Axios;
if (!axios) {
throw new Error('[vue-auth-image] cannot locate Axios');
}
function setImgSrc(el, binding) {
if (binding.oldValue === undefined || binding.value !== binding.oldValue) {
var imageUrl = binding.value;
axios({
method: 'get',
url: imageUrl,
responseType: 'arraybuffer'
})
.then(function(resp) {
var mimeType = resp.headers['content-type'].toLowerCase();
var imgBase64 = new Buffer(resp.data, 'binary').toString('base64');
el.src = 'data:' + mimeType + ';base64,' + imgBase64;
}).catch((function() {
el.src = imageUrl;
}));
}
}
vueAuthImage.install = function(Vue) {
Vue.directive('auth-image', {
bind: function(el, binding) {
setImgSrc(el, binding);
},
componentUpdated: function(el, binding) {
setImgSrc(el, binding);
}
});
};
if (typeof exports == 'object') {
module.exports = vueAuthImage;
} else if (typeof define == 'function' && define.amd) {
define([], function() {
return vueAuthImage;
});
} else if (window.Vue) {
window.VueAuthImage = vueAuthImage;
Vue.use(vueAuthImage);
}
})();