Skip to content

Commit

Permalink
file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankbrgowda committed Jul 16, 2024
1 parent 2698455 commit 6fa1e2e
Showing 1 changed file with 99 additions and 16 deletions.
115 changes: 99 additions & 16 deletions packages/jbrowse-plugin-apollo/src/components/AddAssembly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ export function AddAssembly({
}
}

function supportsRequestStreams() {

Check failure on line 142 in packages/jbrowse-plugin-apollo/src/components/AddAssembly.tsx

View workflow job for this annotation

GitHub Actions / Lint

Move function 'supportsRequestStreams' to the outer scope
if (typeof ReadableStream === undefined) {

Check failure on line 143 in packages/jbrowse-plugin-apollo/src/components/AddAssembly.tsx

View workflow job for this annotation

GitHub Actions / Lint

Invalid typeof comparison value
return false
}

try {
// @ts-expect-error

Check failure on line 148 in packages/jbrowse-plugin-apollo/src/components/AddAssembly.tsx

View workflow job for this annotation

GitHub Actions / Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 10 characters or longer
new Request('', {
body: new ReadableStream(),
method: 'POST',
duplex: 'half',
})
return true
} catch (e) {

Check failure on line 155 in packages/jbrowse-plugin-apollo/src/components/AddAssembly.tsx

View workflow job for this annotation

GitHub Actions / Lint

The catch parameter `e` should be named `error`
console.error('Request streams not supported', e)
return false
}
}

async function onSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault()
setErrorMessage('')
Expand Down Expand Up @@ -180,24 +199,88 @@ export function AddAssembly({
uri,
})
if (apolloFetchFile) {
jobsManager.update(job.name, 'Uploading file, this may take awhile')
jobsManager.update(job.name, 'Uploading file, this may take a while')
const { signal } = controller
const response = await apolloFetchFile(uri, {
method: 'POST',
body: formData,
signal,
})
if (!response.ok) {
const newErrorMessage = await createFetchErrorMessage(
response,
'Error when inserting new assembly (while uploading file)',
)
jobsManager.abortJob(job.name, newErrorMessage)
setErrorMessage(newErrorMessage)
return

if (supportsRequestStreams()) {
const fileStream = file.stream()
const reader = fileStream.getReader()
let uploadedBytes = 0
const fileSize = file.size

const body = new ReadableStream({
async start(controller) {
try {
while (true) {

Check failure on line 214 in packages/jbrowse-plugin-apollo/src/components/AddAssembly.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected constant condition
const { done, value } = await reader.read()
if (done) {
controller.close()
break
}
uploadedBytes += value.length
const percentCompleted = Math.round(
(uploadedBytes * 100) / fileSize,
)
jobsManager.update(
job.name,
'Uploading file...',
percentCompleted,
)
controller.enqueue(value)
}
} catch (error) {
controller.error(error)
}
},
})

try {
const response = await apolloFetchFile(uri, {
method: 'POST',
headers: {
'Content-Type': fileType,
'Content-Length': String(file.size),
},
body,
signal,
// @ts-expect-error

Check failure on line 246 in packages/jbrowse-plugin-apollo/src/components/AddAssembly.tsx

View workflow job for this annotation

GitHub Actions / Lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 10 characters or longer
duplex: 'half',
})

if (!response.ok) {
const newErrorMessage = await createFetchErrorMessage(

Check warning on line 251 in packages/jbrowse-plugin-apollo/src/components/AddAssembly.tsx

View workflow job for this annotation

GitHub Actions / Lint

'newErrorMessage' is assigned a value but never used
response,
'Error when inserting new assembly (while uploading file)',
)
jobsManager.abortJob(job.name, '')
setErrorMessage('')
return
}

const result = await response.json()
fileId = result._id
} catch (error) {
console.error('Upload failed', error)
throw error
}
} else {
const response = await apolloFetchFile(uri, {
method: 'POST',
body: formData,
signal,
})
if (!response.ok) {
const newErrorMessage = await createFetchErrorMessage(
response,
'Error when inserting new assembly (while uploading file)',
)
jobsManager.abortJob(job.name, newErrorMessage)
setErrorMessage(newErrorMessage)
return
}
const result = await response.json()
fileId = result._id
}
const result = await response.json()
fileId = result._id
}
}

Expand Down

0 comments on commit 6fa1e2e

Please sign in to comment.