Skip to content

Commit

Permalink
feat: implement uploadCAR in uploader (storacha#517)
Browse files Browse the repository at this point in the history
This exposes the `upload-client`'s `uploadCAR` function in the
`UploaderProvider`.

---------

Co-authored-by: Travis Vachon <[email protected]>
  • Loading branch information
Alan Shaw and travis authored May 17, 2023
1 parent 1bf4cf2 commit 40036ea
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/uploader-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as UploaderCore from '@w3ui/uploader-core'
**Functions**
- [`uploadFile`](#uploadfile)
- [`uploadDirectory`](#uploaddirectory)
- [`uploadCAR`](#uploadcar)

---

Expand All @@ -47,3 +48,7 @@ Re-exported [`uploadFile` function](https://github.com/web3-storage/w3protocol/t
### `uploadDirectory`

Re-exported [`uploadDirectory` function](https://github.com/web3-storage/w3protocol/tree/main/packages/upload-client#uploaddirectory) from `@web3-storage/upload-client`.

### `uploadCAR`

Re-exported [`uploadCAR` function](https://github.com/web3-storage/w3protocol/tree/main/packages/upload-client#uploadcar) from `@web3-storage/upload-client`.
3 changes: 3 additions & 0 deletions packages/react-uploader/src/Uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ const UploaderComponentContext = createContext<UploaderComponentContextValue>([
},
uploadDirectory: async () => {
throw new Error('missing uploader context provider')
},
uploadCAR: async () => {
throw new Error('missing uploader context provider')
}
}
])
Expand Down
36 changes: 35 additions & 1 deletion packages/react-uploader/src/providers/Uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import type {
import React, { useContext, createContext, useState } from 'react'
import {
uploadFile,
uploadDirectory
uploadDirectory,
uploadCAR
} from '@w3ui/uploader-core'
import { useKeyring } from '@w3ui/react-keyring'
import { add as storeAdd } from '@web3-storage/capabilities/store'
Expand All @@ -31,6 +32,9 @@ export const uploaderContextDefaultValue: UploaderContextValue = [
},
uploadDirectory: async () => {
throw new Error('missing uploader context provider')
},
uploadCAR: async () => {
throw new Error('missing uploader context provider')
}
}
]
Expand Down Expand Up @@ -117,6 +121,36 @@ export function UploaderProvider ({
})
setUploadProgress({})
return result
},
async uploadCAR (car: Blob) {
if (space == null) throw new Error('missing space')
if (agent == null) throw new Error('missing agent')

const storedShards: CARMetadata[] = []
setStoredDAGShards(storedShards)

const conf = {
issuer: agent,
with: space.did(),
audience: servicePrincipal,
proofs: await getProofs([
{ can: storeAdd.can, with: space.did() },
{ can: uploadAdd.can, with: space.did() }
])
}

const result = await uploadCAR(conf, car, {
onShardStored: (meta) => {
storedShards.push(meta)
setStoredDAGShards([...storedShards])
},
onUploadProgress: (status: ProgressStatus) => {
setUploadProgress(statuses => ({ ...statuses, [status.url ?? '']: status }))
},
connection
})
setUploadProgress({})
return result
}
}

Expand Down
35 changes: 34 additions & 1 deletion packages/solid-uploader/src/providers/Uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ParentComponent
} from 'solid-js'
import { createStore } from 'solid-js/store'
import { uploadFile, uploadDirectory } from '@w3ui/uploader-core'
import { uploadFile, uploadDirectory, uploadCAR } from '@w3ui/uploader-core'
import { useKeyring } from '@w3ui/solid-keyring'
import { add as storeAdd } from '@web3-storage/capabilities/store'
import { add as uploadAdd } from '@web3-storage/capabilities/upload'
Expand All @@ -34,6 +34,9 @@ const UploaderContext = createContext<UploaderContextValue>([
},
uploadDirectory: async () => {
throw new Error('missing uploader context provider')
},
uploadCAR: async () => {
throw new Error('missing uploader context provider')
}
}
])
Expand Down Expand Up @@ -112,6 +115,36 @@ export const UploaderProvider: ParentComponent<UploaderProviderProps> = (
})
setState('uploadProgress', {})
return result
},
async uploadCAR (car: Blob) {
if (keyringState.space == null) throw new Error('missing space')
if (keyringState.agent == null) throw new Error('missing agent')

const storedShards: CARMetadata[] = []
setState('storedDAGShards', storedShards)

const conf = {
issuer: keyringState.agent,
with: keyringState.space.did(),
audience: props.servicePrincipal,
proofs: await keyringActions.getProofs([
{ can: storeAdd.can, with: keyringState.space.did() },
{ can: uploadAdd.can, with: keyringState.space.did() }
])
}

const result = await uploadCAR(conf, car, {
onShardStored: (meta) => {
storedShards.push(meta)
setState('storedDAGShards', [...storedShards])
},
onUploadProgress: (status: ProgressStatus) => {
setState('uploadProgress', { ...state.uploadProgress, [status.url ?? '']: status })
},
connection: props.connection
})
setState('uploadProgress', {})
return result
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/uploader-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ConnectionView, Principal } from '@ucanto/interface'

import { Link, Version } from 'multiformats'

export { uploadFile, uploadDirectory } from '@web3-storage/upload-client'
export { uploadFile, uploadDirectory, uploadCAR } from '@web3-storage/upload-client'

export type { CARMetadata, Service, ProgressFn, ProgressStatus, FetchOptions }

Expand Down Expand Up @@ -31,4 +31,8 @@ export interface UploaderContextActions {
uploadDirectory: (
files: File[]
) => Promise<CID>
/**
* Upload a CAR file containing a DAG to the current space.
*/
uploadCAR: (car: Blob) => Promise<CID>
}
34 changes: 33 additions & 1 deletion packages/vue-uploader/src/providers/Uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
} from 'vue'
import {
uploadFile,
uploadDirectory
uploadDirectory,
uploadCAR
} from '@w3ui/uploader-core'
import { KeyringProviderInjectionKey } from '@w3ui/vue-keyring'
import { add as storeAdd } from '@web3-storage/capabilities/store'
Expand Down Expand Up @@ -128,6 +129,37 @@ export const UploaderProvider: Component<UploaderProviderProps> = defineComponen
})
state.uploadProgress = {}
return result
},
async uploadCAR (car: Blob) {
if (space?.value == null) throw new Error('missing space')
if (agent?.value == null) throw new Error('missing agent')
if (getProofs == null) throw new Error('missing getProofs')

const storedShards: CARMetadata[] = []
state.storedDAGShards = storedShards

const conf = {
issuer: agent.value,
with: space.value.did(),
audience: servicePrincipal,
proofs: await getProofs([
{ can: storeAdd.can, with: space.value.did() },
{ can: uploadAdd.can, with: space.value.did() }
])
}

const result = await uploadCAR(conf, car, {
onShardStored: (meta) => {
storedShards.push(meta)
state.storedDAGShards = [...storedShards]
},
onUploadProgress: (status: ProgressStatus) => {
state.uploadProgress = { ...state.uploadProgress, [status.url ?? '']: status }
},
connection
})
state.uploadProgress = {}
return result
}
}

Expand Down

0 comments on commit 40036ea

Please sign in to comment.