proxy controller,direkter Zugriff auf neue Bilder #10

This commit is contained in:
2025-01-17 23:27:29 +00:00
parent ab17db9078
commit 21bd5e4abf
16 changed files with 297 additions and 52 deletions

View File

@@ -1,5 +0,0 @@
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: 'sqlite', // 'mysql' | 'sqlite' | 'turso'
schema: './src/db/schema'
})

View File

@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { DecksController } from './decks.controller';
import { DrizzleService } from './drizzle.service';
import { ProxyController } from './proxy.controller';
@Module({
imports: [],
controllers: [DecksController],
controllers: [DecksController,ProxyController],
providers: [DrizzleService],
})
export class AppModule {}

View File

@@ -1,4 +1,5 @@
// decks.controller.ts
import express from 'express';
import {
Controller,
Get,
@@ -10,8 +11,10 @@ import {
Put,
HttpException,
HttpStatus,
Res,
} from '@nestjs/common';
import { DrizzleService } from './drizzle.service';
import { firstValueFrom } from 'rxjs';
@Controller('decks')
export class DecksController {
@@ -68,7 +71,7 @@ export class DecksController {
const deck = {
name: deckname,
images: [],
images: [] as any,
};
for (const entry of entries) {
@@ -162,4 +165,6 @@ export class DecksController {
) {
return this.drizzleService.updateBox(boxId, data);
}
}

View File

@@ -2,7 +2,7 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { drizzle } from 'drizzle-orm/libsql';
import { Deck, InsertDeck } from '../db/schema';
import { eq, and } from 'drizzle-orm';
import { eq, and, isNull } from 'drizzle-orm';
@Injectable()
export class DrizzleService {
@@ -120,7 +120,7 @@ export class DrizzleService {
.select()
.from(Deck)
.where(
and(eq(Deck.deckname, deck.deckname), eq(Deck.bildid, null)),
and(eq(Deck.deckname, deck.deckname), isNull(Deck.bildid)),
)
.all();

View File

@@ -0,0 +1,121 @@
// decks.controller.ts
import express from 'express';
import {
Controller,
Get,
Post,
Delete,
Body,
Param,
Query,
Put,
HttpException,
HttpStatus,
Res,
} from '@nestjs/common';
import { DrizzleService } from './drizzle.service';
import { firstValueFrom } from 'rxjs';
@Controller('')
export class ProxyController {
constructor() {}
// --------------------
// Proxy Endpoints
// --------------------
// @Get('debug_image/:name/:filename')
// async getDebugImage(
// @Param('name') name: string,
// @Param('filename') filename: string,
// @Res() res: express.Response,
// ) {
// const url = `http://localhost:8080/api/debug_image/${name}/${filename}`;
// //const url = `http://localhost:5000/api/debug_image/20250112_112306_9286e3bf/thumbnail.jpg`;
// try {
// // Fetch the image from the external service
// const response = await fetch(url);
// // Check if the response is OK (status code 200-299)
// if (!response.ok) {
// throw new Error(`Failed to retrieve image: ${response.statusText}`);
// }
// // Get the image data as a buffer
// const imageBuffer = await response.arrayBuffer();
// // Determine the Content-Type based on the file extension
// let contentType = 'image/png'; // Default MIME type
// if (filename.toLowerCase().endsWith('.jpg') || filename.toLowerCase().endsWith('.jpeg')) {
// contentType = 'image/jpeg';
// } else if (filename.toLowerCase().endsWith('.gif')) {
// contentType = 'image/gif';
// } else if (filename.toLowerCase().endsWith('.bmp')) {
// contentType = 'image/bmp';
// } else if (filename.toLowerCase().endsWith('.tiff') || filename.toLowerCase().endsWith('.tif')) {
// contentType = 'image/tiff';
// }
// // Set the Content-Type header and send the image data
// res.set('Content-Type', contentType);
// res.send(Buffer.from(imageBuffer));
// } catch (error) {
// // Handle errors
// res.status(500).json({ error: error.message });
// }
// }
@Post('ocr')
async ocrEndpoint(
@Body() data: { image: string },
@Res() res: express.Response,
) {
try {
if (!data || !data.image) {
throw new HttpException('No image provided', HttpStatus.BAD_REQUEST);
}
const response = await fetch('http://localhost:5000/api/ocr', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ image: data.image }),
});
const result = await response.json();
if (!response.ok) {
if (response.status === 400) {
throw new HttpException(result.error, HttpStatus.BAD_REQUEST);
}
throw new HttpException(
result.error || 'OCR processing failed',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
// Bei erfolgreicher Verarbeitung mit Warnung
if (result.warning) {
return res.status(HttpStatus.OK).json({
warning: result.warning,
debug_dir: result.debug_dir,
});
}
// Bei vollständig erfolgreicher Verarbeitung
return res.status(HttpStatus.OK).json({
status: result.status,
results: result.results,
});
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@@ -3,7 +3,7 @@ import * as t from "drizzle-orm/sqlite-core";
export const Deck = table('Deck', {
id: integer('id').primaryKey({ autoIncrement: true }),
deckname: text('deckname'),
deckname: text('deckname').notNull(),
bildname: text('bildname'),
bildid: text('bildid'),
iconindex: integer('iconindex'),

View File

@@ -6,9 +6,12 @@
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';
import { json, urlencoded } from 'express';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(json({ limit: '50mb' }));
app.use(urlencoded({ limit: '50mb', extended: true }));
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env['PORT'] || 3000;