Skip to content

Commit 1fb46b1

Browse files
author
Al Manning
committed
nested attachment uploads
1 parent 677782f commit 1fb46b1

File tree

4 files changed

+135
-63
lines changed

4 files changed

+135
-63
lines changed

src/publish/confluence/confluence-helper.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -599,26 +599,33 @@ export const findAttachments = (
599599

600600
export const getAttachmentsDirectory = (
601601
baseDirectory: string,
602-
fileName: string = "",
603-
attachmentsToUpload: string[] = []
602+
filePath: string = "",
603+
attachmentPath: string = ""
604604
): string => {
605-
if (attachmentsToUpload.length === 0 || fileName.length === 0) {
605+
let result = baseDirectory;
606+
607+
if (attachmentPath.length === 0 || filePath.length === 0) {
606608
return "";
607609
}
608610

609-
let result = baseDirectory;
611+
const filePathList = filePath.split("/");
612+
const attachmentPathList = attachmentPath.split("/");
613+
614+
const pathNoFileFromList = (pathList: string[]) =>
615+
pathList.slice(0, pathList.length - 1).join("/");
616+
617+
if (attachmentPathList.some((path) => path.endsWith("_files"))) {
618+
return baseDirectory;
619+
}
610620

611621
if (result.endsWith("/_site")) {
612622
result = result.slice(0, -6);
613623
}
614624

615-
const filePathList = fileName.split("/");
616-
617-
if (filePathList.length > 1) {
618-
const directoryPath = filePathList
619-
.slice(0, filePathList.length - 1)
620-
.join("/");
625+
const isRelative = attachmentPathList.length === 1;
621626

627+
if (isRelative && filePathList.length > 1) {
628+
const directoryPath = pathNoFileFromList(filePathList);
622629
result = `${result}/${directoryPath}`;
623630
}
624631

src/publish/confluence/confluence.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -281,21 +281,34 @@ async function publish(
281281

282282
const uploadAttachments = (
283283
baseDirectory: string,
284-
pathList: string[],
284+
attachmentsToUpload: string[],
285285
parentId: string,
286+
filePath: string,
286287
existingAttachments: AttachmentSummary[] = []
287288
): Promise<AttachmentSummary | null>[] => {
288289
const uploadAttachment = async (
289-
pathToUpload: string
290+
attachmentPath: string
290291
): Promise<AttachmentSummary | null> => {
292+
const uploadDirectory = getAttachmentsDirectory(
293+
baseDirectory,
294+
filePath,
295+
attachmentPath
296+
);
297+
291298
trace(
292299
"uploadAttachment",
293-
{ baseDirectory, pathList, parentId, existingAttachments },
300+
{
301+
baseDirectory,
302+
pathList: attachmentsToUpload,
303+
parentId,
304+
existingAttachments,
305+
},
294306
LogPrefix.ATTACHMENT
295307
);
296308
let fileBuffer: Uint8Array;
297309
let fileHash: string;
298-
const path = join(baseDirectory, pathToUpload);
310+
const path = join(uploadDirectory, attachmentPath);
311+
299312
try {
300313
fileBuffer = await Deno.readFile(path);
301314
fileHash = md5Hash(fileBuffer.toString());
@@ -304,7 +317,7 @@ async function publish(
304317
return null;
305318
}
306319

307-
const fileName = pathToUpload;
320+
const fileName = attachmentPath;
308321

309322
const existingDuplicateAttachment = existingAttachments.find(
310323
(attachment: AttachmentSummary) => {
@@ -330,7 +343,7 @@ async function publish(
330343
return attachment;
331344
};
332345

333-
return pathList.map(uploadAttachment);
346+
return attachmentsToUpload.map(uploadAttachment);
334347
};
335348

336349
const updateContent = async (
@@ -342,7 +355,10 @@ async function publish(
342355
): Promise<Content> => {
343356
const previousPage = await client.getContent(id);
344357

345-
const attachmentsToUpload: string[] = findAttachments(body.storage.value);
358+
const attachmentsToUpload: string[] = findAttachments(
359+
body.storage.value,
360+
publishFiles.files
361+
);
346362

347363
trace("attachmentsToUpload", attachmentsToUpload, LogPrefix.ATTACHMENT);
348364

@@ -367,23 +383,18 @@ async function publish(
367383
const existingAttachments: AttachmentSummary[] =
368384
await client.getAttachments(toUpdate.id);
369385

370-
const baseDirectory = getAttachmentsDirectory(
371-
publishFiles.baseDir,
372-
fileName,
373-
attachmentsToUpload
374-
);
375-
376386
trace(
377387
"attachments",
378-
{ existingAttachments, attachmentsToUpload, baseDirectory },
388+
{ existingAttachments, attachmentsToUpload },
379389
LogPrefix.ATTACHMENT
380390
);
381391

382392
const uploadAttachmentsResult = await Promise.all(
383393
uploadAttachments(
384-
baseDirectory,
394+
publishFiles.baseDir,
385395
attachmentsToUpload,
386396
toUpdate.id,
397+
fileName,
387398
existingAttachments
388399
)
389400
);
@@ -474,8 +485,6 @@ async function publish(
474485
createParent: ConfluenceParent = parent,
475486
fileName: string = ""
476487
): Promise<Content> => {
477-
//TODO check why files are always being uniquified
478-
479488
const createTitle = await uniquifyTitle(titleToCreate);
480489

481490
const attachmentsToUpload: string[] = findAttachments(
@@ -505,7 +514,8 @@ async function publish(
505514
uploadAttachments(
506515
publishFiles.baseDir,
507516
attachmentsToUpload,
508-
createdContent.id
517+
createdContent.id,
518+
fileName
509519
)
510520
);
511521
trace(

src/publish/confluence/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ export const V2EDITOR_METADATA = {
1212
},
1313
};
1414

15-
export const DELETE_SLEEP_MILLIS = 5000; //TODO replace with polling
15+
export const DELETE_SLEEP_MILLIS = 1000; //TODO replace with polling

0 commit comments

Comments
 (0)