mirror of
https://github.com/kyantech/Palmr.git
synced 2025-10-23 06:11:58 +00:00
- Added checks to determine if the application is running in a Docker environment. - Updated file storage paths to use `/app/server` in Docker and the current working directory for local development. - Ensured consistent directory creation for uploads and temporary chunks across different environments.
105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
import { LogoService } from "./logo.service";
|
|
import { AppService } from "./service";
|
|
import { FastifyReply, FastifyRequest } from "fastify";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
const isDocker = (() => {
|
|
try {
|
|
require("fs").statSync("/.dockerenv");
|
|
return true;
|
|
} catch {
|
|
try {
|
|
return require("fs").readFileSync("/proc/self/cgroup", "utf8").includes("docker");
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
})();
|
|
|
|
const baseDir = isDocker ? "/app/server" : process.cwd();
|
|
const uploadsDir = path.join(baseDir, "uploads/logo");
|
|
if (!fs.existsSync(uploadsDir)) {
|
|
fs.mkdirSync(uploadsDir, { recursive: true });
|
|
}
|
|
|
|
export class AppController {
|
|
private appService = new AppService();
|
|
private logoService = new LogoService();
|
|
|
|
async getAppInfo(request: FastifyRequest, reply: FastifyReply) {
|
|
try {
|
|
const appInfo = await this.appService.getAppInfo();
|
|
return reply.send(appInfo);
|
|
} catch (error: any) {
|
|
return reply.status(400).send({ error: error.message });
|
|
}
|
|
}
|
|
|
|
async getAllConfigs(request: FastifyRequest, reply: FastifyReply) {
|
|
try {
|
|
const configs = await this.appService.getAllConfigs();
|
|
return reply.send({ configs });
|
|
} catch (error: any) {
|
|
return reply.status(400).send({ error: error.message });
|
|
}
|
|
}
|
|
|
|
async updateConfig(request: FastifyRequest, reply: FastifyReply) {
|
|
try {
|
|
const { key } = request.params as { key: string };
|
|
const { value } = request.body as { value: string };
|
|
|
|
const config = await this.appService.updateConfig(key, value);
|
|
return reply.send({ config });
|
|
} catch (error: any) {
|
|
if (error.message === "Configuration not found") {
|
|
return reply.status(404).send({ error: error.message });
|
|
}
|
|
return reply.status(400).send({ error: error.message });
|
|
}
|
|
}
|
|
|
|
async bulkUpdateConfigs(request: FastifyRequest, reply: FastifyReply) {
|
|
try {
|
|
const updates = request.body as Array<{ key: string; value: string }>;
|
|
const configs = await this.appService.bulkUpdateConfigs(updates);
|
|
return reply.send({ configs });
|
|
} catch (error: any) {
|
|
return reply.status(400).send({ error: error.message });
|
|
}
|
|
}
|
|
|
|
async uploadLogo(request: FastifyRequest, reply: FastifyReply) {
|
|
try {
|
|
const file = await request.file();
|
|
if (!file) {
|
|
return reply.status(400).send({ error: "No file uploaded" });
|
|
}
|
|
|
|
if (!file.mimetype.startsWith("image/")) {
|
|
return reply.status(400).send({ error: "Only images are allowed" });
|
|
}
|
|
|
|
const buffer = await file.toBuffer();
|
|
const base64Logo = await this.logoService.uploadLogo(buffer);
|
|
await this.appService.updateConfig("appLogo", base64Logo);
|
|
|
|
return reply.send({ logo: base64Logo });
|
|
} catch (error: any) {
|
|
console.error("Upload error:", error);
|
|
return reply.status(400).send({ error: error.message });
|
|
}
|
|
}
|
|
|
|
async removeLogo(request: FastifyRequest, reply: FastifyReply) {
|
|
try {
|
|
await this.logoService.deleteLogo();
|
|
return reply.send({ message: "Logo removed successfully" });
|
|
} catch (error: any) {
|
|
console.error("Logo removal error:", error);
|
|
return reply.status(400).send({ error: error.message });
|
|
}
|
|
}
|
|
}
|