Skip to content

Commit 677782f

Browse files
author
Al Manning
committed
nested attachments
1 parent 1a72c3d commit 677782f

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

src/publish/confluence/confluence-helper.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,31 @@ export const findAttachments = (
596596

597597
return uniqueResult ?? [];
598598
};
599+
600+
export const getAttachmentsDirectory = (
601+
baseDirectory: string,
602+
fileName: string = "",
603+
attachmentsToUpload: string[] = []
604+
): string => {
605+
if (attachmentsToUpload.length === 0 || fileName.length === 0) {
606+
return "";
607+
}
608+
609+
let result = baseDirectory;
610+
611+
if (result.endsWith("/_site")) {
612+
result = result.slice(0, -6);
613+
}
614+
615+
const filePathList = fileName.split("/");
616+
617+
if (filePathList.length > 1) {
618+
const directoryPath = filePathList
619+
.slice(0, filePathList.length - 1)
620+
.join("/");
621+
622+
result = `${result}/${directoryPath}`;
623+
}
624+
625+
return result;
626+
};

src/publish/confluence/confluence.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
doWithSpinner,
5454
filterFilesForUpdate,
5555
findAttachments,
56+
getAttachmentsDirectory,
5657
getNextVersion,
5758
getTitle,
5859
isContentCreate,
@@ -336,7 +337,8 @@ async function publish(
336337
publishFiles: PublishFiles,
337338
id: string,
338339
body: ContentBody,
339-
titleParam: string = title
340+
titleParam: string = title,
341+
fileName: string = ""
340342
): Promise<Content> => {
341343
const previousPage = await client.getContent(id);
342344

@@ -364,15 +366,22 @@ async function publish(
364366
if (toUpdate.id) {
365367
const existingAttachments: AttachmentSummary[] =
366368
await client.getAttachments(toUpdate.id);
369+
370+
const baseDirectory = getAttachmentsDirectory(
371+
publishFiles.baseDir,
372+
fileName,
373+
attachmentsToUpload
374+
);
375+
367376
trace(
368377
"attachments",
369-
{ existingAttachments, attachmentsToUpload },
378+
{ existingAttachments, attachmentsToUpload, baseDirectory },
370379
LogPrefix.ATTACHMENT
371380
);
372381

373382
const uploadAttachmentsResult = await Promise.all(
374383
uploadAttachments(
375-
publishFiles.baseDir,
384+
baseDirectory,
376385
attachmentsToUpload,
377386
toUpdate.id,
378387
existingAttachments
@@ -661,7 +670,8 @@ async function publish(
661670
publishFiles,
662671
update.id ?? "",
663672
update.body,
664-
update.title ?? ""
673+
update.title ?? "",
674+
update.fileName ?? ""
665675
);
666676
} else if (isContentDelete(change)) {
667677
if (DELETE_DISABLED) {

tests/unit/confluence.test.ts

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
filterFilesForUpdate,
1717
findAttachments,
1818
findPagesToDelete,
19+
getAttachmentsDirectory,
1920
getMessageFromAPIError,
2021
getNextVersion,
2122
getTitle,
@@ -2907,6 +2908,75 @@ const runFindAttachments = () => {
29072908
});
29082909
};
29092910

2911+
const runGetAttachmentsDirectory = () => {
2912+
const suiteLabel = (label: string) => `GetAttachmentsDirectory_${label}`;
2913+
2914+
const check = (
2915+
expected: string,
2916+
baseDirectory: string,
2917+
attachmentsToUpload: string[],
2918+
fileName: string
2919+
) => {
2920+
assertEquals(
2921+
getAttachmentsDirectory(baseDirectory, fileName, attachmentsToUpload),
2922+
expected
2923+
);
2924+
};
2925+
2926+
unitTest(suiteLabel("empty_to_upload"), async () => {
2927+
const expected = "";
2928+
const baseDirectory = "/Users/fake-base";
2929+
check(expected, baseDirectory, [], "file-name.png");
2930+
});
2931+
2932+
unitTest(suiteLabel("empty_fileName"), async () => {
2933+
const expected = "";
2934+
const attachmentsToUpload = ["file1.png", "file2.png"];
2935+
const baseDirectory = "/Users/fake-base";
2936+
check(expected, baseDirectory, attachmentsToUpload, "");
2937+
});
2938+
2939+
unitTest(suiteLabel("simple"), async () => {
2940+
const expected = "/Users/fake-base";
2941+
2942+
const baseDirectory = "/Users/fake-base";
2943+
const attachmentsToUpload = ["file1.png"];
2944+
const fileName = "fake-file.xml";
2945+
2946+
check(expected, baseDirectory, attachmentsToUpload, fileName);
2947+
});
2948+
2949+
unitTest(suiteLabel("site_in_root"), async () => {
2950+
const expected = "/Users/fake-base";
2951+
2952+
const baseDirectory = "/Users/fake-base/_site";
2953+
const attachmentsToUpload = ["file1.png"];
2954+
const fileName = "fake-file.xml";
2955+
2956+
check(expected, baseDirectory, attachmentsToUpload, fileName);
2957+
});
2958+
2959+
unitTest(suiteLabel("site_nested"), async () => {
2960+
const expected = "/Users/fake-base/fake-parent";
2961+
2962+
const baseDirectory = "/Users/fake-base/_site";
2963+
const attachmentsToUpload = ["file1.png"];
2964+
const fileName = "fake-parent/fake-file.xml";
2965+
2966+
check(expected, baseDirectory, attachmentsToUpload, fileName);
2967+
});
2968+
2969+
unitTest(suiteLabel("site_nested_multi"), async () => {
2970+
const expected = "/Users/fake-base/fake-grand-parent/fake-parent";
2971+
2972+
const baseDirectory = "/Users/fake-base/_site";
2973+
const attachmentsToUpload = ["file1.png"];
2974+
const fileName = "fake-grand-parent/fake-parent/fake-file.xml";
2975+
2976+
check(expected, baseDirectory, attachmentsToUpload, fileName);
2977+
});
2978+
};
2979+
29102980
const runUpdateImagePathsForContentBody = () => {
29112981
const suiteLabel = (label: string) =>
29122982
`UpdateImagePathsForContentBody_${label}`;
@@ -2974,9 +3044,8 @@ if (runAllTests) {
29743044
runExtractLinks();
29753045
runUpdateLinks();
29763046
runFindAttachments();
3047+
runGetAttachmentsDirectory();
29773048
runUpdateImagePathsForContentBody();
29783049
} else {
2979-
// runSpaceCreatesWithNesting();
2980-
// runSpaceUpdatesWithNesting();
2981-
runSpaceUpdatesWithNestedMoves();
3050+
runGetAttachmentsDirectory();
29823051
}

0 commit comments

Comments
 (0)