Download src/admin.ts from launch-calcium/Gallery-Copper-Typescript: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/spaces/launch-calcium/Gallery-Copper-Typescript/resolve/main/src/admin.ts
- Command line
-
hf download hf://spaces/launch-calcium/Gallery-Copper-Typescript/src/admin.ts
-
curl -L -o admin.ts https://huggingface.co/spaces/launch-calcium/Gallery-Copper-Typescript/resolve/main/src/admin.ts
2.36 kB
| import express from 'express'; | |
| import { db } from './db'; | |
| export const adminRouter = express.Router(); | |
| adminRouter.use((req, res, next) => { | |
| const user = (req.session as any).user; | |
| if (!user || !user.isAdmin) { | |
| return res.status(403).render('message', { title: 'Access Denied', message: 'You must be an administrator to view this page.', backUrl: '/' }); | |
| } | |
| next(); | |
| }); | |
| adminRouter.get('/', (req, res) => { | |
| const categories = db.prepare('SELECT * FROM categories ORDER BY pos ASC').all(); | |
| const albums = db.prepare('SELECT * FROM albums ORDER BY aid DESC').all(); | |
| const users = db.prepare('SELECT u.*, g.group_name FROM users u LEFT JOIN usergroups g ON u.user_group = g.group_id').all(); | |
| const configs = db.prepare('SELECT * FROM config').all(); | |
| res.render('admin', { | |
| title: 'Admin Control Panel', | |
| categories, | |
| albums, | |
| users, | |
| configs, | |
| user: (req.session as any).user | |
| }); | |
| }); | |
| adminRouter.post('/category/add', (req, res) => { | |
| const { name, description, parent } = req.body; | |
| if (name && name.trim()) { | |
| db.prepare('INSERT INTO categories (name, description, parent) VALUES (?, ?, ?)').run(name.trim(), description || '', Number(parent) || 0); | |
| } | |
| res.redirect('/admin'); | |
| }); | |
| adminRouter.post('/category/delete/:cid', (req, res) => { | |
| const cid = Number(req.params.cid); | |
| db.prepare('DELETE FROM categories WHERE cid = ?').run(cid); | |
| res.redirect('/admin'); | |
| }); | |
| adminRouter.post('/album/add', (req, res) => { | |
| const { title, description, category, uploads } = req.body; | |
| if (title && title.trim()) { | |
| db.prepare('INSERT INTO albums (title, description, category, uploads) VALUES (?, ?, ?, ?)').run( | |
| title.trim(), | |
| description || '', | |
| Number(category) || 0, | |
| uploads === 'YES' ? 'YES' : 'NO' | |
| ); | |
| } | |
| res.redirect('/admin'); | |
| }); | |
| adminRouter.post('/album/delete/:aid', (req, res) => { | |
| const aid = Number(req.params.aid); | |
| db.prepare('DELETE FROM pictures WHERE aid = ?').run(aid); | |
| db.prepare('DELETE FROM albums WHERE aid = ?').run(aid); | |
| res.redirect('/admin'); | |
| }); | |
| adminRouter.post('/config/update', (req, res) => { | |
| const stmt = db.prepare('INSERT OR REPLACE INTO config (name, value) VALUES (?, ?)'); | |
| for (const [key, val] of Object.entries(req.body)) { | |
| if (typeof val === 'string') { | |
| stmt.run(key, val); | |
| } | |
| } | |
| res.redirect('/admin'); | |
| }); | |