fix: 修复base64真机无法渲染问题
This commit is contained in:
parent
c15cfdfb99
commit
29fcd747ef
@ -71,21 +71,38 @@ export default class Dowloader {
|
|||||||
const file = getFile(fileName);
|
const file = getFile(fileName);
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
// 检查文件是否正常,不正常需要重新下载
|
if (file[KEY_PATH].indexOf('//usr/') !== -1) {
|
||||||
wx.getSavedFileInfo({
|
wx.getFileInfo({
|
||||||
filePath: file[KEY_PATH],
|
filePath: file[KEY_PATH],
|
||||||
success: (res) => {
|
success() {
|
||||||
resolve(file[KEY_PATH]);
|
resolve(file[KEY_PATH]);
|
||||||
},
|
},
|
||||||
fail: (error) => {
|
fail(error) {
|
||||||
console.error(`the file is broken, redownload it, ${JSON.stringify(error)}`);
|
console.error(`base64 file broken, ${JSON.stringify(error)}`);
|
||||||
downloadFile(url, lru).then((path) => {
|
transformBase64File(url, lru).then(path => {
|
||||||
resolve(path);
|
resolve(path);
|
||||||
}, () => {
|
}, () => {
|
||||||
reject();
|
reject();
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
});
|
})
|
||||||
|
} else {
|
||||||
|
// 检查文件是否正常,不正常需要重新下载
|
||||||
|
wx.getSavedFileInfo({
|
||||||
|
filePath: file[KEY_PATH],
|
||||||
|
success: (res) => {
|
||||||
|
resolve(file[KEY_PATH]);
|
||||||
|
},
|
||||||
|
fail: (error) => {
|
||||||
|
console.error(`the file is broken, redownload it, ${JSON.stringify(error)}`);
|
||||||
|
downloadFile(url, lru).then((path) => {
|
||||||
|
resolve(path);
|
||||||
|
}, () => {
|
||||||
|
reject();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (util.isOnlineUrl(url)) {
|
if (util.isOnlineUrl(url)) {
|
||||||
downloadFile(url, lru).then((path) => {
|
downloadFile(url, lru).then((path) => {
|
||||||
@ -107,7 +124,7 @@ export default class Dowloader {
|
|||||||
|
|
||||||
function getFileName(url) {
|
function getFileName(url) {
|
||||||
if (util.isDataUrl(url)) {
|
if (util.isDataUrl(url)) {
|
||||||
const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
|
const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(url) || [];
|
||||||
const fileName = `${sha1.hex_sha1(bodyData)}.${format}`;
|
const fileName = `${sha1.hex_sha1(bodyData)}.${format}`;
|
||||||
return fileName;
|
return fileName;
|
||||||
} else {
|
} else {
|
||||||
@ -126,29 +143,34 @@ function transformBase64File(base64data, lru) {
|
|||||||
const fileName = `${sha1.hex_sha1(bodyData)}.${format}`;
|
const fileName = `${sha1.hex_sha1(bodyData)}.${format}`;
|
||||||
const path = `${wx.env.USER_DATA_PATH}/${fileName}`;
|
const path = `${wx.env.USER_DATA_PATH}/${fileName}`;
|
||||||
const buffer = wx.base64ToArrayBuffer(bodyData.replace(/[\r\n]/g, ""));
|
const buffer = wx.base64ToArrayBuffer(bodyData.replace(/[\r\n]/g, ""));
|
||||||
const result = wx.getFileSystemManager().writeFileSync(path, buffer, 'binary');
|
wx.getFileSystemManager().writeFile({
|
||||||
if (!result) {
|
filePath: path,
|
||||||
wx.getFileInfo({
|
data: buffer,
|
||||||
filePath: path,
|
encoding: 'binary',
|
||||||
success: (tmpRes) => {
|
success() {
|
||||||
const newFileSize = tmpRes.size;
|
wx.getFileInfo({
|
||||||
lru ? doLru(newFileSize).then(() => {
|
filePath: path,
|
||||||
saveFile(fileName, newFileSize, path).then((filePath) => {
|
success: (tmpRes) => {
|
||||||
resolve(filePath);
|
const newFileSize = tmpRes.size;
|
||||||
});
|
lru ? doLru(newFileSize).then(() => {
|
||||||
}, () => {
|
saveFile(fileName, newFileSize, path, true).then((filePath) => {
|
||||||
|
resolve(filePath);
|
||||||
|
});
|
||||||
|
}, () => {
|
||||||
|
resolve(path);
|
||||||
|
}) : resolve(path);
|
||||||
|
},
|
||||||
|
fail: (error) => {
|
||||||
|
// 文件大小信息获取失败,则此文件也不要进行存储
|
||||||
|
console.error(`getFileInfo ${path} failed, ${JSON.stringify(error)}`);
|
||||||
resolve(path);
|
resolve(path);
|
||||||
}) : resolve(path);
|
},
|
||||||
},
|
});
|
||||||
fail: (error) => {
|
},
|
||||||
// 文件大小信息获取失败,则此文件也不要进行存储
|
fail(err) {
|
||||||
console.error(`getFileInfo ${path} failed, ${JSON.stringify(error)}`);
|
console.log(err)
|
||||||
resolve(path);
|
}
|
||||||
},
|
})
|
||||||
});
|
|
||||||
} else {
|
|
||||||
reject()
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,8 +214,22 @@ function downloadFile(url, lru) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveFile(key, newFileSize, tempFilePath) {
|
function saveFile(key, newFileSize, tempFilePath, isDataUrl = false) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
if (isDataUrl) {
|
||||||
|
const totalSize = savedFiles[KEY_TOTAL_SIZE] ? savedFiles[KEY_TOTAL_SIZE] : 0;
|
||||||
|
savedFiles[key] = {};
|
||||||
|
savedFiles[key][KEY_PATH] = tempFilePath;
|
||||||
|
savedFiles[key][KEY_TIME] = new Date().getTime();
|
||||||
|
savedFiles[key][KEY_SIZE] = newFileSize;
|
||||||
|
savedFiles['totalSize'] = newFileSize + totalSize;
|
||||||
|
wx.setStorage({
|
||||||
|
key: SAVED_FILES_KEY,
|
||||||
|
data: savedFiles,
|
||||||
|
});
|
||||||
|
resolve(tempFilePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
wx.saveFile({
|
wx.saveFile({
|
||||||
tempFilePath: tempFilePath,
|
tempFilePath: tempFilePath,
|
||||||
success: (fileRes) => {
|
success: (fileRes) => {
|
||||||
@ -294,12 +330,21 @@ function removeFiles(pathsShouldDelete) {
|
|||||||
if (typeof pathDel === 'object') {
|
if (typeof pathDel === 'object') {
|
||||||
delPath = pathDel.filePath;
|
delPath = pathDel.filePath;
|
||||||
}
|
}
|
||||||
wx.removeSavedFile({
|
if (delPath.indexOf('//usr/') !== -1) {
|
||||||
filePath: delPath,
|
wx.getFileSystemManager().unlink({
|
||||||
fail: (error) => {
|
filePath: delPath,
|
||||||
console.error(`removeSavedFile ${pathDel} failed, ${JSON.stringify(error)}`);
|
fail(error) {
|
||||||
},
|
console.error(`removeSavedFile ${pathDel} failed, ${JSON.stringify(error)}`);
|
||||||
});
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
wx.removeSavedFile({
|
||||||
|
filePath: delPath,
|
||||||
|
fail: (error) => {
|
||||||
|
console.error(`removeSavedFile ${pathDel} failed, ${JSON.stringify(error)}`);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user