Skip to content

Commit

Permalink
refactor: migrate objects apis
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Sep 10, 2024
1 parent f537865 commit 52d4417
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 136 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-jokes-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'renterd': minor
---

The files directory and global mode explorers now use the new list objects API.
7 changes: 7 additions & 0 deletions .changeset/dry-elephants-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/renterd-react': minor
'@siafoundation/renterd-types': minor
---

Updated the multipart upload APIs with the new payloads.
39 changes: 21 additions & 18 deletions apps/renterd/contexts/filesDirectory/dataset.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ObjectDirectoryParams } from '@siafoundation/renterd-types'
import { useObjectDirectory } from '@siafoundation/renterd-react'
import { useObjectList } from '@siafoundation/renterd-react'
import { useDataset as useDatasetGeneric } from '../filesManager/dataset'
import { bucketAndKeyParamsFromPath } from '../../lib/paths'
import { useRouter } from 'next/router'
Expand All @@ -19,32 +18,36 @@ export function useDataset() {
} = useFilesManager()
const router = useRouter()
const limit = Number(router.query.limit || defaultLimit)
const offset = Number(router.query.offset || 0)
const marker = String(router.query.marker)

const pathParams = bucketAndKeyParamsFromPath(activeDirectoryPath)

const params = useMemo(() => {
const p: ObjectDirectoryParams = {
...bucketAndKeyParamsFromPath(activeDirectoryPath),
sortBy: sortField,
sortDir: sortDirection,
offset,
limit,
}
let prefix = pathParams.key
if (fileNamePrefixFilter) {
p.prefix = fileNamePrefixFilter.startsWith('/')
prefix += fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter.slice(1)
: fileNamePrefixFilter
}
return p
return {
prefix,
bucket: pathParams.bucket,
sortBy: sortField,
sortDir: sortDirection,
marker,
limit,
delimiter: '/',
}
}, [
activeDirectoryPath,
fileNamePrefixFilter,
pathParams,
sortField,
sortDirection,
offset,
marker,
limit,
])

const response = useObjectDirectory({
const response = useObjectList({
disabled: !activeBucketName,
params,
config: {
Expand All @@ -57,9 +60,9 @@ export function useDataset() {
const objects = useMemo(
() => ({
isValidating: response.isValidating,
data: response.data?.entries,
data: response.data?.objects,
}),
[response.isValidating, response.data?.entries]
[response.isValidating, response.data?.objects]
)

const d = useDatasetGeneric({
Expand All @@ -69,7 +72,7 @@ export function useDataset() {

return {
limit,
offset,
marker,
response,
dataset: d.data,
refresh: response.mutate,
Expand Down
21 changes: 11 additions & 10 deletions apps/renterd/contexts/filesFlat/dataset.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ObjectListPayload } from '@siafoundation/renterd-types'
import { useObjectList } from '@siafoundation/renterd-react'
import { SortField } from '../filesManager/types'
import { useDataset as useDatasetGeneric } from '../filesManager/dataset'
Expand All @@ -21,22 +20,24 @@ export function useDataset({ sortDirection, sortField }: Props) {
const marker = router.query.marker as string

const params = useMemo(() => {
const p: ObjectListPayload = {
let prefix = ''
if (fileNamePrefixFilter) {
prefix += fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter
: '/' + fileNamePrefixFilter
}
return {
prefix,
bucket: activeBucketName,
sortBy: sortField,
sortDir: sortDirection,
marker,
limit,
delimiter: '',
}
if (fileNamePrefixFilter) {
p.prefix = fileNamePrefixFilter.startsWith('/')
? fileNamePrefixFilter
: '/' + fileNamePrefixFilter
}
return p
}, [
activeBucketName,
fileNamePrefixFilter,
activeBucketName,
sortField,
sortDirection,
marker,
Expand All @@ -45,7 +46,7 @@ export function useDataset({ sortDirection, sortField }: Props) {

const response = useObjectList({
disabled: !activeBucketName,
payload: params,
params,
config: {
swr: {
refreshInterval: defaultDatasetRefreshInterval,
Expand Down
6 changes: 3 additions & 3 deletions apps/renterd/contexts/filesManager/dataset.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjEntry } from '@siafoundation/renterd-types'
import { ObjectMetadata } from '@siafoundation/renterd-types'
import { sortBy, toPairs } from '@technically/lodash'
import useSWR from 'swr'
import { useContracts } from '../contracts'
Expand All @@ -16,7 +16,7 @@ type Props = {
id: string
objects: {
isValidating: boolean
data?: ObjEntry[]
data?: ObjectMetadata[]
}
}

Expand Down Expand Up @@ -57,7 +57,7 @@ export function useDataset({ id, objects }: Props) {
}
})
} else if (objects.data || uploadsList.length) {
objects.data?.forEach(({ name: key, size, health }) => {
objects.data?.forEach(({ key, size, health }) => {
const path = join(activeBucketName, key)
const name = getFilename(key)
dataMap[path] = {
Expand Down
10 changes: 7 additions & 3 deletions apps/renterd/contexts/filesManager/uploads.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { triggerErrorToast } from '@siafoundation/design-system'
import { Bucket } from '@siafoundation/renterd-types'
import {
Bucket,
busListObjectsPrefixRoute,
busListObjectsRoute,
} from '@siafoundation/renterd-types'
import {
useBuckets,
useMultipartUploadAbort,
Expand Down Expand Up @@ -106,7 +110,7 @@ export function useUploads({ activeDirectoryPath }: Props) {
const key = getKeyFromPath(path)
const multipartUpload = new MultipartUpload({
file: uploadFile,
path: key,
key,
bucket: bucket.name,
api: ref.current,
partSize: getMultipartUploadPartSize(
Expand All @@ -133,7 +137,7 @@ export function useUploads({ activeDirectoryPath }: Props) {
}, 1000)
)
multipartUpload.setOnComplete(async () => {
await ref.current.mutate((key) => key.startsWith('/bus/objects'))
await ref.current.mutate((key) => key.startsWith(busListObjectsRoute))
ref.current.removeUpload(uploadId)
setTimeout(() => {
ref.current.checkAndStartUploads()
Expand Down
8 changes: 4 additions & 4 deletions apps/renterd/contexts/uploads/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function useUploadsMain() {
await apiBusUploadAbort.post({
payload: {
bucket: activeBucket?.name,
path: upload.path,
key: upload.key,
uploadID: upload.uploadID,
},
})
Expand All @@ -59,8 +59,8 @@ function useUploadsMain() {
return (
response.data?.uploads?.map((upload) => {
const id = upload.uploadID
const name = getFilename(upload.path)
const fullPath = join(activeBucket?.name, upload.path)
const name = getFilename(upload.key)
const fullPath = join(activeBucket?.name, upload.key)
const localUpload = uploadsMap[id]
if (localUpload) {
{
Expand All @@ -83,7 +83,7 @@ function useUploadsMain() {
await apiBusUploadAbort.post({
payload: {
bucket: activeBucket?.name,
path: upload.path,
key: upload.key,
uploadID: upload.uploadID,
},
})
Expand Down
15 changes: 7 additions & 8 deletions apps/renterd/lib/multipartUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type ApiBusUploadAbort = ReturnType<typeof useMultipartUploadAbort>

export type MultipartParams = {
bucket: string
path: string
key: string
file: File
api: {
workerUploadPart: ApiWorkerUploadPart
Expand All @@ -41,7 +41,7 @@ type UploadedPart = {
export class MultipartUpload {
// params
#bucket: string
#path: string
#key: string
#file: File
#partSize: number
#maxConcurrentParts: number
Expand Down Expand Up @@ -75,7 +75,7 @@ export class MultipartUpload {
constructor(options: MultipartParams) {
// params
this.#bucket = options.bucket
this.#path = options.path
this.#key = options.key
this.#partSize = options.partSize || 1024 * 1024 * 5
this.#maxConcurrentParts = Math.min(options.maxConcurrentParts || 5, 15)
this.#file = options.file
Expand All @@ -96,8 +96,7 @@ export class MultipartUpload {
public async create() {
const createPayload = {
bucket: this.#bucket,
generateKey: true,
path: this.#path,
key: this.#key,
}
const response = await this.#api.busUploadCreate.post({
payload: createPayload,
Expand Down Expand Up @@ -133,7 +132,7 @@ export class MultipartUpload {
await this.#api.busUploadAbort.post({
payload: {
bucket: this.#bucket,
path: this.#path,
key: this.#key,
uploadID: this.#uploadId,
},
})
Expand Down Expand Up @@ -235,7 +234,7 @@ export class MultipartUpload {
try {
const payload = {
bucket: this.#bucket,
path: this.#path,
key: this.#key,
uploadID: this.#uploadId,
parts: this.#uploadedParts.sort((a, b) => a.partNumber - b.partNumber),
}
Expand Down Expand Up @@ -280,7 +279,7 @@ export class MultipartUpload {
try {
const response = await this.#api.workerUploadPart.put({
params: {
key: this.#path.slice(1),
key: this.#key.slice(1),
bucket: this.#bucket,
uploadid: this.#uploadId,
offset: partOffset,
Expand Down
21 changes: 2 additions & 19 deletions libs/renterd-js/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ import {
ObjectDeleteParams,
ObjectDeletePayload,
ObjectDeleteResponse,
ObjectDirectoryParams,
ObjectDirectoryPayload,
ObjectDirectoryResponse,
ObjectListParams,
ObjectListPayload,
ObjectListResponse,
Expand All @@ -133,9 +130,6 @@ import {
ObjectRenamePayload,
ObjectRenameResponse,
ObjectResponse,
ObjectSearchParams,
ObjectSearchPayload,
ObjectSearchResponse,
ObjectsStatsParams,
ObjectsStatsPayload,
ObjectsStatsResponse,
Expand Down Expand Up @@ -230,10 +224,8 @@ import {
busMultipartListuploadsRoute,
busMultipartPartRoute,
busObjectsKeyRoute,
busObjectsListRoute,
busObjectsRenameRoute,
busHostsRoute,
busSearchObjectsRoute,
busSettingKeyRoute,
busSettingsRoute,
busSlabKeyObjectsRoute,
Expand All @@ -255,6 +247,7 @@ import {
AutopilotsPayload,
AutopilotsResponse,
busWalletEventsRoute,
busListObjectsPrefixRoute,
} from '@siafoundation/renterd-types'
import { buildRequestHandler, initAxios } from '@siafoundation/request'
import { AxiosRequestConfig } from 'axios'
Expand Down Expand Up @@ -468,26 +461,16 @@ export function Bus({ api, password }: { api: string; password?: string }) {
BucketDeletePayload,
BucketDeleteResponse
>(axios, 'delete', busBucketNameRoute),
objectDirectory: buildRequestHandler<
ObjectDirectoryParams,
ObjectDirectoryPayload,
ObjectDirectoryResponse
>(axios, 'get', busObjectsKeyRoute),
objectList: buildRequestHandler<
ObjectListParams,
ObjectListPayload,
ObjectListResponse
>(axios, 'post', busObjectsListRoute),
>(axios, 'get', busListObjectsPrefixRoute),
object: buildRequestHandler<ObjectParams, ObjectPayload, ObjectResponse>(
axios,
'get',
busObjectsKeyRoute
),
objectSearch: buildRequestHandler<
ObjectSearchParams,
ObjectSearchPayload,
ObjectSearchResponse
>(axios, 'get', busSearchObjectsRoute),
objectAdd: buildRequestHandler<
ObjectAddParams,
ObjectAddPayload,
Expand Down
Loading

0 comments on commit 52d4417

Please sign in to comment.