MCPcopy Index your code
hub / github.com/subquery/subql / replaceFileReferences

Function replaceFileReferences

packages/cli/src/controller/publish-controller.ts:100–131  ·  view source on GitHub ↗
(
  projectDir: string,
  input: T,
  authToken?: string,
  ipfs?: IPFSHTTPClientLite
)

Source from the content-addressed store, hash-verified

98
99/* Recursively finds all FileReferences in an object and replaces the files with IPFS references */
100async function replaceFileReferences<T extends Record<string, any>>(
101 projectDir: string,
102 input: T,
103 authToken?: string,
104 ipfs?: IPFSHTTPClientLite
105): Promise<T> {
106 if (Array.isArray(input)) {
107 return (await Promise.all(
108 input.map((val) => replaceFileReferences(projectDir, val, authToken, ipfs))
109 )) as unknown as T;
110 } else if (typeof input === 'object' && input !== null) {
111 if (input instanceof Map) {
112 input = mapToObject(input) as T;
113 }
114 if (isFileReference(input)) {
115 const filePath = path.resolve(projectDir, input.file);
116 const content = fs.readFileSync(path.resolve(projectDir, input.file));
117 input.file = await uploadFile({content: content.toString(), path: filePath}, authToken, ipfs).then(
118 (cid) => `ipfs://${cid}`
119 );
120 }
121 const keys = Object.keys(input).filter((key) => key !== '_deployment') as unknown as (keyof T)[];
122 await Promise.all(
123 keys.map(async (key) => {
124 // this is the loop
125 input[key] = await replaceFileReferences(projectDir, input[key], authToken, ipfs);
126 })
127 );
128 }
129
130 return input;
131}
132
133const fileMap = new Map<string | fs.ReadStream, Promise<string>>();
134

Callers 1

uploadToIpfsFunction · 0.85

Calls 4

mapToObjectFunction · 0.90
isFileReferenceFunction · 0.90
uploadFileFunction · 0.85
mapMethod · 0.80

Tested by

no test coverage detected