123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- // 媒体名称
- export const mediaTypes = {
- image: "图片",
- video: "视频",
- audio: "音频",
- };
- // 媒体扩展类型
- export const mediaMimes = {
- image: ["jpg", "png", "jpeg", "bmp", "gif"],
- audio: ["mp3", "aac", "ogg", "wav" /* , "m4a" */],
- video: ["mp4", "mov", "quicktime" /* ,"webm", "rmvb", "wmv" */], //ios:mov
- };
- // 媒体大小显示(MB)
- export const mediaMaxSize = {
- image: 10,
- video: 20,
- audio: 5,
- };
- /**
- * 获取媒体扩展类型
- * @param {Stirng} filename 文件名称
- */
- export const getMime = (filename) => {
- if (!filename || filename.indexOf(".") === -1) {
- return "";
- }
- return filename.split(".").pop().toLowerCase();
- };
- /**
- * 在路径中获取文件名
- * @param {*} path
- */
- export const getFilename = (path) => {
- const segment = (path || "").split("/");
- return segment[segment.length - 1];
- };
- /**
- * 检测媒体文件是否超过预设限制
- * @param {String} type 媒体类型
- * @param {Number} size 文件大小
- */
- export const checkSizeLimit = (type, size) => {
- size = size / 1024 / 1024;
- return size <= mediaMaxSize[type];
- };
- export const checkSizeLimitFree = (size, limit) => {
- size = size / 1024 / 1024;
- return size <= limit;
- };
- /**
- * 检测媒体类型
- * @param {String} type 媒体类型
- * @param {String} filename 文件名称
- */
- export const checkMediaMime = (type, filename) => {
- const mime = getMime(filename);
- const find = mediaMimes[type];
- if (!find) {
- return false;
- }
- return find.indexOf(mime) !== -1;
- };
- export const base64ToBlob = (base64) => {
- let arr = base64.split(","),
- mime = arr[0].match(/:(.*?);/)[1],
- bstr = atob(arr[1]),
- n = bstr.length,
- u8arr = new Uint8Array(n);
- while (n--) {
- u8arr[n] = bstr.charCodeAt(n);
- }
- return new Blob([u8arr], { type: mime });
- };
- export const base64ToDataURL = (base64) => {
- return window.URL.createObjectURL(base64ToBlob(base64));
- };
- export const blobToBase64 = function (blob) {
- return new Promise((resolve) => {
- var reader = new FileReader();
- reader.onload = function () {
- resolve(reader.result);
- };
- reader.readAsDataURL(blob);
- });
- };
- /**
- * 获取图片文件尺寸
- * @param {*} file
- */
- export const getImgWH = (file) => {
- return new Promise((resolve, reject) => {
- var reader = new FileReader();
- //读取图片文件
- let url = URL.createObjectURL(file);
- var image = new Image();
- //FileReader获得Base64字符串
- image.src = url;
- image.onload = function () {
- console.log("图片加载成功");
- //获得图片高宽
- var height = this.height;
- var width = this.width;
- resolve({
- height,
- width,
- });
- };
- image.onerror = function (e) {
- reject({ message: "loadFile Error" });
- };
- // reader.readAsDataURL(file);
- // reader.onload = function (e) {
- // //初始化JavaScript图片对象
- // var image = new Image();
- // //FileReader获得Base64字符串
- // image.src = e.target.result;
- // image.onload = function () {
- // console.error("图片加载成功");
- // //获得图片高宽
- // var height = this.height;
- // var width = this.width;
- // resolve({
- // height,
- // width,
- // });
- // };
- // image.onerror = function (e) {
- // console.error("图片加载失败",e);
- // reject({
- // height: 0,
- // width: 0,
- // });
- // };
- // };
- });
- };
- /**
- * 转化字节单位
- * @param {*} file
- */
- export const changeByteUnit = (x) => {
- const units = ["KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
- let l = 0,
- n = parseInt(x, 10) || 0;
- while (n >= 1024 && ++l) {
- n = n / 1024;
- }
- return n.toFixed(n < 10 && l > 0 ? 2 : 0) + units[l];
- };
|