A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
feat(cli): allow importing singlefile archives via the CLI
Mohamed Bassem5f6ba262c657
diff --git a/apps/cli/src/commands/bookmarks.ts b/apps/cli/src/commands/bookmarks.tsindex d2f9e254..84c3775b 100644--- a/apps/cli/src/commands/bookmarks.ts+++ b/apps/cli/src/commands/bookmarks.ts@@ -302,6 +302,78 @@ bookmarkCmd);});+const singleFileIfExistsModes = [+ "skip",+ "overwrite",+ "overwrite-recrawl",+ "append",+ "append-recrawl",+] as const;++type SingleFileIfExistsMode = (typeof singleFileIfExistsModes)[number];++function parseSingleFileIfExistsMode(value: string): SingleFileIfExistsMode {+ if (!singleFileIfExistsModes.includes(value as SingleFileIfExistsMode)) {+ throw new Error(+ `if-exists must be one of: ${singleFileIfExistsModes.join(", ")}`,+ );+ }+ return value as SingleFileIfExistsMode;+}++bookmarkCmd+ .command("import-singlefile")+ .description("imports a SingleFile HTML archive as a link bookmark")+ .argument("<file>", "the path to the SingleFile HTML archive")+ .requiredOption("--url <url>", "the original URL of the archived page")+ .option(+ "--if-exists <mode>",+ "how to handle an existing bookmark with the same URL",+ parseSingleFileIfExistsMode,+ "skip",+ )+ .action(async (filePath, opts) => {+ const globals = getGlobalOptions();++ try {+ const fileBuffer = fs.readFileSync(filePath);+ const fileName = path.basename(filePath);+ const formData = new FormData();+ formData.append("url", opts.url);+ formData.append(+ "file",+ new Blob([fileBuffer], { type: "text/html" }),+ fileName,+ );++ const endpoint = new URL(+ `${globals.serverAddr}/api/v1/bookmarks/singlefile`,+ );+ endpoint.searchParams.set("ifexists", opts.ifExists);++ const response = await fetch(endpoint, {+ method: "POST",+ headers: {+ authorization: `Bearer ${globals.apiKey}`,+ },+ body: formData,+ });+ if (!response.ok) {+ throw new Error(await getResponseError(response));+ }++ printObject((await response.json()) as object);+ } catch (error) {+ printStatusMessage(+ false,+ `Failed to import SingleFile archive "${filePath}". Reason: ${+ error instanceof Error ? error.message : error+ }`,+ );+ process.exitCode = 1;+ }+ });+bookmarkCmd.command("get").description("fetch information about a bookmark")diff --git a/docs/docs/05-integrations/05-singlefile.md b/docs/docs/05-integrations/05-singlefile.mdindex 44c6aa51..46015e6c 100644--- a/docs/docs/05-integrations/05-singlefile.md+++ b/docs/docs/05-integrations/05-singlefile.md@@ -19,6 +19,18 @@ Karakeep supports being a destination for the [SingleFile extension](https://gitNow, go to any page and click the singlefile extension icon. Once it's done with the upload, the bookmark should show up in your karakeep instance. Note that the singlefile extension doesn't show any progress on the upload. Given that archives are typically large, it might take 30+ seconds until the upload is done and starts showing up in Karakeep.+## Importing an existing archive with the CLI++If you already have a SingleFile HTML archive, import it by passing both the+archive path and the original page URL:++```bash+karakeep bookmarks import-singlefile page.html --url "https://example.com/page"+```++Use `--if-exists MODE` to control how an existing bookmark for the same URL is+handled. The supported modes are described below.+## Handling Existing BookmarksWhen uploading a page that already exists in your archive (same URL), you can control the behavior by setting the `ifexists` query parameter in the upload URL. The available modes are:@@ -47,4 +59,3 @@ Also, you most likely will want to change the default `MAX_ASSET_SIZE_MB` in kar:::infoCurrently, we don't support screenshots for singlefile uploads, but this will change in the future.:::-