mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-05-05 13:22:38 +02:00
fixed billing
This commit is contained in:
parent
03e4d45bc3
commit
bf6d04c4ea
1 changed files with 22 additions and 12 deletions
|
|
@ -3,10 +3,9 @@ import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { tempBinaryCache } from '@/src/application/services/temp-binary-cache';
|
import { tempBinaryCache } from '@/src/application/services/temp-binary-cache';
|
||||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||||
import { UsageTracker } from '@/app/lib/billing';
|
import { UsageTracker, getCustomerForUserId, logUsage as libLogUsage } from '@/app/lib/billing';
|
||||||
import { logUsage } from '@/app/actions/billing.actions';
|
|
||||||
import { authCheck } from '@/app/actions/auth.actions';
|
import { authCheck } from '@/app/actions/auth.actions';
|
||||||
import { USE_AUTH } from '@/app/lib/feature_flags';
|
import { USE_AUTH, USE_BILLING } from '@/app/lib/feature_flags';
|
||||||
|
|
||||||
// POST /api/uploaded-images
|
// POST /api/uploaded-images
|
||||||
// Accepts an image file (multipart/form-data, field name: "file")
|
// Accepts an image file (multipart/form-data, field name: "file")
|
||||||
|
|
@ -15,9 +14,10 @@ import { USE_AUTH } from '@/app/lib/feature_flags';
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
// Require authentication if enabled
|
// Require authentication if enabled
|
||||||
|
let currentUser: any | null = null;
|
||||||
try {
|
try {
|
||||||
if (USE_AUTH) {
|
if (USE_AUTH) {
|
||||||
await authCheck();
|
currentUser = await authCheck();
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
|
|
@ -101,11 +101,16 @@ export async function POST(request: NextRequest) {
|
||||||
|
|
||||||
const url = `/api/uploaded-images/${imageId}`;
|
const url = `/api/uploaded-images/${imageId}`;
|
||||||
|
|
||||||
// Log usage to billing if available
|
// Log usage to billing similar to rag-worker
|
||||||
try {
|
try {
|
||||||
|
if (USE_BILLING && currentUser) {
|
||||||
|
const customer = await getCustomerForUserId(currentUser.id);
|
||||||
|
if (customer) {
|
||||||
const items = usageTracker.flush();
|
const items = usageTracker.flush();
|
||||||
if (items.length > 0) {
|
if (items.length > 0) {
|
||||||
await logUsage({ items });
|
await libLogUsage(customer.id, { items });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
// ignore billing logging errors
|
// ignore billing logging errors
|
||||||
|
|
@ -118,11 +123,16 @@ export async function POST(request: NextRequest) {
|
||||||
const ttlSec = 10 * 60; // 10 minutes
|
const ttlSec = 10 * 60; // 10 minutes
|
||||||
const id = tempBinaryCache.put(buf, mime, ttlSec * 1000);
|
const id = tempBinaryCache.put(buf, mime, ttlSec * 1000);
|
||||||
const url = `/api/tmp-images/${id}`;
|
const url = `/api/tmp-images/${id}`;
|
||||||
// Log usage to billing if available
|
// Log usage to billing similar to rag-worker
|
||||||
try {
|
try {
|
||||||
|
if (USE_BILLING && currentUser) {
|
||||||
|
const customer = await getCustomerForUserId(currentUser.id);
|
||||||
|
if (customer) {
|
||||||
const items = usageTracker.flush();
|
const items = usageTracker.flush();
|
||||||
if (items.length > 0) {
|
if (items.length > 0) {
|
||||||
await logUsage({ items });
|
await libLogUsage(customer.id, { items });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
// ignore billing logging errors
|
// ignore billing logging errors
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue