( files: File[], store: Store, parent: string, )
| 169 | * Returns the subjects of these newly created File resources. |
| 170 | */ |
| 171 | export 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 | } |
no test coverage detected