You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.7 KiB
136 lines
3.7 KiB
"use strict";
|
|
const common_vendor = require("../common/vendor.js");
|
|
function dataUrlToBase64(str) {
|
|
const array = str.split(",");
|
|
return array[array.length - 1];
|
|
}
|
|
let index = 0;
|
|
function getNewFileId() {
|
|
return Date.now() + String(index++);
|
|
}
|
|
function biggerThan(v1, v2) {
|
|
const v1Array = v1.split(".");
|
|
const v2Array = v2.split(".");
|
|
let update = false;
|
|
for (let index2 = 0; index2 < v2Array.length; index2++) {
|
|
const diff = v1Array[index2] - v2Array[index2];
|
|
if (diff !== 0) {
|
|
update = diff > 0;
|
|
break;
|
|
}
|
|
}
|
|
return update;
|
|
}
|
|
function base64ToPath(base64) {
|
|
return new Promise(function(resolve, reject) {
|
|
if (typeof window === "object" && "document" in window) {
|
|
base64 = base64.split(",");
|
|
const type = base64[0].match(/:(.*?);/)[1];
|
|
const str = atob(base64[1]);
|
|
let n = str.length;
|
|
const array = new Uint8Array(n);
|
|
while (n--) {
|
|
array[n] = str.charCodeAt(n);
|
|
}
|
|
return resolve(
|
|
(window.URL || window.webkitURL).createObjectURL(
|
|
new Blob([array], { type })
|
|
)
|
|
);
|
|
}
|
|
let extName = base64.split(",")[0].match(/data\:\S+\/(\S+);/);
|
|
if (extName) {
|
|
extName = extName[1];
|
|
} else {
|
|
reject(new Error("base64 error"));
|
|
}
|
|
const fileName = getNewFileId() + "." + extName;
|
|
if (typeof plus === "object") {
|
|
const basePath = "_doc";
|
|
const dirPath = "uniapp_temp";
|
|
var filePath = basePath + "/" + dirPath + "/" + fileName;
|
|
if (!biggerThan(
|
|
plus.os.name === "Android" ? "1.9.9.80627" : "1.9.9.80472",
|
|
plus.runtime.innerVersion
|
|
)) {
|
|
plus.io.resolveLocalFileSystemURL(
|
|
basePath,
|
|
function(entry) {
|
|
entry.getDirectory(
|
|
dirPath,
|
|
{
|
|
create: true,
|
|
exclusive: false
|
|
},
|
|
function(entry2) {
|
|
entry2.getFile(
|
|
fileName,
|
|
{
|
|
create: true,
|
|
exclusive: false
|
|
},
|
|
function(entry3) {
|
|
entry3.createWriter(function(writer) {
|
|
writer.onwrite = function() {
|
|
resolve(filePath);
|
|
};
|
|
writer.onerror = reject;
|
|
writer.seek(0);
|
|
writer.writeAsBinary(
|
|
dataUrlToBase64(base64)
|
|
);
|
|
}, reject);
|
|
},
|
|
reject
|
|
);
|
|
},
|
|
reject
|
|
);
|
|
},
|
|
reject
|
|
);
|
|
return;
|
|
}
|
|
const bitmap = new plus.nativeObj.Bitmap(fileName);
|
|
bitmap.loadBase64Data(
|
|
base64,
|
|
function() {
|
|
bitmap.save(
|
|
filePath,
|
|
{},
|
|
function() {
|
|
bitmap.clear();
|
|
resolve(filePath);
|
|
},
|
|
function(error) {
|
|
bitmap.clear();
|
|
reject(error);
|
|
}
|
|
);
|
|
},
|
|
function(error) {
|
|
bitmap.clear();
|
|
reject(error);
|
|
}
|
|
);
|
|
return;
|
|
}
|
|
if (typeof common_vendor.wx$1 === "object" && common_vendor.wx$1.canIUse("getFileSystemManager")) {
|
|
var filePath = common_vendor.wx$1.env.USER_DATA_PATH + "/" + fileName;
|
|
common_vendor.wx$1.getFileSystemManager().writeFile({
|
|
filePath,
|
|
data: dataUrlToBase64(base64),
|
|
encoding: "base64",
|
|
success: function() {
|
|
resolve(filePath);
|
|
},
|
|
fail: function(error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
reject(new Error("not support"));
|
|
});
|
|
}
|
|
exports.base64ToPath = base64ToPath;
|
|
|