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:
dev
2026-10-10 13:31:39 +03:00
parent 60e701d1d3
commit 8847028b7e
5 changed files with 93 additions and 1 deletions
+48 -1
View File
@@ -9,6 +9,7 @@ let attachGroupLoaded = false;
let faceRefs = [];
let facePhotos = [];
let faceSimilar = [];
let faceProfilePhoto = '';
async function loadGroupOptions(sel, selected) {
if (!groupOptionsLoaded) {
@@ -266,12 +267,14 @@ async function openStudentFaces(id, name) {
faceRefs = [];
facePhotos = [];
faceSimilar = [];
faceProfilePhoto = '';
renderFaceProfileBtn();
document.getElementById('studentFacesName').textContent = name || '';
renderFaceRefs();
renderFacePhotos();
renderFaceSimilar();
document.getElementById('studentFacesModal').classList.add('open');
await Promise.all([loadFaceRefs(id), loadFacePhotos(id)]);
await Promise.all([loadFaceRefs(id), loadFacePhotos(id), loadFaceProfilePhoto(id)]);
}
function closeStudentFaces() {
@@ -280,6 +283,27 @@ function closeStudentFaces() {
faceRefs = [];
facePhotos = [];
faceSimilar = [];
faceProfilePhoto = '';
renderFaceProfileBtn();
}
function renderFaceProfileBtn() {
const btn = document.getElementById('faceRefProfileBtn');
if (btn) btn.hidden = !faceProfilePhoto;
}
async function loadFaceProfilePhoto(id) {
faceProfilePhoto = '';
try {
const res = await fetch(`${API}/api/students/${id}/profile`, { headers: hdr() });
if (!res.ok) return;
const data = await res.json();
if (typeof data.photo_path === 'string' && data.photo_path.startsWith('/uploads/')) faceProfilePhoto = data.photo_path;
} catch {
faceProfilePhoto = '';
} finally {
if (profileDataId === id) renderFaceProfileBtn();
}
}
async function loadFaceRefs(id) {
@@ -346,6 +370,27 @@ async function addFaceRef(file) {
}
}
async function addFaceRefFromProfile() {
if (!profileDataId) return;
showToast('Ищем лицо на фото профиля...');
try {
const res = await fetch(`${API}/api/students/${profileDataId}/face-refs/from-profile`, { method: 'POST', headers: hdr() });
const data = await res.json();
if (!res.ok) {
showToast(data.error || 'Не удалось добавить эталон');
return;
}
faceRefs.push(data);
faceProfilePhoto = '';
renderFaceProfileBtn();
renderFaceRefs();
showToast('Эталон добавлен из фото профиля, пересчитываем лица на всех фото...');
refreshFacesAfterRefChange();
} catch {
showToast('Не удалось добавить эталон');
}
}
async function delFaceRef(refId) {
try {
const res = await fetch(`${API}/api/students/${profileDataId}/face-refs/${refId}`, { method: 'DELETE', headers: hdr() });
@@ -357,6 +402,7 @@ async function delFaceRef(refId) {
renderFaceRefs();
showToast('Эталон удалён, пересчитываем лица...');
refreshFacesAfterRefChange();
if (profileDataId) loadFaceProfilePhoto(profileDataId);
} catch {
showToast('Не удалось удалить эталон');
}
@@ -819,6 +865,7 @@ document.getElementById('pfFacesBtn').addEventListener('click', () => {
document.getElementById('studentFacesCloseBtn').addEventListener('click', closeStudentFaces);
document.getElementById('studentFacesModal').addEventListener('click', e => { if (e.target === e.currentTarget) closeStudentFaces(); });
document.getElementById('faceRefAddBtn').addEventListener('click', () => document.getElementById('faceRefFile').click());
document.getElementById('faceRefProfileBtn').addEventListener('click', addFaceRefFromProfile);
document.getElementById('faceRefFile').addEventListener('change', e => {
const f = e.target.files[0];
e.target.value = '';