MCPcopy Create free account
hub / github.com/decaporg/decap-cms / uploadFiles

Method uploadFiles

packages/decap-cms-backend-bitbucket/src/API.ts:426–522  ·  view source on GitHub ↗
(
    files: { path: string; newPath?: string; delete?: boolean }[],
    {
      commitMessage,
      branch,
      parentSha,
      hasSubfolders = true,
    }: { commitMessage: string; branch: string; parentSha?: string; hasSubfolders?: boolean },
  )

Source from the content-addressed store, hash-verified

424 };
425
426 async uploadFiles(
427 files: { path: string; newPath?: string; delete?: boolean }[],
428 {
429 commitMessage,
430 branch,
431 parentSha,
432 hasSubfolders = true,
433 }: { commitMessage: string; branch: string; parentSha?: string; hasSubfolders?: boolean },
434 ) {
435 const formData = new FormData();
436 const toMove: { from: string; to: string; contentBlob: Blob; hasSubfolders: boolean }[] = [];
437 files.forEach(file => {
438 if (file.delete) {
439 // delete the file
440 formData.append('files', file.path);
441 } else if (file.newPath) {
442 const contentBlob = get(file, 'fileObj', new Blob([(file as DataFile).raw]));
443 toMove.push({ from: file.path, to: file.newPath, contentBlob, hasSubfolders });
444 } else {
445 // add/modify the file
446 const contentBlob = get(file, 'fileObj', new Blob([(file as DataFile).raw]));
447 // Third param is filename header, in case path is `message`, `branch`, etc.
448 formData.append(file.path, contentBlob, basename(file.path));
449 }
450 });
451 for (const { from, to, contentBlob, hasSubfolders } of toMove) {
452 if (!hasSubfolders) {
453 // New behavior (subfolders: false): Only move the specific file
454 // to move a file in Bitbucket we need to delete the old path
455 // and upload the file content to the new path
456 // NOTE: this is very wasteful, and also the Bitbucket `diff` API
457 // reports these files as deleted+added instead of renamed
458 // delete current path
459 formData.append('files', from);
460 // create in new path
461 formData.append(to, contentBlob, basename(to));
462 } else {
463 // Legacy behavior (subfolders: true, default): Move all files in the directory
464 const sourceDir = dirname(from);
465 const destDir = dirname(to);
466 const filesBranch = parentSha ? this.branch : branch;
467 const files = await this.listAllFiles(sourceDir, 100, filesBranch);
468 for (const file of files) {
469 // to move a file in Bitbucket we need to delete the old path
470 // and upload the file content to the new path
471 // NOTE: this is very wasteful, and also the Bitbucket `diff` API
472 // reports these files as deleted+added instead of renamed
473 // delete current path
474 formData.append('files', file.path);
475 // create in new path
476 const content =
477 file.path === from
478 ? contentBlob
479 : await this.readFile(file.path, null, {
480 branch: filesBranch,
481 parseText: false,
482 });
483 formData.append(

Callers 2

persistFilesMethod · 0.95
editorialWorkflowGitMethod · 0.95

Calls 7

readFileMethod · 0.95
getBranchMethod · 0.95
basenameFunction · 0.90
replaceMethod · 0.80
getFunction · 0.50
listAllFilesMethod · 0.45

Tested by

no test coverage detected