From d95be665660eb252d3ec72bb0a4caaef2558479e Mon Sep 17 00:00:00 2001 From: Alex Freska Date: Tue, 10 Sep 2024 11:34:30 -0400 Subject: [PATCH] refactor: migrate objects apis --- .../contexts/filesDirectory/dataset.tsx | 44 +++++++++---------- .../renterd/contexts/filesManager/dataset.tsx | 4 +- libs/renterd-js/src/bus.ts | 21 +-------- libs/renterd-react/src/bus.ts | 21 +-------- libs/renterd-types/src/bus.ts | 31 ++++--------- libs/renterd-types/src/types.ts | 22 +++++++--- 6 files changed, 52 insertions(+), 91 deletions(-) diff --git a/apps/renterd/contexts/filesDirectory/dataset.tsx b/apps/renterd/contexts/filesDirectory/dataset.tsx index 3219e507f..3cc385e94 100644 --- a/apps/renterd/contexts/filesDirectory/dataset.tsx +++ b/apps/renterd/contexts/filesDirectory/dataset.tsx @@ -1,11 +1,11 @@ -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' import { useMemo } from 'react' import { useFilesManager } from '../filesManager' import { defaultDatasetRefreshInterval } from '../../config/swr' +import { ObjectListPayload } from '@siafoundation/renterd-types' const defaultLimit = 50 @@ -19,34 +19,34 @@ 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 params = useMemo(() => { - const p: ObjectDirectoryParams = { + if (fileNamePrefixFilter) { + return { + prefix: fileNamePrefixFilter.startsWith('/') + ? fileNamePrefixFilter.slice(1) + : fileNamePrefixFilter, + } + } + return undefined + }, [fileNamePrefixFilter]) + + const payload = useMemo(() => { + const p: ObjectListPayload = { ...bucketAndKeyParamsFromPath(activeDirectoryPath), sortBy: sortField, sortDir: sortDirection, - offset, + marker, limit, } - if (fileNamePrefixFilter) { - p.prefix = fileNamePrefixFilter.startsWith('/') - ? fileNamePrefixFilter.slice(1) - : fileNamePrefixFilter - } return p - }, [ - activeDirectoryPath, - fileNamePrefixFilter, - sortField, - sortDirection, - offset, - limit, - ]) + }, [activeDirectoryPath, sortField, sortDirection, marker, limit]) - const response = useObjectDirectory({ + const response = useObjectList({ disabled: !activeBucketName, params, + payload, config: { swr: { refreshInterval: defaultDatasetRefreshInterval, @@ -57,9 +57,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({ @@ -69,7 +69,7 @@ export function useDataset() { return { limit, - offset, + marker, response, dataset: d.data, refresh: response.mutate, diff --git a/apps/renterd/contexts/filesManager/dataset.tsx b/apps/renterd/contexts/filesManager/dataset.tsx index c8af717cd..8bb0d3254 100644 --- a/apps/renterd/contexts/filesManager/dataset.tsx +++ b/apps/renterd/contexts/filesManager/dataset.tsx @@ -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' @@ -16,7 +16,7 @@ type Props = { id: string objects: { isValidating: boolean - data?: ObjEntry[] + data?: ObjectMetadata[] } } diff --git a/libs/renterd-js/src/bus.ts b/libs/renterd-js/src/bus.ts index 0a1e814b2..99ac8309b 100644 --- a/libs/renterd-js/src/bus.ts +++ b/libs/renterd-js/src/bus.ts @@ -121,9 +121,6 @@ import { ObjectDeleteParams, ObjectDeletePayload, ObjectDeleteResponse, - ObjectDirectoryParams, - ObjectDirectoryPayload, - ObjectDirectoryResponse, ObjectListParams, ObjectListPayload, ObjectListResponse, @@ -133,9 +130,6 @@ import { ObjectRenamePayload, ObjectRenameResponse, ObjectResponse, - ObjectSearchParams, - ObjectSearchPayload, - ObjectSearchResponse, ObjectsStatsParams, ObjectsStatsPayload, ObjectsStatsResponse, @@ -230,10 +224,8 @@ import { busMultipartListuploadsRoute, busMultipartPartRoute, busObjectsKeyRoute, - busObjectsListRoute, busObjectsRenameRoute, busHostsRoute, - busSearchObjectsRoute, busSettingKeyRoute, busSettingsRoute, busSlabKeyObjectsRoute, @@ -255,6 +247,7 @@ import { AutopilotsPayload, AutopilotsResponse, busWalletEventsRoute, + busListObjectsPrefixRoute, } from '@siafoundation/renterd-types' import { buildRequestHandler, initAxios } from '@siafoundation/request' import { AxiosRequestConfig } from 'axios' @@ -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, 'post', busListObjectsPrefixRoute), object: buildRequestHandler( axios, 'get', busObjectsKeyRoute ), - objectSearch: buildRequestHandler< - ObjectSearchParams, - ObjectSearchPayload, - ObjectSearchResponse - >(axios, 'get', busSearchObjectsRoute), objectAdd: buildRequestHandler< ObjectAddParams, ObjectAddPayload, diff --git a/libs/renterd-react/src/bus.ts b/libs/renterd-react/src/bus.ts index 05d8821bc..4ef9e8031 100644 --- a/libs/renterd-react/src/bus.ts +++ b/libs/renterd-react/src/bus.ts @@ -115,8 +115,6 @@ import { ObjectDeleteParams, ObjectDeletePayload, ObjectDeleteResponse, - ObjectDirectoryParams, - ObjectDirectoryResponse, ObjectListParams, ObjectListPayload, ObjectListResponse, @@ -125,8 +123,6 @@ import { ObjectRenamePayload, ObjectRenameResponse, ObjectResponse, - ObjectSearchParams, - ObjectSearchResponse, ObjectsStatsParams, ObjectsStatsResponse, SettingParams, @@ -189,10 +185,8 @@ import { busHostsHostKeyRoute, busObjectsRoute, busObjectsKeyRoute, - busObjectsListRoute, busObjectsRenameRoute, busHostsRoute, - busSearchObjectsRoute, busSettingKeyRoute, busSettingsRoute, busStateRoute, @@ -244,6 +238,7 @@ import { WalletEventsParams, WalletEventsResponse, busWalletEventsRoute, + busListObjectsPrefixRoute, } from '@siafoundation/renterd-types' // state @@ -699,12 +694,6 @@ export function useBucketDelete( ) } -export function useObjectDirectory( - args: HookArgsSwr -) { - return useGetSwr({ ...args, route: busObjectsKeyRoute }) -} - export function useObjectList( args: HookArgsWithPayloadSwr< ObjectListParams, @@ -712,19 +701,13 @@ export function useObjectList( ObjectListResponse > ) { - return usePostSwr({ ...args, route: busObjectsListRoute }) + return usePostSwr({ ...args, route: busListObjectsPrefixRoute }) } export function useObject(args: HookArgsSwr) { return useGetSwr({ ...args, route: busObjectsKeyRoute }) } -export function useObjectSearch( - args: HookArgsSwr -) { - return useGetSwr({ ...args, route: busSearchObjectsRoute }) -} - export function useObjectAdd( args: HookArgsCallback ) { diff --git a/libs/renterd-types/src/bus.ts b/libs/renterd-types/src/bus.ts index 2aa5aacc2..ea8fde2b7 100644 --- a/libs/renterd-types/src/bus.ts +++ b/libs/renterd-types/src/bus.ts @@ -16,6 +16,7 @@ import { Host, HostSettings, Obj, + ObjectMetadata, PartialSlab, SlabSlice, } from './types' @@ -60,9 +61,8 @@ export const busBucketsRoute = '/bus/buckets' export const busBucketNameRoute = '/bus/bucket/:name' export const busBucketNamePolicyRoute = '/bus/bucket/:name/policy' export const busObjectsRoute = '/bus/objects' +export const busListObjectsPrefixRoute = '/bus/listobjects/:prefix' export const busObjectsKeyRoute = '/bus/objects/:key' -export const busObjectsListRoute = '/bus/objects/list' -export const busSearchObjectsRoute = '/bus/search/objects' export const busObjectsRenameRoute = '/bus/objects/rename' export const busStatsObjectsRoute = '/bus/stats/objects' export const busSettingRoute = '/bus/setting' @@ -388,37 +388,22 @@ export type BucketDeleteParams = { name: string } export type BucketDeletePayload = void export type BucketDeleteResponse = void -export type ObjEntry = { - name: string - size: number - health: number -} - -export type ObjectDirectoryParams = { - key: string - bucket: string - limit?: number +export type ObjectListParams = { prefix?: string - offset?: number - sortBy?: 'name' | 'health' | 'size' - sortDir?: 'asc' | 'desc' } -export type ObjectDirectoryPayload = void -export type ObjectDirectoryResponse = { hasMore: boolean; entries: ObjEntry[] } - -export type ObjectListParams = void export type ObjectListPayload = { bucket: string + delimiter?: string limit?: number - prefix?: string marker?: string sortBy?: 'name' | 'health' | 'size' sortDir?: 'asc' | 'desc' + substring?: string } export type ObjectListResponse = { hasMore: boolean nextMarker: string - objects: ObjEntry[] + objects: ObjectMetadata[] } export type ObjectParams = { key: string; bucket: string } @@ -432,7 +417,7 @@ export type ObjectSearchParams = { limit: number } export type ObjectSearchPayload = void -export type ObjectSearchResponse = ObjEntry[] +export type ObjectSearchResponse = ObjectMetadata[] export type ObjectAddParams = { key: string; bucket: string } export type ObjectAddPayload = { @@ -521,7 +506,7 @@ export type AlertsDismissResponse = void export type SlabObjectsParams = { key: string } export type SlabObjectsPayload = void -export type SlabObjectsResponse = ObjEntry[] +export type SlabObjectsResponse = ObjectMetadata[] // metrics diff --git a/libs/renterd-types/src/types.ts b/libs/renterd-types/src/types.ts index bc823810b..470054288 100644 --- a/libs/renterd-types/src/types.ts +++ b/libs/renterd-types/src/types.ts @@ -108,17 +108,27 @@ export type SlabSlice = { length: number } -export type Obj = { - name: string +export type ObjectObject = { + encryptionKey: EncryptionKey + slabs?: SlabSlice[] +} + +export type ObjectMetadata = { + key: string size: number health: number - key: EncryptionKey - eTag: string - mimeType: string + eTag?: string modTime: string - slabs?: SlabSlice[] + mimeType?: string } +export type ObjectUserMetadata = Record + +export type Obj = ObjectMetadata & + ObjectObject & { + metadata: ObjectUserMetadata + } + export type ContractSetSettings = { default: string }