MCPcopy
hub / github.com/subquery/subql / uploadFile

Function uploadFile

packages/cli/src/controller/publish-controller.ts:178–231  ·  view source on GitHub ↗
(
  contents: {path: string; content: string},
  authToken?: string,
  ipfs?: IPFSHTTPClientLite
)

Source from the content-addressed store, hash-verified

176}
177
178export async function uploadFile(
179 contents: {path: string; content: string},
180 authToken?: string,
181 ipfs?: IPFSHTTPClientLite
182): Promise<string> {
183 const pathPromise = fileMap.get(contents.path);
184 if (pathPromise !== undefined) {
185 return pathPromise;
186 }
187
188 let pendingClientCid: Promise<string> = Promise.resolve('');
189 if (ipfs) {
190 pendingClientCid = ipfs
191 .add(contents.content, {pin: true, cidVersion: 0})
192 .then((result) => result.cid.toString())
193 .catch((e) => {
194 throw new Error(`Publish file to provided IPFS failed`, {cause: e});
195 });
196 }
197
198 const ipfsWrite = new IPFSHTTPClientLite({
199 url: IPFS_WRITE_ENDPOINT,
200 headers: authToken ? {Authorization: `Bearer ${authToken}`} : undefined,
201 });
202
203 const pendingCid = ipfsWrite
204 .add(contents.content, {pin: true, cidVersion: 0})
205 .then((result) => result.cid)
206 .then(async (cid) => {
207 try {
208 await ipfsWrite.pinRemoteAdd(cid, {service: PIN_SERVICE});
209 return cid.toString();
210 } catch (e) {
211 console.warn(
212 `Failed to pin file ${contents.path}. There might be problems with this file being accessible later. ${e}`
213 );
214 return cid.toString();
215 }
216 })
217 .catch((e) => {
218 throw new Error(`Publish project to default IPFS failed`, {cause: e});
219 });
220
221 fileMap.set(contents.path, pendingCid);
222
223 const [cid, clientCid] = await Promise.all([pendingCid, pendingClientCid]);
224
225 if (clientCid && clientCid !== cid) {
226 throw new Error(`Published and received IPFS cid not identical \n,
227 Client IPFS: ${clientCid}, IPFS: ${cid}`);
228 }
229
230 return cid;
231}
232
233export function getDirectoryCid(fileToCidMap: Map<string, string>): string | undefined {
234 const directoryCid = fileToCidMap.get('');

Callers 1

replaceFileReferencesFunction · 0.85

Calls 5

addMethod · 0.95
pinRemoteAddMethod · 0.95
getMethod · 0.65
setMethod · 0.65
warnMethod · 0.45

Tested by

no test coverage detected