mirror of
https://github.com/ducbao414/win32.run.git
synced 2025-12-16 17:22:51 +09:00
82 lines
2.9 KiB
HTML
82 lines
2.9 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>test webworker</title>
|
|
</head>
|
|
<body>
|
|
<input type="file" id="file" />
|
|
<script type="module" >
|
|
|
|
function hex(buffer) {
|
|
const hexCodes = [];
|
|
const view = new DataView(buffer);
|
|
for (let i = 0; i < view.byteLength; i += 4) {
|
|
const value = view.getUint32(i)
|
|
const stringValue = value.toString(16)
|
|
const padding = '00000000'
|
|
const paddedValue = (padding + stringValue).slice(-padding.length)
|
|
hexCodes.push(paddedValue);
|
|
}
|
|
return hexCodes.join("");
|
|
}
|
|
|
|
function getChecksum(file){
|
|
return new Promise((resolve,reject) => {
|
|
try{
|
|
const reader = new FileReader();
|
|
reader.onload = function() {
|
|
crypto.subtle.digest("SHA-256", reader.result).then(function (hash) {
|
|
resolve(hex(hash));
|
|
});
|
|
}
|
|
reader.readAsArrayBuffer(file);
|
|
}catch(err){
|
|
reject(err);
|
|
}
|
|
});
|
|
}
|
|
|
|
function finish(){
|
|
const d = document.createElement('div');
|
|
d.setAttribute('id','done');
|
|
d.textContent = 'Done.';
|
|
document.body.appendChild(d);
|
|
}
|
|
|
|
async function fileChecksums(obj){
|
|
for( const [key,val] of Object.entries(obj) ){
|
|
obj[key] = val instanceof File ?
|
|
await getChecksum(val) : await fileChecksums(val);
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
import {Archive} from '../../src/libarchive.js';
|
|
|
|
Archive.init({
|
|
workerUrl: '../../dist/worker-bundle.js'
|
|
});
|
|
|
|
window.Archive = Archive;
|
|
|
|
document.getElementById('file').addEventListener('change', async (e) => {
|
|
let obj = null;
|
|
try{
|
|
const file = e.currentTarget.files[0];
|
|
const archive = await Archive.open(file);
|
|
//console.log( await archive.getFilesObject() );
|
|
//console.log( await archive.getFilesArray() );
|
|
obj = await archive.extractFiles();
|
|
//console.log( await archive.getFilesObject() );
|
|
//console.log( await archive.getFilesArray() );
|
|
obj = await fileChecksums(obj);
|
|
}catch(err){
|
|
console.error(err);
|
|
}finally{
|
|
window.obj = obj;
|
|
finish();
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html> |