students: add face reference from the profile photo
POST /api/students/:id/face-refs/from-profile inserts a student_face_refs
row straight from students.photo_path, with no new upload. The file is copied
under a fresh name via storage.copyObject: deleting a ref unlinks its
photo_path, so reusing the original would destroy the profile photo. Path must
start with /uploads/, otherwise 400.
The faces modal gets a "Взять из фото профиля" button, hidden unless the
student has an /uploads/ profile photo (state from GET /api/students/:id/profile).
.face-ref-profile-btn[hidden]{display:none} is required — .btn-primary sets
inline-flex, which overrides the UA [hidden] rule.
This commit is contained in:
@@ -8463,6 +8463,48 @@ app.post('/api/students/:id/face-refs', requireAuth, (req, res, next) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/students/:id/face-refs/from-profile', requireAuth, async (req, res) => {
|
||||
if (!faceIdConfigured()) return res.status(503).json({ error: 'Face-ID сервис не настроен (FACE_ID_URL)' });
|
||||
let id;
|
||||
try {
|
||||
id = reqInt(req.params.id);
|
||||
} catch {
|
||||
return res.status(400).json({ error: 'Неверный id ученика' });
|
||||
}
|
||||
const acc = await studentProfileAccess(req.user, id);
|
||||
if (!acc.found) return res.status(acc.forbidden ? 403 : 404).json({ error: acc.forbidden ? 'Нет доступа к этому ученику' : 'Ученик не найден' });
|
||||
const profilePath = String(acc.student.photo_path || '');
|
||||
const srcKey = profilePath.startsWith('/uploads/') ? storage.keyFromPath(profilePath) : null;
|
||||
if (!srcKey) return res.status(400).json({ error: 'У ученика нет фото профиля' });
|
||||
const copyName = `${crypto.randomBytes(12).toString('hex')}${path.extname(srcKey) || '.jpg'}`;
|
||||
if (!(await storage.copyObject(srcKey, copyName))) {
|
||||
return res.status(400).json({ error: 'Файл фото профиля не найден' });
|
||||
}
|
||||
try {
|
||||
const faces = await faceIdDetect(copyName, null);
|
||||
if (!faces) {
|
||||
safeUnlink(`uploads/${copyName}`);
|
||||
return res.status(503).json({ error: 'Face-ID сервис недоступен' });
|
||||
}
|
||||
if (!faces.length) {
|
||||
safeUnlink(`uploads/${copyName}`);
|
||||
return res.status(400).json({ error: 'Лицо не найдено на фото профиля. Загрузите фото анфас' });
|
||||
}
|
||||
const best = faces.reduce((a, b) => ((b.det_score || 0) > (a.det_score || 0) ? b : a));
|
||||
const { rows } = await pool.query(
|
||||
`INSERT INTO student_face_refs (student_id, photo_path, embedding, det_score) VALUES ($1, $2, $3, $4) RETURNING id, student_id, photo_path, det_score, created_at`,
|
||||
[id, `/uploads/${copyName}`, JSON.stringify(best.embedding), Number(best.det_score) || 0]
|
||||
);
|
||||
await logAudit(req, 'student.face_ref.create', { student_id: id, ref_id: rows[0].id, source: 'profile' });
|
||||
res.status(201).json(rows[0]);
|
||||
rematchFacesBackground();
|
||||
} catch (e) {
|
||||
safeUnlink(`uploads/${copyName}`);
|
||||
console.error('POST /api/students/:id/face-refs/from-profile:', e);
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/students/:id/face-refs', requireAuth, async (req, res) => {
|
||||
let id;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user