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

Method uploadFiles

packages/decap-cms-backend-bitbucket/src/API.ts:429–525  ·  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

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