mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: add GET /api/v1/tools/popular endpoint
This commit is contained in:
@@ -94,6 +94,7 @@ import { registerOcrPdf } from "./ocr-pdf.js";
|
||||
import { registerOptimizeForWeb } from "./optimize-for-web.js";
|
||||
import { registerOrganizePdf } from "./organize-pdf.js";
|
||||
import { registerPassportPhoto } from "./passport-photo.js";
|
||||
import { registerPopularTools } from "./popular.js";
|
||||
import { registerPdfMetadata } from "./pdf-metadata.js";
|
||||
import { registerPdfPageNumbers } from "./pdf-page-numbers.js";
|
||||
import { registerPdfToImage } from "./pdf-to-image.js";
|
||||
@@ -168,6 +169,9 @@ import { registerYamlJson } from "./yaml-json.js";
|
||||
* (when `enableExperimentalTools` is off) are skipped at startup.
|
||||
*/
|
||||
export async function registerToolRoutes(app: FastifyInstance): Promise<void> {
|
||||
// Register non-tool utility endpoints (not subject to disable/experimental skip logic)
|
||||
await registerPopularTools(app);
|
||||
|
||||
// Read disabled tools from settings
|
||||
const [disabledRow] = await db
|
||||
.select()
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { db } from "../../db/index.js";
|
||||
import { jobs } from "../../db/schema.js";
|
||||
import { sql } from "drizzle-orm";
|
||||
|
||||
const DEFAULT_POPULAR = [
|
||||
"resize", "crop", "compress", "convert", "remove-background",
|
||||
"upscale", "merge-pdf", "watermark-text", "compress-video",
|
||||
"trim-video", "convert-audio", "compress-pdf",
|
||||
];
|
||||
|
||||
export async function registerPopularTools(app: FastifyInstance) {
|
||||
app.get("/api/v1/tools/popular", async () => {
|
||||
try {
|
||||
const rows = await db
|
||||
.select({
|
||||
toolId: jobs.toolId,
|
||||
count: sql<number>`count(*)`.as("count"),
|
||||
})
|
||||
.from(jobs)
|
||||
.groupBy(jobs.toolId)
|
||||
.orderBy(sql`count(*) desc`)
|
||||
.limit(12);
|
||||
|
||||
if (rows.length < 4) {
|
||||
return { tools: DEFAULT_POPULAR };
|
||||
}
|
||||
return { tools: rows.map((r) => r.toolId) };
|
||||
} catch {
|
||||
return { tools: DEFAULT_POPULAR };
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user