fix: 修复base64真机无法渲染问题
This commit is contained in:
parent
c15cfdfb99
commit
29fcd747ef
@ -71,21 +71,38 @@ export default class Dowloader {
|
||||
const file = getFile(fileName);
|
||||
|
||||
if (file) {
|
||||
// 检查文件是否正常,不正常需要重新下载
|
||||
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();
|
||||
});
|
||||
},
|
||||
});
|
||||
if (file[KEY_PATH].indexOf('//usr/') !== -1) {
|
||||
wx.getFileInfo({
|
||||
filePath: file[KEY_PATH],
|
||||
success() {
|
||||
resolve(file[KEY_PATH]);
|
||||
},
|
||||
fail(error) {
|
||||
console.error(`base64 file broken, ${JSON.stringify(error)}`);
|
||||
transformBase64File(url, lru).then(path => {
|
||||
resolve(path);
|
||||
}, () => {
|
||||
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 {
|
||||
if (util.isOnlineUrl(url)) {
|
||||
downloadFile(url, lru).then((path) => {
|
||||
@ -107,7 +124,7 @@ export default class Dowloader {
|
||||
|
||||
function getFileName(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}`;
|
||||
return fileName;
|
||||
} else {
|
||||
@ -126,29 +143,34 @@ function transformBase64File(base64data, lru) {
|
||||
const fileName = `${sha1.hex_sha1(bodyData)}.${format}`;
|
||||
const path = `${wx.env.USER_DATA_PATH}/${fileName}`;
|
||||
const buffer = wx.base64ToArrayBuffer(bodyData.replace(/[\r\n]/g, ""));
|
||||
const result = wx.getFileSystemManager().writeFileSync(path, buffer, 'binary');
|
||||
if (!result) {
|
||||
wx.getFileInfo({
|
||||
filePath: path,
|
||||
success: (tmpRes) => {
|
||||
const newFileSize = tmpRes.size;
|
||||
lru ? doLru(newFileSize).then(() => {
|
||||
saveFile(fileName, newFileSize, path).then((filePath) => {
|
||||
resolve(filePath);
|
||||
});
|
||||
}, () => {
|
||||
wx.getFileSystemManager().writeFile({
|
||||
filePath: path,
|
||||
data: buffer,
|
||||
encoding: 'binary',
|
||||
success() {
|
||||
wx.getFileInfo({
|
||||
filePath: path,
|
||||
success: (tmpRes) => {
|
||||
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);
|
||||
},
|
||||
fail: (error) => {
|
||||
// 文件大小信息获取失败,则此文件也不要进行存储
|
||||
console.error(`getFileInfo ${path} failed, ${JSON.stringify(error)}`);
|
||||
resolve(path);
|
||||
},
|
||||
});
|
||||
} else {
|
||||
reject()
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
fail(err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
@ -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) => {
|
||||
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({
|
||||
tempFilePath: tempFilePath,
|
||||
success: (fileRes) => {
|
||||
@ -294,12 +330,21 @@ function removeFiles(pathsShouldDelete) {
|
||||
if (typeof pathDel === 'object') {
|
||||
delPath = pathDel.filePath;
|
||||
}
|
||||
wx.removeSavedFile({
|
||||
filePath: delPath,
|
||||
fail: (error) => {
|
||||
console.error(`removeSavedFile ${pathDel} failed, ${JSON.stringify(error)}`);
|
||||
},
|
||||
});
|
||||
if (delPath.indexOf('//usr/') !== -1) {
|
||||
wx.getFileSystemManager().unlink({
|
||||
filePath: delPath,
|
||||
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