1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
import { limitConcurrency } from "../concurrency";
import { MAX_LIST_NAME_LENGTH } from "../types/lists";
import { ImportSource, ParsedBookmark, parseImportFile } from "./parsers";
export interface ImportCounts {
successes: number;
failures: number;
alreadyExisted: number;
total: number;
}
export interface ImportDeps {
createList: (input: {
name: string;
icon: string;
parentId?: string;
}) => Promise<{ id: string }>;
createBookmark: (
bookmark: ParsedBookmark,
sessionId: string,
) => Promise<{ id: string; alreadyExists?: boolean }>;
addBookmarkToLists: (input: {
bookmarkId: string;
listIds: string[];
}) => Promise<void>;
updateBookmarkTags: (input: {
bookmarkId: string;
tags: string[];
}) => Promise<void>;
createImportSession: (input: {
name: string;
rootListId: string;
}) => Promise<{ id: string }>;
}
export interface ImportOptions {
concurrencyLimit?: number;
parsers?: Partial<
Record<ImportSource, (textContent: string) => ParsedBookmark[]>
>;
}
export interface ImportResult {
counts: ImportCounts;
rootListId: string | null;
importSessionId: string | null;
}
export async function importBookmarksFromFile(
{
file,
source,
rootListName,
deps,
onProgress,
}: {
file: { text: () => Promise<string> };
source: ImportSource;
rootListName: string;
deps: ImportDeps;
onProgress?: (done: number, total: number) => void;
},
options: ImportOptions = {},
): Promise<ImportResult> {
const { concurrencyLimit = 20, parsers } = options;
const textContent = await file.text();
const parsedBookmarks = parsers?.[source]
? parsers[source]!(textContent)
: parseImportFile(source, textContent);
if (parsedBookmarks.length === 0) {
return {
counts: { successes: 0, failures: 0, alreadyExisted: 0, total: 0 },
rootListId: null,
importSessionId: null,
};
}
const rootList = await deps.createList({ name: rootListName, icon: "⬆️" });
const session = await deps.createImportSession({
name: `${source.charAt(0).toUpperCase() + source.slice(1)} Import - ${new Date().toLocaleDateString()}`,
rootListId: rootList.id,
});
onProgress?.(0, parsedBookmarks.length);
const PATH_DELIMITER = "$$__$$";
// Build required paths
const allRequiredPaths = new Set<string>();
for (const bookmark of parsedBookmarks) {
for (const path of bookmark.paths) {
if (path && path.length > 0) {
for (let i = 1; i <= path.length; i++) {
const subPath = path.slice(0, i);
const pathKey = subPath.join(PATH_DELIMITER);
allRequiredPaths.add(pathKey);
}
}
}
}
const allRequiredPathsArray = Array.from(allRequiredPaths).sort(
(a, b) => a.split(PATH_DELIMITER).length - b.split(PATH_DELIMITER).length,
);
const pathMap: Record<string, string> = { "": rootList.id };
for (const pathKey of allRequiredPathsArray) {
const parts = pathKey.split(PATH_DELIMITER);
const parentKey = parts.slice(0, -1).join(PATH_DELIMITER);
const parentId = pathMap[parentKey] || rootList.id;
const folderName = parts[parts.length - 1];
const folderList = await deps.createList({
name: folderName.substring(0, MAX_LIST_NAME_LENGTH),
parentId,
icon: "📁",
});
pathMap[pathKey] = folderList.id;
}
let done = 0;
const importPromises = parsedBookmarks.map((bookmark) => async () => {
try {
const listIds = bookmark.paths.map(
(path) => pathMap[path.join(PATH_DELIMITER)] || rootList.id,
);
if (listIds.length === 0) listIds.push(rootList.id);
const created = await deps.createBookmark(bookmark, session.id);
await deps.addBookmarkToLists({ bookmarkId: created.id, listIds });
if (bookmark.tags && bookmark.tags.length > 0) {
await deps.updateBookmarkTags({
bookmarkId: created.id,
tags: bookmark.tags,
});
}
return created;
} finally {
done += 1;
onProgress?.(done, parsedBookmarks.length);
}
});
const resultsPromises = limitConcurrency(importPromises, concurrencyLimit);
const results = await Promise.allSettled(resultsPromises);
let successes = 0;
let failures = 0;
let alreadyExisted = 0;
for (const r of results) {
if (r.status === "fulfilled") {
if (r.value.alreadyExists) alreadyExisted++;
else successes++;
} else {
failures++;
}
}
return {
counts: {
successes,
failures,
alreadyExisted,
total: parsedBookmarks.length,
},
rootListId: rootList.id,
importSessionId: session.id,
};
}
|