Skip to content

Commit

Permalink
feat: set favicon on CAD logo upload
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Jun 11, 2023
1 parent afb4515 commit 0e8e9b3
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { ExtendedBadRequest } from "src/exceptions/extended-bad-request";
import { getImageWebPPath } from "lib/images/get-image-webp-path";
import { AuditLogActionType, createAuditLogEntry } from "@snailycad/audit-logger/server";
import type { User } from "@snailycad/types";
import { resolve } from "node:path";
import { existsSync } from "node:fs";
import sharp from "sharp";

@Controller("/admin/manage/cad-settings/image")
@ContentType("application/json")
Expand Down Expand Up @@ -67,6 +70,8 @@ export class ManageCitizensController {
executorId: user.id,
});

this.handleUploadFavicon(file);

return data;
}

Expand Down Expand Up @@ -103,4 +108,19 @@ export class ManageCitizensController {
}),
);
}

private async handleUploadFavicon(image: PlatformMulterFile) {
const clientPublicFolder = resolve(process.cwd(), "../client", "public");
const doesFaviconExist = existsSync(`${clientPublicFolder}/favicon.png`);

if (doesFaviconExist) {
// don't override the existing favicon
return;
}

const sharpImage = sharp(image.buffer).png({ quality: 80 });
const buffer = await sharpImage.toBuffer();

await fs.writeFile(`${clientPublicFolder}/favicon.png`, buffer);
}
}

0 comments on commit 0e8e9b3

Please sign in to comment.