Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github + OPFS delete + OPFS tests #977

Merged
merged 12 commits into from
Feb 9, 2024
Merged
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bldrs",
"version": "1.0.888",
"version": "1.0.895",
"main": "src/index.jsx",
"license": "MIT",
"homepage": "https://github.com/bldrs-ai/Share",
Expand Down
14 changes: 7 additions & 7 deletions src/Components/OpenModelControl.jsx
pablo-mayrgundter marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function OpenModelDialog({
const selectRepo = async (repo) => {
setSelectedRepoName(repo)
const owner = orgNamesArr[selectedOrgName]
const files = await getFiles(repoNamesArr[repo], owner, accessToken)
const files = await getFiles(owner, repoNamesArr[repo], accessToken)
const fileNames = Object.keys(files).map((key) => files[key].name)
setFilesArr(fileNames)
}
Expand Down Expand Up @@ -169,12 +169,12 @@ function OpenModelDialog({
<Selector label={'Repository'} list={repoNamesArr} selected={selectedRepoName} setSelected={selectRepo} testId={'Repository'}/>
<Selector label={'File'} list={filesArr} selected={selectedFileName} setSelected={setSelectedFileName} testId={'File'}/>
{selectedFileName !== '' &&
<Box sx={{textAlign: 'center', marginTop: '4px'}}>
<RectangularButton
title={'LOAD FILE'}
onClick={navigateToFile}
/>
</Box>
<Box sx={{textAlign: 'center', marginTop: '4px'}}>
<RectangularButton
title={'LOAD FILE'}
onClick={navigateToFile}
/>
</Box>
}
</Stack> :
<Box sx={{padding: '0px 10px'}} elevation={0}>
Expand Down
188 changes: 182 additions & 6 deletions src/OPFS/OPFS.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ self.addEventListener('message', async (event) => {
assertValues(event.data,
['objectUrl', 'commitHash', 'owner', 'repo', 'branch', 'onProgress', 'originalFilePath'])
await downloadModelToOPFS(objectUrl, commitHash, originalFilePath, owner, repo, branch, onProgress)
} else if (event.data.command === 'doesFileExist') {
const {commitHash, originalFilePath, owner, repo, branch} =
assertValues(event.data,
['commitHash', 'originalFilePath', 'owner', 'repo', 'branch'])

await doesFileExistInOPFS(commitHash, originalFilePath, owner, repo, branch)
} else if (event.data.command === 'deleteModel') {
const {commitHash, originalFilePath, owner, repo, branch} =
assertValues(event.data,
['commitHash', 'originalFilePath', 'owner', 'repo', 'branch'])

await deleteModelFromOPFS(commitHash, originalFilePath, owner, repo, branch)
}
} catch (error) {
self.postMessage({error: error.message})
Expand All @@ -47,8 +59,7 @@ async function downloadModelToOPFS(objectUrl, commitHash, originalFilePath, owne
try {
ownerFolderHandle = await opfsRoot.getDirectoryHandle(owner, {create: false})
} catch (error) {
// Ignore errors here, as it is a valid operation if the folder does
// not exist yet.
// Expected: folder does not exist
}

if (ownerFolderHandle === null) {
Expand All @@ -65,8 +76,7 @@ async function downloadModelToOPFS(objectUrl, commitHash, originalFilePath, owne
try {
repoFolderHandle = await ownerFolderHandle.getDirectoryHandle(repo, {create: false})
} catch (error) {
// Ignore errors here, as it is a valid operation if the folder does
// not exist yet.
// Expected: folder does not exist
}

if (repoFolderHandle === null) {
Expand All @@ -83,8 +93,7 @@ async function downloadModelToOPFS(objectUrl, commitHash, originalFilePath, owne
try {
branchFolderHandle = await repoFolderHandle.getDirectoryHandle(branch, {create: false})
} catch (error) {
// Ignore errors here, as it is a valid operation if the folder does
// not exist yet.
// Expected: folder does not exist
}

if (branchFolderHandle === null) {
Expand Down Expand Up @@ -308,6 +317,173 @@ async function writeModelToOPFSFromFile(modelFile, objectKey, originalFileName,
}
}

/**
* This function navigates to a filepath in OPFS to see if it exists.
* If any parent folders or the file do not exist, it will return 'notexist'.
* If it exists, it will return 'exist'
*
pablo-mayrgundter marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} commitHash
pablo-mayrgundter marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} originalFilePath
* @param {*} owner
* @param {*} repo
* @param {*} branch
* @return {string} postmessage specifying operation status
*/
async function doesFileExistInOPFS(commitHash, originalFilePath, owner, repo, branch) {
const opfsRoot = await navigator.storage.getDirectory()
let ownerFolderHandle = null
let repoFolderHandle = null
let branchFolderHandle = null
// See if owner folder handle exists
try {
ownerFolderHandle = await opfsRoot.getDirectoryHandle(owner, {create: false})
} catch (error) {
// Expected: folder does not exist
}

if (ownerFolderHandle === null) {
self.postMessage({completed: true, event: 'notexist', commitHash: commitHash})
return
}

// See if repo folder handle exists
try {
repoFolderHandle = await ownerFolderHandle.getDirectoryHandle(repo, {create: false})
} catch (error) {
// Expected: folder does not exist
}

if (repoFolderHandle === null) {
self.postMessage({completed: true, event: 'notexist', commitHash: commitHash})
return
}

// See if branch folder handle exists
try {
branchFolderHandle = await repoFolderHandle.getDirectoryHandle(branch, {create: false})
} catch (error) {
// Expected: folder does not exist
}

if (branchFolderHandle === null) {
self.postMessage({completed: true, event: 'notexist', commitHash: commitHash})
return
}

// Get a file handle in the folder for the model
let modelBlobFileHandle = null
let modelDirectoryHandle = null
const pathSegments = originalFilePath.split('/')
const strippedFileName = pathSegments[pathSegments.length - 1]
// lets see if our commit hash matches
// Get file handle for file blob
try {
// eslint-disable-next-line no-unused-vars
[modelDirectoryHandle, modelBlobFileHandle] = await
retrieveFileWithPath(branchFolderHandle, originalFilePath, commitHash, false)
} catch (error) {
// expected if file not found
}

let fileIsCached = false
if (modelBlobFileHandle !== null) {
// file name is name.ifc.commitHash, we just want to compare commitHash
const testCommitHash = modelBlobFileHandle.name.split(strippedFileName)[1].slice(1)
if (commitHash === testCommitHash) {
fileIsCached = true
}
}

if (fileIsCached) {
self.postMessage({completed: true, event: 'exist', commitHash: commitHash})
return
}

self.postMessage({completed: true, event: 'notexist', commitHash: commitHash})
}

/**
* This function navigates to the model location in OPFS and deletes it.
* If any parent folders or the file do not exist, it will return 'notexist'.
* If it successfully deletes the file, it will return 'deleted'.
*
pablo-mayrgundter marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} commitHash
pablo-mayrgundter marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} originalFilePath
* @param {*} owner
* @param {*} repo
* @param {*} branch
* @return {string} postmessage specifying operation status
*/
async function deleteModelFromOPFS(commitHash, originalFilePath, owner, repo, branch) {
const opfsRoot = await navigator.storage.getDirectory()
let ownerFolderHandle = null
let repoFolderHandle = null
let branchFolderHandle = null
// See if owner folder handle exists
try {
ownerFolderHandle = await opfsRoot.getDirectoryHandle(owner, {create: false})
} catch (error) {
// Expected: folder does not exist
}

if (ownerFolderHandle === null) {
self.postMessage({completed: true, event: 'notexist', commitHash: commitHash})
return
}

// See if repo folder handle exists
try {
repoFolderHandle = await ownerFolderHandle.getDirectoryHandle(repo, {create: false})
} catch (error) {
// Expected: folder does not exist
}

if (repoFolderHandle === null) {
self.postMessage({completed: true, event: 'notexist', commitHash: commitHash})
return
}

// See if branch folder handle exists
try {
branchFolderHandle = await repoFolderHandle.getDirectoryHandle(branch, {create: false})
} catch (error) {
// Expected: folder does not exist
}

if (branchFolderHandle === null) {
self.postMessage({completed: true, event: 'notexist', commitHash: commitHash})
return
}

// Get a file handle in the folder for the model
let modelBlobFileHandle = null
const pathSegments = originalFilePath.split('/')
const strippedFileName = pathSegments[pathSegments.length - 1]
// lets see if our commit hash matches
// Get file handle for file blob
try {
[, modelBlobFileHandle] = await
retrieveFileWithPath(branchFolderHandle, originalFilePath, commitHash, false)
} catch (error) {
// expected if file not found
}

let fileIsCached = false
if (modelBlobFileHandle !== null) {
// file name is name.ifc.commitHash, we just want to compare commitHash
const testCommitHash = modelBlobFileHandle.name.split(strippedFileName)[1].slice(1)
if (commitHash === testCommitHash) {
fileIsCached = true
}
}

if (fileIsCached && modelBlobFileHandle !== null) {
modelBlobFileHandle.remove()
}

self.postMessage({completed: true, event: 'deleted', commitHash: commitHash})
}

/**
*
*/
Expand Down