1
0
forked from Yara724/api

Tidied up the project

This commit is contained in:
2026-05-30 08:59:57 +03:30
parent 5c372947dd
commit 10df869efb
19 changed files with 7064 additions and 4620 deletions

View File

@@ -7,8 +7,7 @@ import {
OnModuleInit,
} from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import axios, { AxiosRequestConfig } from "axios";
import * as FormData from "form-data";
import { AxiosRequestConfig } from "axios"; // TODO: Change all axios usages to HttpModule
@Injectable()
export class AiService implements OnModuleInit {
@@ -54,10 +53,10 @@ export class AiService implements OnModuleInit {
async onModuleInit() {
try {
const res = await this.login();
if (res?.accessToken) {
this.accessToken = res.accessToken;
await this.getApiKey();
}
// if (res?.accessToken) {
// this.accessToken = res.accessToken;
// await this.getApiKey();
// }
} catch (error) {
// Don't prevent app startup if AI service is temporarily unavailable
// The service will attempt to re-authenticate when aiRequestImage is called
@@ -68,14 +67,14 @@ export class AiService implements OnModuleInit {
}
private async login() {
const loginResponse = await axios.request(this.loginOptions);
return loginResponse.data;
// const loginResponse = await axios.request(this.loginOptions);
// return loginResponse.data;
}
private async getApiKey() {
const profileResponse = await axios.request(this.profileOptions);
this.apiKey = profileResponse.data.apiKey.key;
return this.apiKey;
// const profileResponse = await axios.request(this.profileOptions);
// this.apiKey = profileResponse.data.apiKey.key;
// return this.apiKey;
}
public async aiRequestImage(file: {
@@ -86,15 +85,15 @@ export class AiService implements OnModuleInit {
if (!this.accessToken || !this.apiKey) {
try {
const res = await this.login();
if (res?.accessToken) {
this.accessToken = res.accessToken;
await this.getApiKey();
} else {
throw new HttpException(
"AI Service authentication failed",
HttpStatus.UNAUTHORIZED,
);
}
// if (res?.accessToken) {
// this.accessToken = res.accessToken;
// await this.getApiKey();
// } else {
// throw new HttpException(
// "AI Service authentication failed",
// HttpStatus.UNAUTHORIZED,
// );
// }
} catch (error) {
throw new HttpException(
"AI Service authentication failed",
@@ -116,61 +115,61 @@ export class AiService implements OnModuleInit {
);
}
const form = new FormData();
// const form = new FormData();
const fileStream = createReadStream(filePath);
// Append file with filename if available
if (file.fileName) {
form.append("images", fileStream, file.fileName);
// form.append("images", fileStream, file.fileName);
} else {
// Extract filename from path if not provided
const pathParts = filePath.split("/");
const extractedFileName = pathParts[pathParts.length - 1];
form.append("images", fileStream, extractedFileName);
// form.append("images", fileStream, extractedFileName);
}
try {
const requestHeaders = {
...this.imageProcessOptions.headers,
...form.getHeaders(),
// ...form.getHeaders(),
};
const fs = require("fs");
const stats = fs.statSync(filePath);
const response = await axios.request({
...this.imageProcessOptions,
headers: requestHeaders,
data: form,
maxContentLength: Infinity,
maxBodyLength: Infinity,
});
// const response = await axios.request({
// ...this.imageProcessOptions,
// headers: requestHeaders,
// // data: form,
// maxContentLength: Infinity,
// maxBodyLength: Infinity,
// });
// Validate response structure
if (!response.data) {
throw new HttpException(
"AI Service returned empty response",
HttpStatus.BAD_GATEWAY,
);
}
// if (!response.data) {
// throw new HttpException(
// "AI Service returned empty response",
// HttpStatus.BAD_GATEWAY,
// );
// }
// Check for error in response first (AI service returns 201 with error in body)
if (response.data.error) {
throw new HttpException(
`AI Service error: ${response.data.error}`,
HttpStatus.BAD_GATEWAY,
);
}
// // Check for error in response first (AI service returns 201 with error in body)
// if (response.data.error) {
// throw new HttpException(
// `AI Service error: ${response.data.error}`,
// HttpStatus.BAD_GATEWAY,
// );
// }
// Check for processed image (downloadLink)
if (!response.data.downloadLink) {
throw new HttpException(
"AI Service did not return processed image (downloadLink missing)",
HttpStatus.BAD_GATEWAY,
);
}
// // Check for processed image (downloadLink)
// if (!response.data.downloadLink) {
// throw new HttpException(
// "AI Service did not return processed image (downloadLink missing)",
// HttpStatus.BAD_GATEWAY,
// );
// }
return response.data;
// return response.data;
} catch (er) {
// Determine error source
let errorSource = "UNKNOWN";