filecoin-pin
Version:
Bridge IPFS content to Filecoin Onchain Cloud using familiar tools
93 lines • 3.58 kB
JavaScript
import { METADATA_KEYS } from '@filoz/synapse-sdk';
/**
* Get the direct download URL for a piece from a provider
*/
export function getDownloadURL(providerInfo, pieceCid) {
const serviceURL = providerInfo.products?.PDP?.data?.serviceURL;
return serviceURL ? `${serviceURL.replace(/\/$/, '')}/piece/${pieceCid}` : '';
}
/**
* Get the service URL from provider info
*/
export function getServiceURL(providerInfo) {
return providerInfo.products?.PDP?.data?.serviceURL ?? '';
}
/**
* Upload a CAR file to Filecoin via Synapse.
*
* This function encapsulates the common upload pattern:
* 1. Submit CAR data to Synapse storage
* 2. Track upload progress via callbacks
* 3. Return piece information
*
* @param synapseService - Initialized Synapse service
* @param carData - CAR file data as Uint8Array
* @param rootCid - The IPFS root CID to associate with this piece
* @param logger - Logger instance for tracking
* @param options - Optional callbacks and context
* @returns Upload result with piece information
*/
export async function uploadToSynapse(synapseService, carData, rootCid, logger, options = {}) {
const { onProgress, contextId = 'upload' } = options;
// Merge provided callbacks with logging callbacks
const uploadCallbacks = {
onUploadComplete: (pieceCid) => {
logger.info({
event: 'synapse.upload.piece_uploaded',
contextId,
pieceCid: pieceCid.toString(),
}, 'Upload to PDP server complete');
onProgress?.({ type: 'onUploadComplete', data: { pieceCid } });
},
onPieceAdded: (txHash) => {
if (txHash != null) {
logger.info({
event: 'synapse.upload.piece_added',
contextId,
txHash: txHash,
}, 'Piece addition transaction submitted');
}
else {
logger.info({
event: 'synapse.upload.piece_added',
contextId,
}, 'Piece added to data set');
}
onProgress?.({ type: 'onPieceAdded', data: { txHash } });
},
onPieceConfirmed: (pieceIds) => {
logger.info({
event: 'synapse.upload.piece_confirmed',
contextId,
pieceIds,
}, 'Piece addition confirmed on-chain');
onProgress?.({ type: 'onPieceConfirmed', data: { pieceIds } });
},
};
// Upload using Synapse with IPFS root CID metadata
const uploadOptions = {
...uploadCallbacks,
metadata: {
...(options.pieceMetadata ?? {}),
[METADATA_KEYS.IPFS_ROOT_CID]: rootCid.toString(), // Associate piece with IPFS root CID
},
context: synapseService.storage,
};
const synapseResult = await synapseService.synapse.storage.upload(carData, uploadOptions);
// Log success
logger.info({
event: 'synapse.upload.success',
contextId,
pieceCid: synapseResult.pieceCid,
pieceId: synapseResult.pieceId,
dataSetId: synapseService.storage.dataSetId,
}, 'Successfully uploaded to Filecoin with Synapse');
const result = {
pieceCid: synapseResult.pieceCid.toString(),
pieceId: synapseResult.pieceId !== undefined ? Number(synapseResult.pieceId) : undefined,
dataSetId: String(synapseService.storage.dataSetId),
providerInfo: synapseService.providerInfo,
};
return result;
}
//# sourceMappingURL=synapse.js.map