taro-hooks
Version:
为 Taro 而设计的 Hooks Library
160 lines (159 loc) • 5.15 kB
JavaScript
function _await(value, then, direct) {
if (direct) {
return then ? then(value) : value;
}
if (!value || !value.then) {
value = Promise.resolve(value);
}
return then ? value.then(then) : value;
}
function _async(f) {
return function () {
for (var args = [], i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
try {
return Promise.resolve(f.apply(this, args));
} catch (e) {
return Promise.reject(e);
}
};
}
function _catch(body, recover) {
try {
var result = body();
} catch (e) {
return recover(e);
}
if (result && result.then) {
return result.then(void 0, recover);
}
return result;
}
function _invoke(body, then) {
var result = body();
if (result && result.then) {
return result.then(then);
}
return then(result);
}
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.compressImage = exports.saveImageToPhotosAlbum = exports.generateBlobUrl = exports.downloadImage = void 0;
var tslib_1 = require("tslib");
var compressorjs_1 = tslib_1.__importDefault(require("@taro-hooks/compressorjs"));
var tool_1 = require("../../utils/tool");
function checkBase64Prefix(url) {
return url.startsWith('data:');
}
function dataURITOBlob(url) {
var _mimeString$match;
var _url$split = url.split(','),
mimeString = _url$split[0],
dataString = _url$split[1];
var mime = mimeString == null ? void 0 : mimeString.match == null ? void 0 : (_mimeString$match = mimeString.match(/:(.*?);/)) == null ? void 0 : _mimeString$match[1];
var length = dataString.length;
var unitArray8 = new Uint8Array(length);
while (length--) {
unitArray8[length] = dataString.charCodeAt(length);
}
return new Blob([unitArray8], {
type: mime
});
}
var downloadImage = _async(function (filePath) {
return checkBase64Prefix(filePath) ? dataURITOBlob(filePath) : _await(fetch(filePath), function (responese) {
return _await(responese.blob());
});
});
exports.downloadImage = downloadImage;
var generateBlobUrl = function generateBlobUrl(blob) {
var blobInstance = new Blob([blob], {
type: blob.type
});
var href = window.URL.createObjectURL(blobInstance);
return href;
};
exports.generateBlobUrl = generateBlobUrl;
var saveImageToPhotosAlbum = _async(function (_ref) {
var _exit = false;
var filePath = _ref.filePath,
success = _ref.success,
fail = _ref.fail;
var failResult = (0, tool_1.generateGeneralFail)('saveImageToPhotosAlbum');
return _invoke(function () {
if (filePath) {
return _catch(function () {
return _await((0, exports.downloadImage)(filePath), function (blob) {
var href = (0, exports.generateBlobUrl)(blob);
var downloadElement = document.createElement('a');
downloadElement.href = href;
// base64 not have name, make random name
downloadElement.download = Date.now() + '';
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
window.URL.revokeObjectURL(href);
var result = {
errMsg: 'saveImageToPhotosAlbum: success'
};
success == null ? void 0 : success(result);
_exit = true;
return result;
});
}, function (e) {
var withMessageFailResult = (0, tool_1.generateGeneralFail)('saveImageToPhotosAlbum', e.message);
fail == null ? void 0 : fail(withMessageFailResult);
_exit = true;
return withMessageFailResult;
});
}
}, function (_result) {
if (_exit) return _result;
fail == null ? void 0 : fail(failResult);
return failResult;
});
});
exports.saveImageToPhotosAlbum = saveImageToPhotosAlbum;
/**
* compressImage for h5
* @param src source path
* @param quality compress quality
* @returns
*/
var compressImage = _async(function (_ref2) {
var src = _ref2.src,
_ref2$quality = _ref2.quality,
quality = _ref2$quality === void 0 ? 80 : _ref2$quality,
_success = _ref2.success,
fail = _ref2.fail;
return _catch(function () {
return _await((0, exports.downloadImage)(src), function (fileBlob) {
var compress = new Promise(function (resolve, reject) {
new compressorjs_1["default"](fileBlob, {
quality: quality / 100,
success: function success(res) {
var tempFilePath = (0, exports.generateBlobUrl)(res);
var successResult = {
tempFilePath: tempFilePath,
errMsg: 'compressImage:ok'
};
_success == null ? void 0 : _success(successResult);
resolve(successResult);
},
error: function error(e) {
var failResult = (0, tool_1.generateGeneralFail)('compressImage', e.message);
fail == null ? void 0 : fail(failResult);
reject(failResult);
}
});
});
return _await(compress);
});
}, function (e) {
return (0, tool_1.generateGeneralFail)('compressImage', e.errMsg || e.message);
});
});
exports.compressImage = compressImage;
;