Skip to content

Commit

Permalink
Merge pull request #62 from Paivola-Student-Innovation-Lab/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
dkristia authored May 29, 2024
2 parents 973ba9d + d8eae07 commit 7157c63
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
6 changes: 4 additions & 2 deletions client/src/hooks/useFunctionality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import useMultithreading from "./useMultiThreading";

function useFunctionality() {
//hook defenitions
const{setUiState, reset} = usefunctionalityState((state)=>({setUiState: state.setUiState,reset: state.reset}))
const{setUiState, fileStore} = usefunctionalityState((state)=>({setUiState: state.setUiState,fileStore: state.filestore}))
const {handleChecks} = useChecks()
const {handleLoad} = useOnPageLoad()
const cryptFiles = useMultithreading()
Expand All @@ -32,7 +32,9 @@ function useFunctionality() {

//reset opfs when someone tries to unload the page
const handleReset = async() => {
reset()
navigator.storage.getDirectory().then((directoryhandle) => {
directoryhandle.removeEntry(fileStore)
})
}
useEffect(() => {
window.addEventListener('beforeunload', handleReset)
Expand Down
42 changes: 26 additions & 16 deletions client/src/hooks/useOnPageLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,53 @@ function useOnPageLoad(){
const translate = useIntl().formatMessage
const remove = async()=>{
// Remove all files from opfs
for await (const key of (await navigator.storage.getDirectory()).keys()){
let newKey = "stored file 0001"
for await (const key of (await navigator.storage.getDirectory()).keys()) {
if(newKey === "stored file 0001"){
const number = parseInt(key.split(" ")[2])
// File store name is formatted like this to make sorting work properly up to 9999
newKey = "stored file " + ("000" + (number + 1)).slice(-4)
}
await(await navigator.storage.getDirectory()).removeEntry(key)
}
// Create default file
setFileStore("stored file")
await(await navigator.storage.getDirectory()).getFileHandle("stored file", {create: true})
// Create a new file to opfs
setFileStore(newKey)
await(await navigator.storage.getDirectory()).getFileHandle(newKey, {create: true})

// Close question modal
closeModal()
}
const createNew = async()=>{
// Add an increasing number to the end of the default key until the key is no longer in the array of stored files
let number=0
let key = "stored file"
while(Object.keys(await navigator.storage.getDirectory()).includes(key)){
number+=1
key="stored file"+number
let newKey = "stored file 0001"
for await(const key of (await navigator.storage.getDirectory()).keys()){
// Get a key that is one greater than previous greatest key
const number = parseInt(key.split(" ")[2])
// File store name is formatted like this to make sorting work properly up to 9999
newKey = "stored file " + ("000" + (number + 1)).slice(-4)
break
}
//creates file from key
setFileStore(key)
await(await navigator.storage.getDirectory()).getFileHandle(key, {create: true})
// Create file from key
setFileStore(newKey)
await(await navigator.storage.getDirectory()).getFileHandle(newKey, {create: true})
// Close question modal
closeModal()
}


const handleLoad = async() => {
// Check if there are any files in opfs
let filesinstore=false
if (Object.keys(await navigator.storage.getDirectory()).length>0){
for await (const key of (await navigator.storage.getDirectory()).keys()){
// If there are files in opfs, ask the user how to proceed. The user can call one of the functions above through this modal
makeModal(translate({id: "another_tab"}), translate({id: "another_tab_text"}), [[translate({id: "remove_tab"}), remove], [translate({id: "create_tab"}), createNew]], true)
filesinstore=true
break
}
// If no files were found, create default file
if(!filesinstore){
setFileStore("stored file")
await(await navigator.storage.getDirectory()).getFileHandle("stored file", {create: true})
// File store name is formatted like this to make sorting work properly up to 9999
setFileStore("stored file 0001")
await(await navigator.storage.getDirectory()).getFileHandle("stored file 0001", {create: true})
}
}
return { handleLoad }
Expand Down
13 changes: 9 additions & 4 deletions client/src/utils/cryptWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { chunkFile, combineUint8Arrays } from "./Chunking";

init()
let paused = false
let hasPaused = false
let deletedFileSize = 0
let cryptData: {files: File[], encrypting: boolean, startId: number, endId: number, chunkSize: number, nonce: Uint8Array, key: Uint8Array} = {files: [], encrypting: true, startId: 0, endId: -1, chunkSize: 0, nonce: new Uint8Array, key: new Uint8Array}

const createChunk = async(key:number, files: (Blob|Uint8Array)[], chunkSize: number) => {
let dataProcessed = key*chunkSize
let byteArray: Uint8Array = new Uint8Array([])
Expand Down Expand Up @@ -39,7 +39,8 @@ const cryptFiles = async () => {
if(paused){
//store data to continue crypting in the future
cryptData.startId = id
break
hasPaused = true
return
}
const chunk = await createChunk(id, files, chunkSize)
let cryptedChunk;
Expand All @@ -48,9 +49,10 @@ const cryptFiles = async () => {
}
else{
cryptedChunk = decrypt(chunk, nonce, key)
}
}
postMessage([id, cryptedChunk])
}

}

onmessage = async(message) => {
Expand All @@ -60,7 +62,10 @@ onmessage = async(message) => {
}
else if(message.data === "unpause"){
paused = false
cryptFiles()
if (hasPaused) {
hasPaused = false
cryptFiles()
}
}
else if(message.data === "clear"){
//when crypting is stopped
Expand Down

0 comments on commit 7157c63

Please sign in to comment.