Skip to content

Commit

Permalink
Do not calculate CID when wrapWithDirectory is true
Browse files Browse the repository at this point in the history
  • Loading branch information
jakapurg committed Dec 2, 2024
1 parent 21fb11c commit ac3352d
Showing 1 changed file with 70 additions and 43 deletions.
113 changes: 70 additions & 43 deletions packages/sdk/src/util/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,61 +53,88 @@ export async function uploadFiles(uploadParams: {
const uploadedFiles = [];

for (const fileGroup of chunkify(files, fileChunkSize)) {
const metadata = {
files: [] as FileUploadResult[],
urls: [] as string[],
cids: [] as string[],
};
if (params.wrapWithDirectory) {
for (const fg of fileGroup) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { content, ...rest } = fg;

const readContent = fg.index ? fs.readFileSync(fg.index) : fg.content;

fg.content = readContent;
}

const { files } = await ApillonApi.post<IFileUploadResponse>(
`${apiPrefix}/upload`,
{
files: fileGroup.map((fg) => {
// Remove content property from the payload
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { content, ...rest } = fg;
return rest;
}),
sessionUuid,
},
);

for (const fg of fileGroup) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { content, ...rest } = fg;
await uploadFilesToS3(files, fileGroup);

metadata.files.push(rest);
uploadedFiles.push(files);
} else {
const metadata = {
files: [] as FileUploadResult[],
urls: [] as string[],
cids: [] as string[],
};

const readContent = fg.index ? fs.readFileSync(fg.index) : fg.content;
for (const fg of fileGroup) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { content, ...rest } = fg;

fg.content = readContent;
metadata.files.push(rest);

const cid = await ipfsHash.of(readContent, {
cidVersion: 1,
});
const readContent = fg.index ? fs.readFileSync(fg.index) : fg.content;

metadata.cids.push(cid);
}
fg.content = readContent;

const { links } = await ApillonApi.post<{ links: string[] }>(
`/storage/link-on-ipfs-multiple`,
{
cids: metadata.cids,
},
);
const cid = await ipfsHash.of(readContent, {
cidVersion: 1,
});

metadata.urls = links;
metadata.cids.push(cid);
}

const { files } = await ApillonApi.post<IFileUploadResponse>(
`${apiPrefix}/upload`,
{
files: metadata.files,
sessionUuid,
},
);
const { links } = await ApillonApi.post<{ links: string[] }>(
`/storage/link-on-ipfs-multiple`,
{
cids: metadata.cids,
},
);

// Upload doesn't return files in the same order as sent
const sortedFiles = metadata.files.map((metaFile) => {
return files.find((file) => file.fileName === metaFile.fileName);
});
metadata.urls = links;
const { files } = await ApillonApi.post<IFileUploadResponse>(
`${apiPrefix}/upload`,
{
files: metadata.files,
sessionUuid,
},
);

await uploadFilesToS3(sortedFiles, fileGroup);
// Upload doesn't return files in the same order as sent
const sortedFiles = metadata.files.map((metaFile) => {
return files.find((file) => file.fileName === metaFile.fileName);
});

const filesWithUrl = sortedFiles.map((file, index) => {
return {
...file,
CID: metadata.cids[index],
url: metadata.urls[index],
};
});
uploadedFiles.push(filesWithUrl);
await uploadFilesToS3(sortedFiles, fileGroup);

const filesWithUrl = sortedFiles.map((file, index) => {
return {
...file,
CID: metadata.cids[index],
url: metadata.urls[index],
};
});
uploadedFiles.push(filesWithUrl);
}
}

ApillonLogger.logWithTime('File upload complete.');
Expand Down

0 comments on commit ac3352d

Please sign in to comment.