MCPcopy Index your code
hub / github.com/atomicdata-dev/atomic-data-browser / uploadFiles

Function uploadFiles

lib/src/client.ts:171–209  ·  view source on GitHub ↗
(
  files: File[],
  store: Store,
  parent: string,
)

Source from the content-addressed store, hash-verified

169 * Returns the subjects of these newly created File resources.
170 */
171export async function uploadFiles(
172 files: File[],
173 store: Store,
174 parent: string,
175): Promise<string[]> {
176 const formData = new FormData();
177
178 files.map(file => {
179 formData.append('assets', file, file.name);
180 });
181
182 const uploadURL = new URL(store.getServerUrl() + '/upload');
183 uploadURL.searchParams.set('parent', parent);
184 const signedHeaders = await signRequest(
185 uploadURL.toString(),
186 store.getAgent(),
187 {},
188 );
189
190 const options = {
191 method: 'POST',
192 body: formData,
193 headers: signedHeaders,
194 };
195
196 const resp = await fetch(uploadURL.toString(), options);
197 const body = await resp.text();
198 if (resp.status !== 200) {
199 throw Error(body);
200 }
201 const json = JSON.parse(body);
202 const resources = parseJsonADArray(json);
203 const fileSubjects = [];
204 for (const r of resources) {
205 store.addResource(r);
206 fileSubjects.push(r.getSubject());
207 }
208 return fileSubjects;
209}

Callers 2

UploadFormFunction · 0.90
UploadWrapperFunction · 0.90

Calls 7

parseJsonADArrayFunction · 0.90
signRequestFunction · 0.85
getServerUrlMethod · 0.80
setMethod · 0.80
getAgentMethod · 0.80
addResourceMethod · 0.80
getSubjectMethod · 0.80

Tested by

no test coverage detected