yach.util.imgtobase64
Advanced tools
Comparing version 0.1.6 to 0.1.7
// qiao | ||
var qiao = {}; | ||
qiao.db = require('qiao.indexeddb.js'); | ||
qiao.db = require('qiao.indexeddb.js'); | ||
qiao.req = require('qiao.plugin.request'); | ||
@@ -14,27 +14,29 @@ | ||
var database; | ||
var databaseName = 'yach.cache'; | ||
var tableName = 'userpic'; | ||
var version = 1; | ||
var defaultImg = require('./config.json').defaultImg; | ||
var intervalTime = 60 * 1000; | ||
exports.initIndexedDB = async function(){ | ||
var tables = [{ | ||
name : tableName, | ||
key : 'url' | ||
}]; | ||
database = await qiao.db.openDB(databaseName, version); | ||
var res = qiao.db.createTable(db, tables); | ||
var databaseName = 'yach.cache'; | ||
var tableName = 'userpic'; | ||
var version = 1; | ||
var defaultImg = require('./config.json').defaultImg; | ||
var intervalTime = 60 * 1000; | ||
exports.initIndexedDB = async function () { | ||
var tables = [ | ||
{ | ||
name: tableName, | ||
key: 'url', | ||
}, | ||
]; | ||
if(window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'init db and create table', res); | ||
} | ||
database = await qiao.db.openDB(databaseName, version); | ||
var res = qiao.db.createTable(db, tables); | ||
if (window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'init db and create table', res); | ||
}; | ||
// base64 img get | ||
exports.base64ImgGet = async function(url){ | ||
if(!database || database.constructor.name!=='IDBDatabase') return url; | ||
exports.base64ImgGet = async function (url) { | ||
if (!database || database.constructor.name !== 'IDBDatabase') return url; | ||
var checkRes = await checkUrl(url); | ||
if(checkRes) return checkRes.data || checkRes; | ||
if (checkRes) return checkRes.data || checkRes; | ||
if(!base64Imgs.length || base64Imgs.indexOf(url) == -1){ | ||
if (!base64Imgs.length || base64Imgs.indexOf(url) == -1) { | ||
base64Imgs.push(url); | ||
@@ -44,33 +46,33 @@ } | ||
return url; | ||
} | ||
}; | ||
// base64 img get tmp | ||
exports.base64ImgGetTmp = function(url){ | ||
return url||defaultImg; | ||
} | ||
exports.base64ImgGetTmp = function (url) { | ||
return url || defaultImg; | ||
}; | ||
// base64 img get now | ||
exports.base64ImgGetNow = async function(url){ | ||
if(!database || database.constructor.name!=='IDBDatabase') return url; | ||
exports.base64ImgGetNow = async function (url) { | ||
if (!database || database.constructor.name !== 'IDBDatabase') return url; | ||
var checkRes = await checkUrl(url); | ||
if(checkRes) return checkRes.data || checkRes; | ||
if (checkRes) return checkRes.data || checkRes; | ||
var imgBase64 = await qiao.req.imgToBase64Sync(url); | ||
if(imgBase64.type != 'suc'){ | ||
if(window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'img to base64 now fail', imgBase64); | ||
if (imgBase64.type != 'suc') { | ||
if (window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'img to base64 now fail', imgBase64); | ||
return url; | ||
} | ||
var r = await qiao.db.save(database, tableName, url, {url:url, data: imgBase64.msg}); | ||
if(window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'img to base64 now to db', r); | ||
var r = await qiao.db.save(database, tableName, url, { url: url, data: imgBase64.msg }); | ||
if (window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'img to base64 now to db', r); | ||
return imgBase64.msg; | ||
} | ||
}; | ||
// base64 imgArr get | ||
exports.base64ImgArrGet = async function(list, key){ | ||
if(!list || !list.length || !key) return []; | ||
exports.base64ImgArrGet = async function (list, key) { | ||
if (!list || !list.length || !key) return []; | ||
for(var i=0; i<list.length; i++){ | ||
for (var i = 0; i < list.length; i++) { | ||
list[i][key] = await exports.base64ImgGet(list[i][key]); | ||
@@ -80,17 +82,17 @@ } | ||
return list; | ||
} | ||
}; | ||
// base64 imgArr get tmp | ||
exports.base64ImgArrGetTmp = function(list, key){ | ||
if(!list || !list.length || !key) return []; | ||
return list.map(item=>{ | ||
item[key]=item[key]||defaultImg; | ||
exports.base64ImgArrGetTmp = function (list, key) { | ||
if (!list || !list.length || !key) return []; | ||
return list.map((item) => { | ||
item[key] = item[key] || defaultImg; | ||
return item; | ||
}); | ||
} | ||
}; | ||
// check url | ||
async function checkUrl(url){ | ||
if(!url){ | ||
if(window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'no url'); | ||
async function checkUrl(url) { | ||
if (!url) { | ||
if (window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'no url'); | ||
return defaultImg; | ||
@@ -104,54 +106,56 @@ } | ||
var intervalId; | ||
exports.base64ImgStart = function(){ | ||
if(intervalId) return; | ||
exports.base64ImgStart = function () { | ||
if (intervalId) return; | ||
intervalId = setInterval(async function(){ | ||
intervalId = setInterval(async function () { | ||
await base64ImgConvert(); | ||
}, intervalTime); | ||
} | ||
}; | ||
// base64 img convert | ||
async function base64ImgConvert(){ | ||
if(!database || database.constructor.name!=='IDBDatabase'){ | ||
if(intervalId) clearInterval(intervalId); | ||
async function base64ImgConvert() { | ||
if (!database || database.constructor.name !== 'IDBDatabase') { | ||
if (intervalId) clearInterval(intervalId); | ||
return; | ||
} | ||
if(window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'start convert', base64IsConvert, navigator.onLine); | ||
if(base64IsConvert) return; | ||
if (window.debugImgToBase64) | ||
console.log('qiaowenbin', 'img to base64', 'start convert', base64IsConvert, navigator.onLine); | ||
if (base64IsConvert) return; | ||
// offline | ||
if(!navigator.onLine) return; | ||
if (!navigator.onLine) return; | ||
// convert it | ||
convertIt(); | ||
convertIt(); | ||
} | ||
// convert it | ||
function convertIt(){ | ||
function convertIt() { | ||
let imgs = base64Imgs; | ||
imgs = Array.from(new Set(imgs)); | ||
if(!imgs.length) return; | ||
if (!imgs.length) return; | ||
base64Imgs = []; | ||
base64IsConvert = true; | ||
if(window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'convert imgs', imgs.length); | ||
if (window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'convert imgs', imgs.length); | ||
var index = 0; | ||
for(var i=0; i<imgs.length; i++){ | ||
(function(){ | ||
for (var i = 0; i < imgs.length; i++) { | ||
(function () { | ||
var url = imgs[i]; | ||
qiao.req.imgToBase64(url, function(imgBase64){ | ||
if(++index == imgs.length){ | ||
qiao.req.imgToBase64(url, function (imgBase64) { | ||
if (++index == imgs.length) { | ||
index = 0; | ||
base64IsConvert = false; | ||
} | ||
if(imgBase64.type != 'suc'){ | ||
if(window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'img cache fail', imgBase64); | ||
if (imgBase64.type != 'suc') { | ||
if (window.debugImgToBase64) | ||
console.log('qiaowenbin', 'img to base64', 'img cache fail', imgBase64); | ||
return; | ||
} | ||
qiao.db.save(database, tableName, url, {url:url, data: imgBase64.msg}); | ||
if(window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'img cache and return'); | ||
qiao.db.save(database, tableName, url, { url: url, data: imgBase64.msg }); | ||
if (window.debugImgToBase64) console.log('qiaowenbin', 'img to base64', 'img cache and return'); | ||
}); | ||
@@ -158,0 +162,0 @@ })(i); |
{ | ||
"name": "yach.util.imgtobase64", | ||
"version": "0.1.6", | ||
"license": "MIT", | ||
"description": "yach util: img to base64", | ||
"main": "index.js", | ||
"author": "qiaowenbin", | ||
"scripts": {}, | ||
"dependencies": { | ||
"qiao.indexeddb.js": "0.1.0", | ||
"qiao.plugin.request": "^0.3.0" | ||
} | ||
"name": "yach.util.imgtobase64", | ||
"version": "0.1.7", | ||
"license": "MIT", | ||
"description": "yach util: img to base64", | ||
"main": "index.js", | ||
"author": "qiaowenbin", | ||
"scripts": {}, | ||
"dependencies": { | ||
"qiao.indexeddb.js": "0.1.3", | ||
"qiao.plugin.request": "0.3.0" | ||
} | ||
} |
# yach.util.imgtobase64 | ||
## 安装 | ||
```sh | ||
@@ -8,8 +9,10 @@ npm i yach.util.imgtobase64 | ||
## initIndexedDB | ||
## initIndexedDB | ||
```js | ||
初始化本地数据库 | ||
初始化本地数据库; | ||
``` | ||
## base64ImgGet | ||
```js | ||
@@ -20,2 +23,3 @@ 获取图片url对应的base64值,暂存数组 | ||
## base64ImgGetNow | ||
```js | ||
@@ -26,2 +30,3 @@ 获取图片url对应的base64值,立即获取 | ||
## base64ImgArrGet | ||
```js | ||
@@ -32,23 +37,35 @@ 获取图片url对应的base64值,数组的方式 | ||
## base64ImgStart | ||
```js | ||
开始图片url转base64的定时器 | ||
开始图片url转base64的定时器; | ||
``` | ||
## version | ||
### 0.0.8.20201103 | ||
1. ncu | ||
### 0.0.7.20201023 | ||
1. ncu | ||
### 0.0.6.20201022 | ||
1. ncu | ||
### 0.0.5.20200706 | ||
1. fix default img | ||
### 0.0.4.20200705 | ||
1. 修改readme | ||
1. 修改 readme | ||
### 0.0.3.20200630 | ||
1. base64ImgArrGetTmp | ||
### 0.0.2.20200609 | ||
1. qiao.db.js@0.0.4 | ||
@@ -58,4 +75,5 @@ 2. base64ImgGetTmp | ||
### 0.0.1.20200608 | ||
1. init project | ||
2. exports | ||
3. base64ImgArrGet |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5664
131
75
+ Addedqiao.indexeddb.js@0.1.3(transitive)
+ Addedqiao.plugin.request@0.3.0(transitive)
- Removedqiao.indexeddb.js@0.1.0(transitive)
- Removedqiao.plugin.request@0.3.1(transitive)
Updatedqiao.indexeddb.js@0.1.3
Updatedqiao.plugin.request@0.3.0