Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix publish ipfs 503 #1331

Merged
merged 5 commits into from
Oct 11, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/cli/src/controller/publish-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ async function replaceFileReferences<T>(
const keys = Object.keys(input) as unknown as (keyof T)[];
await Promise.all(
keys.map(async (key) => {
// this is the loop
input[key] = await replaceFileReferences(projectDir, input[key], authToken, ipfs);
})
);
Expand All @@ -99,13 +100,14 @@ async function replaceFileReferences<T>(
return input;
}

const fileMap = new Map<string | fs.ReadStream, string>();

export async function uploadFile(
content: string | fs.ReadStream,
authToken: string,
ipfs?: IPFSHTTPClient
): Promise<string> {
let ipfsClientCid: string;
// if user provide ipfs, we will try to upload it to this gateway
if (ipfs) {
try {
ipfsClientCid = (await ipfs.add(content, {pin: true, cidVersion: 0})).cid.toString();
Expand All @@ -115,16 +117,21 @@ export async function uploadFile(
}
let ipfsClusterCid: string;
try {
ipfsClusterCid = await UploadFileByCluster(
determineStringOrFsStream(content) ? await fs.promises.readFile(content.path, 'utf8') : content,
authToken
);
if (fileMap.get(content)) {
stwiname marked this conversation as resolved.
Show resolved Hide resolved
ipfsClusterCid = fileMap.get(content);
} else {
ipfsClusterCid = await UploadFileByCluster(
determineStringOrFsStream(content) ? await fs.promises.readFile(content.path, 'utf8') : content,
authToken
);
fileMap.set(content, ipfsClusterCid);
}
} catch (e) {
throw new Error(`Publish project to default cluster failed, ${e}`);
}
// Validate IPFS cid
if (ipfsClientCid && ipfsClientCid !== ipfsClusterCid) {
throw new Error(`Published and received IPFS cid not identical \n,
throw new Error(`Published and received IPFS cid not identical \n,
IPFS gateway: ${ipfsClientCid}, IPFS cluster: ${ipfsClusterCid}`);
}
return ipfsClusterCid;
Expand Down