Skip to content

Commit

Permalink
feat: renterd multipart upload hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 22, 2024
1 parent 40e402a commit 4c22445
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-ducks-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/react-renterd': minor
---

Add useMultipartUploadCreate, useMultipartUploadComplete, useMultipartUploadAbort, useMultipartUploadAddPart, useMultipartUploadChunk, useMultipartUploadListUploads, useMultipartUploadListParts.
157 changes: 157 additions & 0 deletions libs/react-renterd/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import {
Host,
HostSettings,
Obj,
PartialSlab,
SiacoinElement,
SlabSlice,
WalletTransaction,
} from './siaTypes'

Expand Down Expand Up @@ -838,3 +840,158 @@ export function useMetricsWallet(
// hostKey: string
// origin: string
// }

// multipart

export type MultipartUploadCreatePayload = {
path: string
bucket: string
key?: string /// defaults to key:0000000000000000000000000000000000000000000000000000000000000000
}

export function useMultipartUploadCreate(
args?: HookArgsCallback<
void,
MultipartUploadCreatePayload,
{ uploadID: string }
>
) {
return usePostFunc(
{ ...args, route: '/bus/multipart/create' },
async (mutate) => {
mutate((key) => {
return key.startsWith('/bus/multipart')
})
}
)
}

export type MultipartUploadCompletePart = {
partNumber: number
eTag: string
}

export type MultipartUploadCompletePayload = {
path: string
bucket: string
uploadID: string
parts: MultipartUploadCompletePart[]
}

export function useMultipartUploadComplete(
args?: HookArgsCallback<void, MultipartUploadCompletePayload, void>
) {
return usePostFunc(
{ ...args, route: '/bus/multipart/complete' },
async (mutate) => {
mutate((key) => {
return key.startsWith('/bus/multipart')
})
}
)
}

export type MultipartUploadAbortPayload = {
path: string
bucket: string
uploadID: string
}

export function useMultipartUploadAbort(
args?: HookArgsCallback<void, MultipartUploadAbortPayload, void>
) {
return usePostFunc(
{ ...args, route: '/bus/multipart/abort' },
async (mutate) => {
mutate((key) => {
return key.startsWith('/bus/multipart')
})
}
)
}

export type MultipartUploadListPartsPayload = {
bucket: string
path: string
uploadID: string
partNumberMarker: number
limit: number
}

export type MultipartUploadListPartsResponse = {
hasMore: boolean
nextMarker?: number
parts: {
partNumber: number
lastModified: string
eTag: string
size: number
}[]
}

export function useMultipartUploadListParts(
args?: HookArgsWithPayloadSwr<
void,
MultipartUploadListPartsPayload,
MultipartUploadListPartsResponse
>
) {
return usePostSwr({
...args,
route: '/bus/multipart/listparts',
})
}

export type MultipartUploadListUploadsPayload = {
bucket: string
prefix: string
pathMarker?: string
uploadIDMarker?: string
limit: number
}

export type MultipartUploadListUploadsResponse = {
uploads: {
path: string
uploadID: string
createdAt: string
}[]
}

export function useMultipartUploadListUploads(
args?: HookArgsWithPayloadSwr<
void,
MultipartUploadListUploadsPayload,
MultipartUploadListUploadsResponse
>
) {
return usePostSwr({
...args,
route: '/bus/multipart/listuploads',
})
}

export type MultipartUploadAddPartPayload = {
path: string
bucket: string
uploadID: string
eTag: string
partNumber: number
contractSet?: string
partialSlabs?: PartialSlab[]
slices?: SlabSlice[]
usedContracts?: Contract[]
}

export function useMultipartUploadAddPart(
args?: HookArgsCallback<void, MultipartUploadAddPartPayload, void>
) {
return usePostFunc(
{ ...args, route: '/bus/multipart/part' },
async (mutate) => {
mutate((key) => {
return key.startsWith('/bus/multipart/listparts')
})
}
)
}
6 changes: 6 additions & 0 deletions libs/react-renterd/src/siaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ export type Sector = {
root: string
}

export type PartialSlab = {
key: EncryptionKey
offset: number
length: number
}

export type Slab = {
health: number
key: EncryptionKey
Expand Down
30 changes: 30 additions & 0 deletions libs/react-renterd/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ export function useObjectUpload(
)
}

export type MultipartUploadChunkParams = {
key: string
bucket?: string
contractset?: string
uploadid: string
partnumber: number
minshards?: number
totalshards?: number
offset?: number
disablepreshardingencryption?: boolean
}

export function useMultipartUploadChunk(
args?: HookArgsCallback<MultipartUploadChunkParams, Blob, void>
) {
return usePutFunc({
...args,
config: {
...args?.config,
axios: {
...args?.config?.axios,
headers: {
'Content-Type': 'multipart/form-data',
},
},
},
route: '/worker/multipart/:key',
})
}

export type RhpScanRequest = {
hostKey: string
hostIP: string
Expand Down

0 comments on commit 4c22445

Please sign in to comment.