Skip to content

Commit abb366b

Browse files
authored
fix: upload nodes triggering (#525)
* fix: upload nodes triggering * feat: enhance artifact flattening in upload processing
1 parent b9cf2fd commit abb366b

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

apps/backend/src/core/objects/nodes.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const migrateFromBlockstoreToNodesTable = async (
152152
}
153153

154154
for (const upload of uploads) {
155-
const rootCID = await getUploadCID(uploadId)
155+
const headCID = await getUploadCID(upload.id)
156156
const blockstore = await getUploadBlockstore(upload.id)
157157

158158
const BATCH_SIZE = 100
@@ -170,7 +170,7 @@ const migrateFromBlockstoreToNodesTable = async (
170170
uniqueNodes.map((e) => ({
171171
cid: cidToString(e.cid),
172172
root_cid: cidToString(rootCID),
173-
head_cid: cidToString(rootCID),
173+
head_cid: cidToString(headCID),
174174
type: decodeIPLDNodeData(Buffer.from(e.block)).type,
175175
encoded_node: Buffer.from(e.block).toString('base64'),
176176
block_published_on: null,
@@ -179,6 +179,8 @@ const migrateFromBlockstoreToNodesTable = async (
179179
piece_offset: null,
180180
})),
181181
)
182+
183+
logger.info(`Saved nodes from headCID=${headCID}`)
182184
},
183185
BATCH_SIZE,
184186
)

apps/backend/src/core/uploads/uploadProcessing.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import {
1313
MetadataType,
1414
processBufferToIPLDFormatFromChunks,
1515
processChunksToIPLDFormat,
16+
OffchainMetadata,
1617
} from '@autonomys/auto-dag-data'
1718
import {
1819
FolderUpload,
1920
InteractionType,
2021
UploadType,
2122
UserWithOrganization,
23+
UploadArtifacts,
24+
FolderArtifacts,
2225
} from '@auto-drive/models'
2326
import { BlockstoreUseCases } from './blockstore.js'
2427
import { mapTableToModel } from './uploads.js'
@@ -203,6 +206,17 @@ const handleFileUploadFinalization = async (
203206
return metadata.dataCid
204207
}
205208

209+
const flattenArtifacts = (artifact: UploadArtifacts): OffchainMetadata[] => {
210+
const collected: OffchainMetadata[] = [artifact.metadata]
211+
if ((artifact as FolderArtifacts).childrenArtifacts) {
212+
const folder = artifact as FolderArtifacts
213+
for (const child of folder.childrenArtifacts) {
214+
collected.push(...flattenArtifacts(child))
215+
}
216+
}
217+
return collected
218+
}
219+
206220
const handleFolderUploadFinalization = async (
207221
user: UserWithOrganization,
208222
uploadId: string,
@@ -215,9 +229,13 @@ const handleFolderUploadFinalization = async (
215229
const { metadata, childrenArtifacts } =
216230
await UploadArtifactsUseCase.generateFolderArtifacts(uploadId)
217231

218-
const fullMetadata = [metadata, ...childrenArtifacts.map((e) => e.metadata)]
232+
const allMetadata: OffchainMetadata[] = [
233+
metadata,
234+
...childrenArtifacts.flatMap((child) => flattenArtifacts(child)),
235+
]
236+
219237
await Promise.all(
220-
fullMetadata.map((childMetadata) =>
238+
allMetadata.map((childMetadata) =>
221239
ObjectUseCases.saveMetadata(
222240
metadata.dataCid,
223241
childMetadata.dataCid,

apps/backend/src/core/uploads/uploads.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ const scheduleUploadTagging = async (cid: string): Promise<void> => {
334334

335335
const tagUpload = async (
336336
cid: string,
337+
isRoot: boolean = true,
337338
): Promise<Result<void, ObjectNotFoundError>> => {
338339
const getResult = await ObjectUseCases.getMetadata(cid)
339340
if (getResult.isErr()) {
@@ -344,7 +345,9 @@ const tagUpload = async (
344345
const metadata = getResult.value
345346
if (metadata?.type === 'folder') {
346347
const results = await Promise.all(
347-
metadata.children.map((child) => UploadsUseCases.tagUpload(child.cid)),
348+
metadata.children.map((child) =>
349+
UploadsUseCases.tagUpload(child.cid, false),
350+
),
348351
)
349352
const combinedResult = Result.combine(results)
350353
if (combinedResult.isErr()) {
@@ -365,7 +368,9 @@ const tagUpload = async (
365368
}
366369
}
367370

368-
await scheduleNodesPublish(cid)
371+
if (isRoot) {
372+
await scheduleNodesPublish(cid)
373+
}
369374

370375
return ok()
371376
}

0 commit comments

Comments
 (0)