with open('app/Controllers/Admin.php', 'r', encoding='utf-8') as f:
    content = f.read()

func = """    /**
     * Serve a template photo for preview
     */
    public function serveTemplatePhoto(string $filename)
    {
        $filePath = WRITEPATH . 'uploads/template_photos/' . $filename;
        if (!file_exists($filePath)) {
            throw \\CodeIgniter\\Exceptions\\PageNotFoundException::forPageNotFound();
        }

        $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
        $mimeTypes = [
            'jpg'  => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'png'  => 'image/png',
            'webp' => 'image/webp',
            'gif'  => 'image/gif',
        ];
        $mime = $mimeTypes[$ext] ?? 'application/octet-stream';

        return $this->response
            ->setHeader('Content-Type', $mime)
            ->setHeader('Cache-Control', 'max-age=3600')
            ->setBody(file_get_contents($filePath));
    }
"""

content = content.replace("public function addTemplate()", func + "\n    public function addTemplate()")

with open('app/Controllers/Admin.php', 'w', encoding='utf-8') as f:
    f.write(content)
