MCPcopy
hub / github.com/streetwriters/notesnook / uploadFile

Function uploadFile

apps/web/src/interfaces/fs.ts:240–294  ·  view source on GitHub ↗
(
  filename: string,
  requestOptions: RequestOptionsWithSignal
)

Source from the content-addressed store, hash-verified

238};
239
240async 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
296async function singlePartUploadFile(
297 fileHandle: FileHandle,

Callers

nothing calls this directly

Calls 13

singlePartUploadFileFunction · 0.85
multiPartUploadFileFunction · 0.85
reportProgressFunction · 0.85
toS3ErrorFunction · 0.85
resetUploadFunction · 0.85
showErrorFunction · 0.85
addAdditionalDataMethod · 0.80
existsFunction · 0.70
getUploadedFileSizeFunction · 0.70
checkUploadFunction · 0.70
readFileMethod · 0.65
sizeMethod · 0.45

Tested by

no test coverage detected