@doufunao123/asset-gateway 0.3.0 → 0.4.0
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.
- package/dist/index.js +108 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/index.ts
|
|
4
|
-
import { Command as
|
|
4
|
+
import { Command as Command8 } from "commander";
|
|
5
5
|
|
|
6
6
|
// src/commands/auth.ts
|
|
7
7
|
import { existsSync as existsSync2, unlinkSync } from "fs";
|
|
@@ -70,7 +70,7 @@ function normalizeError(error2) {
|
|
|
70
70
|
|
|
71
71
|
// src/meta.ts
|
|
72
72
|
var CLI_NAME = "asset-gateway";
|
|
73
|
-
var CLI_VERSION = "0.
|
|
73
|
+
var CLI_VERSION = "0.4.0";
|
|
74
74
|
var CLI_DESCRIPTION = "Universal asset generation gateway CLI";
|
|
75
75
|
var DEFAULT_GATEWAY_URL = "https://assets.xiaomao.chat";
|
|
76
76
|
|
|
@@ -694,12 +694,107 @@ function createJobCommand() {
|
|
|
694
694
|
return command;
|
|
695
695
|
}
|
|
696
696
|
|
|
697
|
-
// src/commands/
|
|
697
|
+
// src/commands/process.ts
|
|
698
|
+
import { mkdirSync as mkdirSync3, readFileSync as readFileSync2, writeFileSync as writeFileSync3 } from "fs";
|
|
699
|
+
import { existsSync as existsSync3 } from "fs";
|
|
700
|
+
import { join as join3 } from "path";
|
|
698
701
|
import { Command as Command5 } from "commander";
|
|
702
|
+
function readInputAsBase64(input) {
|
|
703
|
+
if (existsSync3(input)) {
|
|
704
|
+
const bytes = readFileSync2(input);
|
|
705
|
+
return `data:image/png;base64,${bytes.toString("base64")}`;
|
|
706
|
+
}
|
|
707
|
+
return input;
|
|
708
|
+
}
|
|
709
|
+
function saveProcessOutput(data, outputDir) {
|
|
710
|
+
const outputData = data.output_data;
|
|
711
|
+
if (typeof outputData !== "string" || !outputData) return null;
|
|
712
|
+
mkdirSync3(outputDir, { recursive: true });
|
|
713
|
+
const timestamp = Date.now();
|
|
714
|
+
const filePath = join3(outputDir, `processed_${timestamp}.png`);
|
|
715
|
+
writeFileSync3(filePath, Buffer.from(outputData, "base64"));
|
|
716
|
+
delete data.output_data;
|
|
717
|
+
return filePath;
|
|
718
|
+
}
|
|
719
|
+
function createProcessCommand() {
|
|
720
|
+
const command = new Command5("process").description("Post-process images (remove-bg, crop, resize, upscale)");
|
|
721
|
+
command.addCommand(
|
|
722
|
+
new Command5("remove-bg").description("Remove background from an image (AI-powered, BiRefNet)").requiredOption("--input <path>", "Input image (file path or URL)").option("--smart-crop", "Also crop to power-of-2 after removing background").option("--output-dir <dir>", "Directory to save output", ".").action(async function(options) {
|
|
723
|
+
try {
|
|
724
|
+
const ctx = createContext(this);
|
|
725
|
+
const ops = [{ op: "remove_bg" }];
|
|
726
|
+
if (options.smartCrop) {
|
|
727
|
+
ops.push({ op: "smart_crop", mode: "power_of2" });
|
|
728
|
+
}
|
|
729
|
+
const data = await ctx.client.post("/api/process", {
|
|
730
|
+
input: readInputAsBase64(options.input),
|
|
731
|
+
operations: ops
|
|
732
|
+
});
|
|
733
|
+
const localPath = saveProcessOutput(data, options.outputDir);
|
|
734
|
+
if (localPath) data.local_path = localPath;
|
|
735
|
+
printSuccess("process.remove-bg", data, ctx);
|
|
736
|
+
} catch (error2) {
|
|
737
|
+
printError("process.remove-bg", error2);
|
|
738
|
+
}
|
|
739
|
+
})
|
|
740
|
+
);
|
|
741
|
+
command.addCommand(
|
|
742
|
+
new Command5("crop").description("Smart crop an image (trim transparent borders)").requiredOption("--input <path>", "Input image (file path or URL)").option("--mode <mode>", "Crop mode: tightest or power_of2", "tightest").option("--output-dir <dir>", "Directory to save output", ".").action(async function(options) {
|
|
743
|
+
try {
|
|
744
|
+
const ctx = createContext(this);
|
|
745
|
+
const data = await ctx.client.post("/api/process", {
|
|
746
|
+
input: readInputAsBase64(options.input),
|
|
747
|
+
operations: [{ op: "smart_crop", mode: options.mode }]
|
|
748
|
+
});
|
|
749
|
+
const localPath = saveProcessOutput(data, options.outputDir);
|
|
750
|
+
if (localPath) data.local_path = localPath;
|
|
751
|
+
printSuccess("process.crop", data, ctx);
|
|
752
|
+
} catch (error2) {
|
|
753
|
+
printError("process.crop", error2);
|
|
754
|
+
}
|
|
755
|
+
})
|
|
756
|
+
);
|
|
757
|
+
command.addCommand(
|
|
758
|
+
new Command5("resize").description("Resize an image to exact dimensions").requiredOption("--input <path>", "Input image (file path or URL)").requiredOption("--width <n>", "Target width").requiredOption("--height <n>", "Target height").option("--output-dir <dir>", "Directory to save output", ".").action(async function(options) {
|
|
759
|
+
try {
|
|
760
|
+
const ctx = createContext(this);
|
|
761
|
+
const data = await ctx.client.post("/api/process", {
|
|
762
|
+
input: readInputAsBase64(options.input),
|
|
763
|
+
operations: [{ op: "resize", width: Number(options.width), height: Number(options.height) }]
|
|
764
|
+
});
|
|
765
|
+
const localPath = saveProcessOutput(data, options.outputDir);
|
|
766
|
+
if (localPath) data.local_path = localPath;
|
|
767
|
+
printSuccess("process.resize", data, ctx);
|
|
768
|
+
} catch (error2) {
|
|
769
|
+
printError("process.resize", error2);
|
|
770
|
+
}
|
|
771
|
+
})
|
|
772
|
+
);
|
|
773
|
+
command.addCommand(
|
|
774
|
+
new Command5("upscale").description("AI upscale an image (2x or 4x via Real-ESRGAN)").requiredOption("--input <path>", "Input image (file path or URL)").option("--scale <n>", "Scale factor (2 or 4)", "4").option("--output-dir <dir>", "Directory to save output", ".").action(async function(options) {
|
|
775
|
+
try {
|
|
776
|
+
const ctx = createContext(this);
|
|
777
|
+
const data = await ctx.client.post("/api/process", {
|
|
778
|
+
input: readInputAsBase64(options.input),
|
|
779
|
+
operations: [{ op: "upscale", scale: Number(options.scale) }]
|
|
780
|
+
});
|
|
781
|
+
const localPath = saveProcessOutput(data, options.outputDir);
|
|
782
|
+
if (localPath) data.local_path = localPath;
|
|
783
|
+
printSuccess("process.upscale", data, ctx);
|
|
784
|
+
} catch (error2) {
|
|
785
|
+
printError("process.upscale", error2);
|
|
786
|
+
}
|
|
787
|
+
})
|
|
788
|
+
);
|
|
789
|
+
return command;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
// src/commands/provider.ts
|
|
793
|
+
import { Command as Command6 } from "commander";
|
|
699
794
|
function createProviderCommand() {
|
|
700
|
-
const command = new
|
|
795
|
+
const command = new Command6("provider").description("Provider management");
|
|
701
796
|
command.addCommand(
|
|
702
|
-
new
|
|
797
|
+
new Command6("list").description("List available providers").action(async function() {
|
|
703
798
|
try {
|
|
704
799
|
const ctx = createContext(this);
|
|
705
800
|
const data = await ctx.client.get("/api/providers");
|
|
@@ -710,7 +805,7 @@ function createProviderCommand() {
|
|
|
710
805
|
})
|
|
711
806
|
);
|
|
712
807
|
command.addCommand(
|
|
713
|
-
new
|
|
808
|
+
new Command6("health").description("Check provider health").argument("[name]", "Specific provider name").action(async function(name) {
|
|
714
809
|
try {
|
|
715
810
|
const ctx = createContext(this);
|
|
716
811
|
const path = name ? `/api/providers/${encodeURIComponent(name)}/health` : "/api/providers/health";
|
|
@@ -725,11 +820,11 @@ function createProviderCommand() {
|
|
|
725
820
|
}
|
|
726
821
|
|
|
727
822
|
// src/commands/upload.ts
|
|
728
|
-
import { Command as
|
|
823
|
+
import { Command as Command7 } from "commander";
|
|
729
824
|
function createUploadCommand() {
|
|
730
|
-
const command = new
|
|
825
|
+
const command = new Command7("upload").description("Upload and manage assets");
|
|
731
826
|
command.addCommand(
|
|
732
|
-
new
|
|
827
|
+
new Command7("file").description("Upload a file and get a public URL").argument("<path>", "Path to file to upload").action(async function(filePath) {
|
|
733
828
|
const ctx = createContext(this);
|
|
734
829
|
try {
|
|
735
830
|
const data = await ctx.client.uploadFile(filePath);
|
|
@@ -740,7 +835,7 @@ function createUploadCommand() {
|
|
|
740
835
|
})
|
|
741
836
|
);
|
|
742
837
|
command.addCommand(
|
|
743
|
-
new
|
|
838
|
+
new Command7("list").description("List uploaded assets").action(async function() {
|
|
744
839
|
const ctx = createContext(this);
|
|
745
840
|
try {
|
|
746
841
|
const data = await ctx.client.get("/api/assets");
|
|
@@ -751,7 +846,7 @@ function createUploadCommand() {
|
|
|
751
846
|
})
|
|
752
847
|
);
|
|
753
848
|
command.addCommand(
|
|
754
|
-
new
|
|
849
|
+
new Command7("delete").description("Delete an uploaded asset (admin only)").argument("<filename>", "Filename to delete").action(async function(filename) {
|
|
755
850
|
const ctx = createContext(this);
|
|
756
851
|
try {
|
|
757
852
|
const data = await ctx.client.delete(`/api/assets/${encodeURIComponent(filename)}`);
|
|
@@ -765,12 +860,13 @@ function createUploadCommand() {
|
|
|
765
860
|
}
|
|
766
861
|
|
|
767
862
|
// src/index.ts
|
|
768
|
-
var program = new
|
|
863
|
+
var program = new Command8().name("asset-gateway").description("Universal asset generation gateway CLI").version(CLI_VERSION).option(
|
|
769
864
|
"--gateway-url <url>",
|
|
770
865
|
`Gateway URL (default: $ASSET_GATEWAY_URL, auth config, or ${DEFAULT_GATEWAY_URL})`
|
|
771
866
|
).option("--token <token>", "API token for authentication").option("--human", "Human-readable output instead of JSON").option("--fields <fields>", "Comma-separated list of output fields");
|
|
772
867
|
program.addCommand(createAuthCommand());
|
|
773
868
|
program.addCommand(createGenerateCommand());
|
|
869
|
+
program.addCommand(createProcessCommand());
|
|
774
870
|
program.addCommand(createProviderCommand());
|
|
775
871
|
program.addCommand(createUploadCommand());
|
|
776
872
|
program.addCommand(createJobCommand());
|