Initial commit
This commit is contained in:
60
resources/js/SubmissionsClass/HashesManager.js
Normal file
60
resources/js/SubmissionsClass/HashesManager.js
Normal file
@@ -0,0 +1,60 @@
|
||||
import { calculate as calculateHashes } from "../hashes.js";
|
||||
|
||||
export function HashesManager( wire ) {
|
||||
return {
|
||||
|
||||
/**
|
||||
* Wire variable instance.
|
||||
*/
|
||||
$wire: wire,
|
||||
|
||||
/**
|
||||
* If a file hash is currently calculated or not.
|
||||
* @type {boolean}
|
||||
*/
|
||||
isCalculating: false,
|
||||
|
||||
/**
|
||||
* An error on hash calculation.
|
||||
* @type {any|null}
|
||||
*/
|
||||
error: null,
|
||||
|
||||
async handleSubmitFile(){
|
||||
|
||||
if( this.isCalculating === true ) // Calculation already done for another file.
|
||||
return;
|
||||
|
||||
this.error = null; // Reset.
|
||||
const FILE = await this.openFileInput();
|
||||
|
||||
if( !FILE )
|
||||
return; // No file sent.
|
||||
|
||||
this.isCalculating = true;
|
||||
|
||||
try {
|
||||
const RESULT = await calculateHashes(FILE);
|
||||
await this.$wire.addHash(RESULT.filename, RESULT.crc32, RESULT.sha1); // Send a signal to livewire.
|
||||
} catch(err) {
|
||||
this.error = err.message;
|
||||
} finally {
|
||||
this.isCalculating = false;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Open a specific file.
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
async openFileInput(){
|
||||
return new Promise(resolve => {
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.onchange = () => resolve(input.files[0] ?? null );
|
||||
input.oncancel = () => resolve(null);
|
||||
input.click();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user