generated from oveddan/scaffold-eth-ts-tailwind
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathipfsSceneLoader.ts
136 lines (112 loc) · 3.17 KB
/
ipfsSceneLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Web3File } from "web3.storage";
import { SceneAndFiles, StoredSceneAndFiles } from "../../types/scene";
import {
FileLocationKindLocal,
FileLocationKindStored,
FileLocationLocal,
FileLocationStored,
SceneFilesLocal,
SceneFilesStored,
} from "../../types/shared";
import { metadataFileName } from "./ipfsSceneSaver";
import { makeWeb3StorageClient } from "./web3Storage";
async function toLocalFile(
fileLocation: FileLocationStored,
files: Web3File[],
folderCid: string | undefined
): Promise<FileLocationLocal> {
if (fileLocation.kind === FileLocationKindStored.https)
return {
kind: FileLocationKindLocal.https,
url: fileLocation.url,
};
if (fileLocation.kind === FileLocationKindStored.local) {
const file = files.find((x) => x.name === fileLocation.path);
if (!file)
throw new Error(
`did not get file for stored file with id ${fileLocation.path}`
);
return {
kind: FileLocationKindLocal.ipfs,
file,
cid: file.cid,
url: `ipfs://${folderCid}/${fileLocation.path}`,
};
}
if (fileLocation.kind === FileLocationKindStored.ipfs) {
const files = await (
await makeWeb3StorageClient().get(fileLocation.cid)
)?.files();
if (!files || !files[0])
throw new Error(`could not get file for cid ${fileLocation.cid}`);
return {
kind: FileLocationKindLocal.ipfs,
file: files[0],
cid: fileLocation.cid,
url: fileLocation.url,
};
}
throw new Error("unknown file location kind");
}
async function storedFilesToLocal(
storedFiles: SceneFilesStored,
files: Web3File[],
folderCid: string | undefined
): Promise<SceneFilesLocal> {
const localFiles = await Promise.all(
Object.entries(storedFiles).map(async ([id, configuredFile]) => {
const localFile = await toLocalFile(configuredFile, files, folderCid);
return { id, localFile };
})
);
return localFiles.reduce(
(acc: SceneFilesLocal, { id, localFile }): SceneFilesLocal => {
return {
...acc,
[id]: localFile,
};
},
{}
);
}
export const loadStoredSceneAndFilesFromIpfs = async (
cid: string
): Promise<
StoredSceneAndFiles & {
storedFiles: Web3File[];
}
> => {
const client = makeWeb3StorageClient();
const res = await client.get(cid);
if (!res?.ok) {
throw new Error(
`failed to get ${cid} - [${res?.status}] ${res?.statusText}`
);
}
const storedFiles = await res?.files();
const metadataFile = storedFiles.find((x) => x.name === metadataFileName);
if (!metadataFile)
throw new Error("missing metadata.json files from archive");
const metadataText = await metadataFile.text();
const { scene, files } = JSON.parse(metadataText) as StoredSceneAndFiles;
return {
scene,
files,
storedFiles,
};
};
const loadSceneFromIpfs = async (
cid: string,
handleProgress: (progress: number) => void
): Promise<SceneAndFiles> => {
const { scene, files, storedFiles } = await loadStoredSceneAndFilesFromIpfs(
cid
);
const localFiles = await storedFilesToLocal(files, storedFiles, cid);
return {
scene,
files: localFiles,
cid,
};
};
export default loadSceneFromIpfs;