(
contents: {path: string; content: string}[],
authToken?: string,
isMultichain?: boolean,
ipfs?: IPFSHTTPClientLite
)
| 133 | const fileMap = new Map<string | fs.ReadStream, Promise<string>>(); |
| 134 | |
| 135 | export async function uploadFiles( |
| 136 | contents: {path: string; content: string}[], |
| 137 | authToken?: string, |
| 138 | isMultichain?: boolean, |
| 139 | ipfs?: IPFSHTTPClientLite |
| 140 | ): Promise<Map<string, string>> { |
| 141 | const fileCidMap: Map<string, string> = new Map(); |
| 142 | |
| 143 | if (ipfs) { |
| 144 | try { |
| 145 | const results = await ipfs.addAll(contents, {wrapWithDirectory: isMultichain}); |
| 146 | |
| 147 | for (const result of results) { |
| 148 | fileCidMap.set(result.path, result.cid.toString()); |
| 149 | } |
| 150 | } catch (e) { |
| 151 | throw new Error(`Publish project to provided IPFS gateway failed`, {cause: e}); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const ipfsWrite = new IPFSHTTPClientLite({ |
| 156 | url: IPFS_WRITE_ENDPOINT, |
| 157 | headers: authToken ? {Authorization: `Bearer ${authToken}`} : undefined, |
| 158 | }); |
| 159 | |
| 160 | try { |
| 161 | const results = await ipfsWrite.addAll(contents, {pin: true, cidVersion: 0, wrapWithDirectory: isMultichain}); |
| 162 | for (const result of results) { |
| 163 | fileCidMap.set(result.path, result.cid); |
| 164 | |
| 165 | await ipfsWrite.pinRemoteAdd(result.cid, {service: PIN_SERVICE}).catch((e) => { |
| 166 | console.warn( |
| 167 | `Failed to pin file ${result.path}. There might be problems with this file being accessible later. ${e}` |
| 168 | ); |
| 169 | }); |
| 170 | } |
| 171 | } catch (e) { |
| 172 | throw new Error(`Publish project files to IPFS failed`, {cause: e}); |
| 173 | } |
| 174 | |
| 175 | return fileCidMap; |
| 176 | } |
| 177 | |
| 178 | export async function uploadFile( |
| 179 | contents: {path: string; content: string}, |
no test coverage detected