( filename: string, requestOptions: RequestOptionsWithSignal )
| 238 | }; |
| 239 | |
| 240 | async function uploadFile( |
| 241 | filename: string, |
| 242 | requestOptions: RequestOptionsWithSignal |
| 243 | ) { |
| 244 | const fileHandle = await streamablefs.readFile(filename); |
| 245 | if (!fileHandle || !(await exists(fileHandle))) |
| 246 | throw new Error( |
| 247 | `File is corrupt or missing data. Please upload the file again. (File hash: ${filename})` |
| 248 | ); |
| 249 | if (fileHandle.file.additionalData?.uploaded) return true; |
| 250 | |
| 251 | // if file already exists on the server, we just return true |
| 252 | // we don't reupload the file i.e. overwriting is not possible. |
| 253 | const uploadedFileSize = await getUploadedFileSize(filename); |
| 254 | if (uploadedFileSize === -1) return false; |
| 255 | if (uploadedFileSize > 0 && uploadedFileSize === (await fileHandle.size())) |
| 256 | return true; |
| 257 | |
| 258 | try { |
| 259 | const uploaded = |
| 260 | fileHandle.file.size < MINIMUM_MULTIPART_FILE_SIZE |
| 261 | ? await singlePartUploadFile(fileHandle, filename, requestOptions) |
| 262 | : await multiPartUploadFile(fileHandle, filename, requestOptions); |
| 263 | |
| 264 | if (uploaded) { |
| 265 | await checkUpload( |
| 266 | filename, |
| 267 | requestOptions.chunkSize, |
| 268 | fileHandle.file.size |
| 269 | ); |
| 270 | await fileHandle.addAdditionalData("uploaded", true); |
| 271 | } |
| 272 | |
| 273 | return uploaded; |
| 274 | } catch (e) { |
| 275 | console.error(e); |
| 276 | reportProgress(undefined, { type: "upload", hash: filename }); |
| 277 | const error = toS3Error(e); |
| 278 | if ( |
| 279 | [ |
| 280 | "NoSuchKey", |
| 281 | "NoSuchUpload", |
| 282 | "IncompleteBody", |
| 283 | "InternalError", |
| 284 | "InvalidObjectState", |
| 285 | "InvalidPart", |
| 286 | "InvalidPartOrder", |
| 287 | "SignatureDoesNotMatch" |
| 288 | ].includes(error.Code) |
| 289 | ) |
| 290 | await resetUpload(fileHandle); |
| 291 | showError(error); |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | async function singlePartUploadFile( |
| 297 | fileHandle: FileHandle, |
nothing calls this directly
no test coverage detected