@inblog/cli 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1107,7 +1107,119 @@ function registerAuthCommands(program2) {
1107
1107
  }
1108
1108
 
1109
1109
  // src/commands/posts.ts
1110
- var fs3 = __toESM(require("fs"));
1110
+ var fs4 = __toESM(require("fs"));
1111
+
1112
+ // src/utils/upload.ts
1113
+ var import_node_fs = __toESM(require("fs"));
1114
+ var import_node_path = __toESM(require("path"));
1115
+ var import_node_crypto2 = require("crypto");
1116
+ var WORKER_URL = "https://upload.inblog.dev/";
1117
+ var MAX_FILE_SIZE = 10 * 1024 * 1024;
1118
+ var MIME_TYPES = {
1119
+ ".png": "image/png",
1120
+ ".jpg": "image/jpeg",
1121
+ ".jpeg": "image/jpeg",
1122
+ ".gif": "image/gif",
1123
+ ".webp": "image/webp",
1124
+ ".svg": "image/svg+xml",
1125
+ ".ico": "image/x-icon"
1126
+ };
1127
+ function isLocalPath(value) {
1128
+ if (value.startsWith("http://") || value.startsWith("https://")) return false;
1129
+ return import_node_fs.default.existsSync(value);
1130
+ }
1131
+ async function uploadImage(filePath, bucket) {
1132
+ const resolved = import_node_path.default.resolve(filePath);
1133
+ if (!import_node_fs.default.existsSync(resolved)) {
1134
+ throw new Error(`File not found: ${resolved}`);
1135
+ }
1136
+ const stat = import_node_fs.default.statSync(resolved);
1137
+ if (stat.size > MAX_FILE_SIZE) {
1138
+ throw new Error(`File size exceeds 10MB limit: ${(stat.size / 1024 / 1024).toFixed(1)}MB`);
1139
+ }
1140
+ const ext = import_node_path.default.extname(resolved).toLowerCase();
1141
+ const contentType = MIME_TYPES[ext] || "application/octet-stream";
1142
+ const fileKey = `${bucket}/${(/* @__PURE__ */ new Date()).toISOString()}-${(0, import_node_crypto2.randomUUID)()}`;
1143
+ const body = import_node_fs.default.readFileSync(resolved);
1144
+ const response = await fetch(`${WORKER_URL}?fileKey=${fileKey}`, {
1145
+ method: "POST",
1146
+ body,
1147
+ headers: { "Content-Type": contentType }
1148
+ });
1149
+ if (!response.ok) {
1150
+ throw new Error(`Upload failed: ${response.status} ${response.statusText}`);
1151
+ }
1152
+ const data = await response.json();
1153
+ return data.publicUrl;
1154
+ }
1155
+ async function resolveImageUrl(value, bucket) {
1156
+ if (!isLocalPath(value)) return value;
1157
+ return uploadImage(value, bucket);
1158
+ }
1159
+ async function uploadBase64(dataUri, bucket) {
1160
+ const match = dataUri.match(/^data:([^;]+);base64,(.+)$/s);
1161
+ if (!match) throw new Error("Invalid data URI");
1162
+ const contentType = match[1];
1163
+ const body = Buffer.from(match[2], "base64");
1164
+ if (body.length > MAX_FILE_SIZE) {
1165
+ throw new Error(`Base64 image exceeds 10MB limit: ${(body.length / 1024 / 1024).toFixed(1)}MB`);
1166
+ }
1167
+ const fileKey = `${bucket}/${(/* @__PURE__ */ new Date()).toISOString()}-${(0, import_node_crypto2.randomUUID)()}`;
1168
+ const response = await fetch(`${WORKER_URL}?fileKey=${fileKey}`, {
1169
+ method: "POST",
1170
+ body,
1171
+ headers: { "Content-Type": contentType }
1172
+ });
1173
+ if (!response.ok) {
1174
+ throw new Error(`Upload failed: ${response.status} ${response.statusText}`);
1175
+ }
1176
+ const data = await response.json();
1177
+ return data.publicUrl;
1178
+ }
1179
+ async function processContentImages(html) {
1180
+ const imgSrcRegex = /(<img\s[^>]*\bsrc=["'])([^"']+)(["'][^>]*>)/gi;
1181
+ const matches = [];
1182
+ let m;
1183
+ while ((m = imgSrcRegex.exec(html)) !== null) {
1184
+ matches.push({ full: m[0], prefix: m[1], src: m[2], suffix: m[3], index: m.index });
1185
+ }
1186
+ if (matches.length === 0) return { html, uploadCount: 0 };
1187
+ let uploadCount = 0;
1188
+ const replacements = /* @__PURE__ */ new Map();
1189
+ const concurrency = 5;
1190
+ for (let i = 0; i < matches.length; i += concurrency) {
1191
+ const batch = matches.slice(i, i + concurrency);
1192
+ const results = await Promise.allSettled(
1193
+ batch.map(async ({ src }) => {
1194
+ if (src.startsWith("https://source.inblog.dev/") || src.startsWith("https://image.inblog.dev/")) {
1195
+ return null;
1196
+ }
1197
+ if (src.startsWith("data:image/")) {
1198
+ const url = await uploadBase64(src, "post_image");
1199
+ return { src, url };
1200
+ }
1201
+ if (isLocalPath(src)) {
1202
+ const url = await uploadImage(src, "post_image");
1203
+ return { src, url };
1204
+ }
1205
+ return null;
1206
+ })
1207
+ );
1208
+ for (const result of results) {
1209
+ if (result.status === "fulfilled" && result.value) {
1210
+ replacements.set(result.value.src, result.value.url);
1211
+ uploadCount++;
1212
+ }
1213
+ }
1214
+ }
1215
+ let processed = html;
1216
+ for (const [src, url] of replacements) {
1217
+ processed = processed.split(src).join(url);
1218
+ }
1219
+ return { html: processed, uploadCount };
1220
+ }
1221
+
1222
+ // src/commands/posts.ts
1111
1223
  function formatPost(post) {
1112
1224
  return [
1113
1225
  ["ID", post.id],
@@ -1183,19 +1295,27 @@ Showing page ${meta.page ?? 1} (${data.length} of ${meta.total} posts)`);
1183
1295
  handleError(error, json);
1184
1296
  }
1185
1297
  });
1186
- posts.command("create").description("Create post (draft by default, use --published to publish)").requiredOption("-t, --title <title>", "Post title").option("-s, --slug <slug>", "Post slug").option("-d, --description <desc>", "Post description").option("--content <html>", "HTML content").option("--content-file <path>", "Read HTML content from file").option("--notion-url <url>", "Notion page URL").option("--published", "Publish immediately").option("--tag-ids <ids>", "Comma-separated tag IDs").option("--author-ids <ids>", "Comma-separated author IDs").option("--canonical-url <url>", "Canonical URL").option("--meta-title <title>", "Meta title").option("--meta-description <desc>", "Meta description").action(async function() {
1298
+ posts.command("create").description("Create post (draft by default, use --published to publish)").requiredOption("-t, --title <title>", "Post title").option("-s, --slug <slug>", "Post slug").option("-d, --description <desc>", "Post description").option("--content <html>", "HTML content").option("--content-file <path>", "Read HTML content from file").option("--image <path-or-url>", "Cover image (local file or URL)").option("--notion-url <url>", "Notion page URL").option("--published", "Publish immediately").option("--tag-ids <ids>", "Comma-separated tag IDs").option("--author-ids <ids>", "Comma-separated author IDs").option("--canonical-url <url>", "Canonical URL").option("--meta-title <title>", "Meta title").option("--meta-description <desc>", "Meta description").action(async function() {
1187
1299
  const json = isJsonMode(this);
1188
1300
  try {
1189
1301
  const opts = this.opts();
1190
1302
  const { posts: endpoint } = createClientFromCommand(this);
1191
1303
  let contentHtml = opts.content;
1192
1304
  if (opts.contentFile) {
1193
- contentHtml = fs3.readFileSync(opts.contentFile, "utf-8");
1305
+ contentHtml = fs4.readFileSync(opts.contentFile, "utf-8");
1306
+ }
1307
+ if (contentHtml) {
1308
+ const { html, uploadCount } = await processContentImages(contentHtml);
1309
+ contentHtml = html;
1310
+ if (uploadCount > 0 && !json) {
1311
+ printWarning(`Uploaded ${uploadCount} image(s) to CDN.`);
1312
+ }
1194
1313
  }
1195
1314
  const input = { title: opts.title };
1196
1315
  if (opts.slug) input.slug = opts.slug;
1197
1316
  if (opts.description) input.description = opts.description;
1198
1317
  if (contentHtml) input.content_html = contentHtml;
1318
+ if (opts.image) input.image = { url: await resolveImageUrl(opts.image, "featured_image") };
1199
1319
  if (opts.notionUrl) input.notion_url = opts.notionUrl;
1200
1320
  if (opts.published) input.published = true;
1201
1321
  if (opts.canonicalUrl) input.canonical_url = opts.canonicalUrl;
@@ -1214,20 +1334,28 @@ Showing page ${meta.page ?? 1} (${data.length} of ${meta.total} posts)`);
1214
1334
  handleError(error, json);
1215
1335
  }
1216
1336
  });
1217
- posts.command("update <id>").description("Update post fields (title, slug, content, SEO metadata)").option("-t, --title <title>", "Post title").option("-s, --slug <slug>", "Post slug").option("-d, --description <desc>", "Post description").option("--content <html>", "HTML content").option("--content-file <path>", "Read HTML content from file").option("--canonical-url <url>", "Canonical URL").option("--meta-title <title>", "Meta title").option("--meta-description <desc>", "Meta description").action(async function(id) {
1337
+ posts.command("update <id>").description("Update post fields (title, slug, content, SEO metadata)").option("-t, --title <title>", "Post title").option("-s, --slug <slug>", "Post slug").option("-d, --description <desc>", "Post description").option("--content <html>", "HTML content").option("--content-file <path>", "Read HTML content from file").option("--image <path-or-url>", "Cover image (local file or URL)").option("--canonical-url <url>", "Canonical URL").option("--meta-title <title>", "Meta title").option("--meta-description <desc>", "Meta description").action(async function(id) {
1218
1338
  const json = isJsonMode(this);
1219
1339
  try {
1220
1340
  const opts = this.opts();
1221
1341
  const { posts: endpoint } = createClientFromCommand(this);
1222
1342
  let contentHtml = opts.content;
1223
1343
  if (opts.contentFile) {
1224
- contentHtml = fs3.readFileSync(opts.contentFile, "utf-8");
1344
+ contentHtml = fs4.readFileSync(opts.contentFile, "utf-8");
1345
+ }
1346
+ if (contentHtml) {
1347
+ const { html, uploadCount } = await processContentImages(contentHtml);
1348
+ contentHtml = html;
1349
+ if (uploadCount > 0 && !json) {
1350
+ printWarning(`Uploaded ${uploadCount} image(s) to CDN.`);
1351
+ }
1225
1352
  }
1226
1353
  const input = {};
1227
1354
  if (opts.title) input.title = opts.title;
1228
1355
  if (opts.slug) input.slug = opts.slug;
1229
1356
  if (opts.description) input.description = opts.description;
1230
1357
  if (contentHtml) input.content_html = contentHtml;
1358
+ if (opts.image) input.image = { url: await resolveImageUrl(opts.image, "featured_image") };
1231
1359
  if (opts.canonicalUrl !== void 0) input.canonical_url = opts.canonicalUrl || null;
1232
1360
  if (opts.metaTitle !== void 0) input.meta_title = opts.metaTitle || null;
1233
1361
  if (opts.metaDescription !== void 0) input.meta_description = opts.metaDescription || null;
@@ -1672,54 +1800,6 @@ function getDnsProviderGuide(provider) {
1672
1800
  return DNS_PROVIDER_GUIDES[provider] || null;
1673
1801
  }
1674
1802
 
1675
- // src/utils/upload.ts
1676
- var import_node_fs = __toESM(require("fs"));
1677
- var import_node_path = __toESM(require("path"));
1678
- var import_node_crypto2 = require("crypto");
1679
- var WORKER_URL = "https://upload.inblog.dev/";
1680
- var MAX_FILE_SIZE = 10 * 1024 * 1024;
1681
- var MIME_TYPES = {
1682
- ".png": "image/png",
1683
- ".jpg": "image/jpeg",
1684
- ".jpeg": "image/jpeg",
1685
- ".gif": "image/gif",
1686
- ".webp": "image/webp",
1687
- ".svg": "image/svg+xml",
1688
- ".ico": "image/x-icon"
1689
- };
1690
- function isLocalPath(value) {
1691
- if (value.startsWith("http://") || value.startsWith("https://")) return false;
1692
- return import_node_fs.default.existsSync(value);
1693
- }
1694
- async function uploadImage(filePath, bucket) {
1695
- const resolved = import_node_path.default.resolve(filePath);
1696
- if (!import_node_fs.default.existsSync(resolved)) {
1697
- throw new Error(`File not found: ${resolved}`);
1698
- }
1699
- const stat = import_node_fs.default.statSync(resolved);
1700
- if (stat.size > MAX_FILE_SIZE) {
1701
- throw new Error(`File size exceeds 10MB limit: ${(stat.size / 1024 / 1024).toFixed(1)}MB`);
1702
- }
1703
- const ext = import_node_path.default.extname(resolved).toLowerCase();
1704
- const contentType = MIME_TYPES[ext] || "application/octet-stream";
1705
- const fileKey = `${bucket}/${(/* @__PURE__ */ new Date()).toISOString()}-${(0, import_node_crypto2.randomUUID)()}`;
1706
- const body = import_node_fs.default.readFileSync(resolved);
1707
- const response = await fetch(`${WORKER_URL}?fileKey=${fileKey}`, {
1708
- method: "POST",
1709
- body,
1710
- headers: { "Content-Type": contentType }
1711
- });
1712
- if (!response.ok) {
1713
- throw new Error(`Upload failed: ${response.status} ${response.statusText}`);
1714
- }
1715
- const data = await response.json();
1716
- return data.publicUrl;
1717
- }
1718
- async function resolveImageUrl(value, bucket) {
1719
- if (!isLocalPath(value)) return value;
1720
- return uploadImage(value, bucket);
1721
- }
1722
-
1723
1803
  // src/commands/blogs.ts
1724
1804
  function registerBlogsCommands(program2) {
1725
1805
  const blogs = program2.command("blogs").description("Manage blogs \u2014 list, switch, view, and update blog settings");
@@ -2593,6 +2673,35 @@ function registerAnalyticsCommands(program2) {
2593
2673
  });
2594
2674
  }
2595
2675
 
2676
+ // src/commands/images.ts
2677
+ var VALID_BUCKETS = ["post_image", "featured_image", "logo", "favicon", "og_image", "banner", "avatar"];
2678
+ function registerImagesCommands(program2) {
2679
+ const images = program2.command("images").description("Upload images to inblog CDN");
2680
+ images.command("upload <file...>").description("Upload local image file(s) to inblog CDN").option("-b, --bucket <type>", "Image bucket (post_image, featured_image, logo, favicon, og_image, banner)", "post_image").action(async function(files) {
2681
+ const json = isJsonMode(this);
2682
+ try {
2683
+ const opts = this.opts();
2684
+ const bucket = opts.bucket;
2685
+ if (!VALID_BUCKETS.includes(bucket)) {
2686
+ throw new Error(`Invalid bucket: ${bucket}. Valid: ${VALID_BUCKETS.join(", ")}`);
2687
+ }
2688
+ const results = [];
2689
+ for (const file of files) {
2690
+ const url = await uploadImage(file, bucket);
2691
+ results.push({ file, url });
2692
+ if (!json) {
2693
+ printSuccess(`${file} \u2192 ${url}`);
2694
+ }
2695
+ }
2696
+ if (json) {
2697
+ printJson(results);
2698
+ }
2699
+ } catch (error) {
2700
+ handleError(error, json);
2701
+ }
2702
+ });
2703
+ }
2704
+
2596
2705
  // bin/inblog.ts
2597
2706
  var program = new import_commander.Command();
2598
2707
  program.name("inblog").description("CLI for managing inblog.ai blog content (posts, tags, authors, redirects, forms)").version("0.2.0").option("--json", "Output as JSON (for programmatic use)").option("--base-url <url>", "API base URL").option("--no-color", "Disable colored output");
@@ -2607,5 +2716,6 @@ registerFormResponsesCommands(program);
2607
2716
  registerConfigCommands(program);
2608
2717
  registerSearchConsoleCommands(program);
2609
2718
  registerAnalyticsCommands(program);
2719
+ registerImagesCommands(program);
2610
2720
  program.parse();
2611
2721
  //# sourceMappingURL=inblog.js.map