const newFile = await encipherFileList(file);
import CryptoJS from 'crypto-js';
import CryptoU8array from "./crypto-en";
import hooks from '/@/hooks'
const key = CryptoJS.enc.Utf8.parse("1111111111111111"); // 十六位十六进制数作为秘钥
const iv = CryptoJS.enc.Utf8.parse('1111111111111111'); // 十六位十六进制数作为秘钥偏移量
/**
* 使用 AES 进行文件加密
* @param word 需要加密的二进制流文件
* @returns 返回base64
*/
export function encryptFile(word) {
const messageWordArray = CryptoU8array.u8array.parse(word);
const encrypted = CryptoJS.AES.encrypt(messageWordArray, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
const encryptedBytes = encrypted.ciphertext;
return CryptoU8array.u8array.stringify(encryptedBytes);
}
/**
* 转换blob对象
*/
export function atobToblob(intArray,type) {
// 创建一个 Blob 对象
var blob = new Blob([intArray], { type: type });
return blob;
}
/**
* 使用 AES 进行文件解密
* @param word 需要解密的二进制流文件
* @returns 返回base64
*/
export function decryptFile(word) {
const messageWordArray = CryptoU8array.u8array.parse(word);
const decrypt = CryptoJS.AES.decrypt({ ciphertext: messageWordArray }, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return CryptoU8array.u8array.stringify(decrypt);
}
export function arrayBufferToBlob(arrayBuffer, type) {
return new Blob([new Uint8Array(arrayBuffer)], { type });
}
/**
* 加密文件
* @param file
* @returns
*/
export function encipherFileList(file) {
return new Promise((resolve, reject) => {
try {
let reader = new FileReader()
reader.readAsArrayBuffer(file) // 以二进制数据读取文件
reader.onload = async () => {
let fileContent = new Uint8Array(reader.result) // 转成Uint8Array类型
let encryptedContent = encryptFile(fileContent); // 加密
let blobType = file.type
let fileName = file.name
let blob = atobToblob(encryptedContent, blobType) // 将加密后串转blob
let blobToFile = new File([blob], fileName, {
type: blobType,
lastModified: Date.now()
}); // 得到加密文件
resolve(blobToFile)
}
} catch {
reject()
console.log('err: encipherFileList')
}
})
}
/**
* 获取流文件base64
* @param file
* @returns
*/
function getBase64(file: File) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
resolve(reader.result);
}
reader.onerror = (error) => reject(error);
});
}
/**
* 检测图片是否存在
* @param url 图片文件地址
* @returns
*/
export function verifyImageComplete(url) {
return new Promise((resolve, reject) => {
var img = new Image();
img.onload = function() {
console.log('Image is valid and can be displayed.');
resolve(true)
};
img.onerror = function() {
console.log('Image is either corrupt or cannot be displayed.');
resolve(false)
};
img.src = `${hooks.defaultAPI}${url}`;
})
}
/**
* 解密文件
* @param file
* @returns
*/
export function decryptFileList(file) {
return new Promise(async (resolve, reject) => {
try {
const blob = await fetch(`${hooks.defaultAPI}${file.url}`).then(response => response.blob());
const arrayBuffer = await new Response(blob).arrayBuffer();
const encryptedContent = new Uint8Array(arrayBuffer);
const decryptedContent = decryptFile(encryptedContent);
const decryptedBlob = arrayBufferToBlob(decryptedContent, 'image/jpeg'); // 这里假设解密后是图片类型
const decryptedFile = new File([decryptedBlob], file.name, { type: decryptedBlob.type });
file.url = URL.createObjectURL(decryptedFile)
const base64 = await getBase64(decryptedFile)
resolve({ url: file.url, file: decryptedFile, base64 })
} catch {
console.log('err: decryptFileList')
reject()
}
})
}
/**
* 将图片转换成流文件
* @param url
* @returns
*/
export function urlTransFile(url) {
return new Promise(async (resolve, reject) => {
const blob = await fetch(`${hooks.defaultAPI}${url}`).then(response => response.blob());
const file = new File([blob], "image.jpg", { type: "image/jpeg" });
resolve(file)
})
}
/**
* 获取图片缩略图
* @param imageFile
* @param maxWidth
* @param maxHeight
* @param quality
* @returns
*/
export function generateThumbnail(imageFile, maxWidth, maxHeight, quality) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
let width = img.width;
let height = img.height;
// 计算缩放比例
if (width > height) {
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
} else {
if (height > maxHeight) {
width *= maxHeight / height;
height = maxHeight;
}
}
canvas.width = width;
canvas.height = height;
// 绘制图片到canvas
ctx.drawImage(img, 0, 0, width, height);
// 将canvas转换为base64格式的缩略图
const thumbnailDataURL = canvas.toDataURL('image/jpeg', quality);
resolve(thumbnailDataURL);
};
img.onerror = function() {
resolve('err');
};
img.src = reader.result;
};
reader.onerror = (error) => reject(error);
reader.readAsDataURL(imageFile);
});
}