Skip to content

Commit aa5349c

Browse files
author
Al Manning
committed
window support for finding attachments to upload
1 parent f027677 commit aa5349c

File tree

3 files changed

+182
-20
lines changed

3 files changed

+182
-20
lines changed

src/publish/confluence/confluence-helper.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { ApiError, PublishRecord } from "../types.ts";
22
import { ensureTrailingSlash } from "../../core/path.ts";
3+
import {
4+
join,
5+
basename,
6+
parse,
7+
dirname,
8+
toFileUrl,
9+
resolve,
10+
} from "path/mod.ts";
311
import { isHttpUrl } from "../../core/url.ts";
412
import { AccountToken, InputMetadata } from "../provider.ts";
513
import {
@@ -614,8 +622,10 @@ export const updateImagePaths = (body: ContentBody): ContentBody => {
614622
export const findAttachments = (
615623
bodyValue: string,
616624
publishFiles: string[] = [],
617-
filePath: string = ""
625+
filePathParam: string = ""
618626
): string[] => {
627+
const filePath = filePathParam.replaceAll("\\", "/");
628+
619629
const pathList = filePath.split("/");
620630
const parentPath = pathList.slice(0, pathList.length - 1).join("/");
621631

@@ -624,17 +634,22 @@ export const findAttachments = (
624634

625635
if (publishFiles.length > 0) {
626636
uniqueResult = uniqueResult.map((assetFileName: string) => {
627-
const assetInPublishFiles = publishFiles.find((assetPath) => {
628-
return assetPath.endsWith(`${parentPath}/${assetFileName}`);
637+
const assetInPublishFiles = publishFiles.find((assetPathParam) => {
638+
const assetPath = assetPathParam.replaceAll("\\", "/");
639+
640+
const toCheck = join(parentPath, assetFileName);
641+
642+
return assetPath === toCheck;
629643
});
644+
630645
return assetInPublishFiles ?? assetFileName;
631646
});
632647
}
633648

634649
return uniqueResult ?? [];
635650
};
636651

637-
export const getAttachmentsDirectory = (
652+
export const getAttachmentsDirectoryOld = (
638653
baseDirectory: string,
639654
filePath: string = "",
640655
attachmentPath: string = ""
@@ -645,6 +660,7 @@ export const getAttachmentsDirectory = (
645660
return "";
646661
}
647662

663+
//FIXME use DENO
648664
const filePathList = filePath.split("/");
649665
let attachmentPathList = attachmentPath.split("/");
650666

@@ -673,3 +689,23 @@ export const getAttachmentsDirectory = (
673689

674690
return result;
675691
};
692+
693+
export const getAttachmentsDirectory = (
694+
baseDirectory: string,
695+
filePath: string = "",
696+
attachmentPath: string = ""
697+
): string => {
698+
let result = baseDirectory;
699+
700+
const baseParse = parse(baseDirectory);
701+
const baseDirectoryBaseName = basename(baseDirectory);
702+
703+
if (attachmentPath.length === 0 || filePath.length === 0) {
704+
return "";
705+
}
706+
707+
if (baseDirectoryBaseName === "_site") {
708+
}
709+
710+
return result;
711+
};

src/publish/confluence/confluence.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,25 +290,33 @@ async function publish(
290290
const uploadAttachment = async (
291291
attachmentPath: string
292292
): Promise<AttachmentSummary | null> => {
293-
const uploadDirectory = getAttachmentsDirectory(
294-
baseDirectory,
295-
filePath,
296-
attachmentPath
297-
);
293+
// const uploadDirectory = getAttachmentsDirectory(
294+
// baseDirectory,
295+
// filePath,
296+
// attachmentPath
297+
// );
298+
//
299+
//
300+
//
301+
//
302+
//
303+
304+
let fileBuffer: Uint8Array;
305+
let fileHash: string;
306+
const path = join(baseDirectory, attachmentPath);
298307

299308
trace(
300309
"uploadAttachment",
301310
{
302311
baseDirectory,
303-
pathList: attachmentsToUpload,
312+
attachmentPath,
313+
attachmentsToUpload,
304314
parentId,
305315
existingAttachments,
316+
path,
306317
},
307318
LogPrefix.ATTACHMENT
308319
);
309-
let fileBuffer: Uint8Array;
310-
let fileHash: string;
311-
const path = join(uploadDirectory, attachmentPath);
312320

313321
try {
314322
fileBuffer = await Deno.readFile(path);
@@ -362,8 +370,6 @@ async function publish(
362370
fileName
363371
);
364372

365-
//FIXME expected elephant.png
366-
367373
const uniqueTitle = await uniquifyTitle(titleToUpdate, id);
368374

369375
trace("attachmentsToUpload", attachmentsToUpload, LogPrefix.ATTACHMENT);

tests/unit/confluence.test.ts

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (C) 2020 by RStudio, PBC
44
*
55
*/
6+
import { posix, win32 } from "path/mod.ts";
67
import { unitTest } from "../test.ts";
78
import { assertEquals, assertThrows } from "testing/asserts.ts";
89

@@ -2833,7 +2834,7 @@ const runUpdateLinks = () => {
28332834
check(expected, changes, fileMetadataTable);
28342835
});
28352836

2836-
otest(suiteLabel("one_update_link_nested_absolute"), async () => {
2837+
test(suiteLabel("one_update_link_nested_absolute"), async () => {
28372838
const changes: ConfluenceSpaceChange[] = [UPDATE_LINKS_ONE_NESTED_ABS];
28382839
const rootURL = "fake-server/wiki/spaces/QUARTOCONF/pages";
28392840
const expectedUpdate: ContentUpdate = {
@@ -2960,7 +2961,7 @@ const runFindAttachments = () => {
29602961
const bodyValue: string =
29612962
'<ri:attachment ri:filename="elephant.png" ri:version-at-save="1" />';
29622963
const filePaths: string[] = ["fake-path/elephant.png"];
2963-
const expected: string[] = ["fake-path/elephant.png"];
2964+
const expected: string[] = ["elephant.png"];
29642965
check(expected, bodyValue, filePaths);
29652966
});
29662967

@@ -2973,7 +2974,55 @@ const runFindAttachments = () => {
29732974
const expected: string[] = [
29742975
"computations/r/computations-r_files/figure-publish/fig-airquality-1.png",
29752976
];
2976-
check(expected, bodyValue, filePaths);
2977+
check(expected, bodyValue, filePaths, "computations/r/index.xml");
2978+
});
2979+
2980+
test(suiteLabel("single_image_lookup_full_path_win"), async () => {
2981+
const bodyValue: string =
2982+
'<ri:attachment ri:filename="computations-r_files/figure-publish/fig-airquality-1.png" ri:version-at-save="1" />';
2983+
const filePaths: string[] = [
2984+
"computations\\r\\computations-r_files\\figure-publish\\fig-airquality-1.png",
2985+
];
2986+
const expected: string[] = [
2987+
"computations\\r\\computations-r_files\\figure-publish\\fig-airquality-1.png",
2988+
];
2989+
check(expected, bodyValue, filePaths, "computations\\r\\index.xml");
2990+
});
2991+
2992+
test(suiteLabel("single_with_same_name_nested_win"), async () => {
2993+
const filePaths: string[] = [
2994+
"folder\\images\\elephant.png",
2995+
"images\\elephant.png",
2996+
];
2997+
const bodyValue: string =
2998+
'<ri:attachment ri:filename="images/elephant.png" />';
2999+
const expected: string[] = ["images\\elephant.png"];
3000+
const path = "index.xml";
3001+
check(expected, bodyValue, filePaths, path);
3002+
});
3003+
3004+
test(suiteLabel("single_with_same_name_nested_win2"), async () => {
3005+
const filePaths: string[] = [
3006+
"folder\\images\\elephant.png",
3007+
"images\\elephant.png",
3008+
];
3009+
const bodyValue: string =
3010+
'<ri:attachment ri:filename="images/elephant.png" />';
3011+
const expected: string[] = ["folder\\images\\elephant.png"];
3012+
const path = "folder\\index.xml";
3013+
check(expected, bodyValue, filePaths, path);
3014+
});
3015+
3016+
test(suiteLabel("single_with_same_name_nested"), async () => {
3017+
const filePaths: string[] = [
3018+
"images/elephant.png",
3019+
"folder/images/elephant.png",
3020+
];
3021+
const bodyValue: string =
3022+
'<ri:attachment ri:filename="images/elephant.png" />';
3023+
const expected: string[] = ["images/elephant.png"];
3024+
const path = "index.xml";
3025+
check(expected, bodyValue, filePaths, path);
29773026
});
29783027

29793028
test(suiteLabel("single_image_lookup_relative_path"), async () => {
@@ -2988,6 +3037,18 @@ const runFindAttachments = () => {
29883037
check(expected, bodyValue, filePaths, path);
29893038
});
29903039

3040+
test(suiteLabel("single_image_lookup_relative_path_win"), async () => {
3041+
const bodyValue: string =
3042+
'<ri:attachment ri:filename="elephant.png" ri:version-at-save="1" />';
3043+
const filePaths: string[] = [
3044+
"images\\elephant.png",
3045+
"parent\\inner-parent\\elephant.png",
3046+
];
3047+
const path = "parent\\inner-parent\\hello-world3.xml";
3048+
const expected: string[] = ["parent\\inner-parent\\elephant.png"];
3049+
check(expected, bodyValue, filePaths, path);
3050+
});
3051+
29913052
test(suiteLabel("single_image_lookup_dupe_name"), async () => {
29923053
const bodyValue: string =
29933054
'<ri:attachment ri:filename="elephant.png" ri:version-at-save="1" />';
@@ -3070,13 +3131,26 @@ const runGetAttachmentsDirectory = () => {
30703131
check(expected, baseDirectory, "", "file-name.png");
30713132
});
30723133

3134+
test(suiteLabel("empty_to_upload_windows"), async () => {
3135+
const expected = "";
3136+
const baseDirectory = win32.fromFileUrl("file:///Users/fake-base");
3137+
check(expected, baseDirectory, "", "file-name.png");
3138+
});
3139+
30733140
test(suiteLabel("empty_fileName"), async () => {
30743141
const expected = "";
30753142
const attachmentPath = "file1.png";
30763143
const baseDirectory = "/Users/fake-base";
30773144
check(expected, baseDirectory, attachmentPath, "");
30783145
});
30793146

3147+
test(suiteLabel("empty_fileName_windows"), async () => {
3148+
const expected = "";
3149+
const attachmentPath = "file1.png";
3150+
const baseDirectory = win32.fromFileUrl("file:///Users/fake-base");
3151+
check(expected, baseDirectory, attachmentPath, "");
3152+
});
3153+
30803154
test(suiteLabel("simple"), async () => {
30813155
const expected = "/Users/fake-base";
30823156

@@ -3087,6 +3161,18 @@ const runGetAttachmentsDirectory = () => {
30873161
check(expected, baseDirectory, attachmentPath, fileName);
30883162
});
30893163

3164+
test(suiteLabel("simple_windows"), async () => {
3165+
const expected = String.raw`c:\Users\fake-base`;
3166+
const baseDirectory = String.raw`c:\Users\fake-base`;
3167+
3168+
console.log("baseDirectory", baseDirectory);
3169+
3170+
const attachmentPath = "file1.png";
3171+
const fileName = "fake-file.xml";
3172+
3173+
check(expected, baseDirectory, attachmentPath, fileName);
3174+
});
3175+
30903176
test(suiteLabel("site_in_root"), async () => {
30913177
const expected = "/Users/fake-base";
30923178

@@ -3132,7 +3218,7 @@ const runGetAttachmentsDirectory = () => {
31323218
check(expected, baseDirectory, attachmentPath, fileName);
31333219
});
31343220

3135-
otest(suiteLabel("site_nested_relative_dotslash"), async () => {
3221+
test(suiteLabel("site_nested_relative_dotslash"), async () => {
31363222
const expected = "/Users/fake-base/fake-parent";
31373223

31383224
const baseDirectory = "/Users/fake-base/_site";
@@ -3251,5 +3337,39 @@ if (RUN_ALL_TESTS) {
32513337
runUpdateImagePathsForContentBody();
32523338
runCapFirstLetter();
32533339
} else {
3254-
runCapFirstLetter();
3340+
runFindAttachments();
32553341
}
3342+
3343+
// WINDOWS
3344+
// publishFiles: {
3345+
// baseDir: "C:\\Users\\Megaport\\Documents\\dev\\quarto-confluence-test-main\\simple-site2\\_site",
3346+
// rootFile: "index.html",
3347+
// files: [
3348+
// "folder\\images\\elephant.png",
3349+
// "folder\\index.xml",
3350+
// "images\\elephant.png",
3351+
// "index.html",
3352+
// "index.xml",
3353+
// "search.json",
3354+
// "site_libs\\bootstrap\\bootstrap-icons.css",
3355+
// "site_libs\\bootstrap\\bootstrap-icons.woff",
3356+
// "site_libs\\bootstrap\\bootstrap.min.css",
3357+
// "site_libs\\bootstrap\\bootstrap.min.js",
3358+
// "site_libs\\clipboard\\clipboard.min.js",
3359+
// "site_libs\\quarto-html\\anchor.min.js",
3360+
// "site_libs\\quarto-html\\popper.min.js",
3361+
// "site_libs\\quarto-html\\quarto-syntax-highlighting.css",
3362+
// "site_libs\\quarto-html\\quarto.js",
3363+
// "site_libs\\quarto-html\\tippy.css",
3364+
// "site_libs\\quarto-html\\tippy.umd.min.js",
3365+
// "site_libs\\quarto-nav\\headroom.min.js",
3366+
// "site_libs\\quarto-nav\\quarto-nav.js",
3367+
// "site_libs\\quarto-search\\autocomplete.umd.js",
3368+
// "site_libs\\quarto-search\\fuse.min.js",
3369+
// "site_libs\\quarto-search\\quarto-search.js"
3370+
// ],
3371+
// metadataByInput: {
3372+
// "folder\\index.qmd": { title: "Page 2", author: undefined, date: undefined },
3373+
// "index.qmd": { title: "Page 1", author: undefined, date: undefined }
3374+
// }
3375+
// },

0 commit comments

Comments
 (0)