feat(export): файлы группы в архиве по студенту и модулю, фото — в 'Фото записи'
This commit is contained in:
@@ -10832,9 +10832,10 @@ app.get('/api/groups/:id/export/files', requireAuth, async (req, res) => {
|
|||||||
pool.query('SELECT id, name FROM students WHERE group_id = $1 AND deleted_at IS NULL ORDER BY name', [gid]),
|
pool.query('SELECT id, name FROM students WHERE group_id = $1 AND deleted_at IS NULL ORDER BY name', [gid]),
|
||||||
includeFiles
|
includeFiles
|
||||||
? pool.query(
|
? pool.query(
|
||||||
`SELECT pf.name, pf.path, e.student_name, e.created_at
|
`SELECT pf.name, pf.path, e.student_name, e.module_id, m.name AS module_name, e.created_at
|
||||||
FROM project_files pf
|
FROM project_files pf
|
||||||
JOIN entries e ON e.id = pf.entry_id
|
JOIN entries e ON e.id = pf.entry_id
|
||||||
|
LEFT JOIN modules m ON m.id = e.module_id
|
||||||
WHERE ${condStr} AND pf.detached_at IS NULL
|
WHERE ${condStr} AND pf.detached_at IS NULL
|
||||||
ORDER BY e.student_name, e.created_at DESC, pf.id DESC`,
|
ORDER BY e.student_name, e.created_at DESC, pf.id DESC`,
|
||||||
params
|
params
|
||||||
@@ -10842,8 +10843,9 @@ app.get('/api/groups/:id/export/files', requireAuth, async (req, res) => {
|
|||||||
: null,
|
: null,
|
||||||
includePhotos && includeMain
|
includePhotos && includeMain
|
||||||
? pool.query(
|
? pool.query(
|
||||||
`SELECT e.photo_path, e.student_name, e.created_at
|
`SELECT e.photo_path, e.student_name, e.module_id, m.name AS module_name, e.created_at
|
||||||
FROM entries e
|
FROM entries e
|
||||||
|
LEFT JOIN modules m ON m.id = e.module_id
|
||||||
WHERE ${condStr} AND e.photo_path IS NOT NULL
|
WHERE ${condStr} AND e.photo_path IS NOT NULL
|
||||||
ORDER BY e.student_name, e.created_at DESC`,
|
ORDER BY e.student_name, e.created_at DESC`,
|
||||||
params
|
params
|
||||||
@@ -10851,9 +10853,10 @@ app.get('/api/groups/:id/export/files', requireAuth, async (req, res) => {
|
|||||||
: null,
|
: null,
|
||||||
includePhotos && photosMode === 'all'
|
includePhotos && photosMode === 'all'
|
||||||
? pool.query(
|
? pool.query(
|
||||||
`SELECT ep.photo_path, ep.caption, e.student_name, e.created_at
|
`SELECT ep.photo_path, ep.caption, e.student_name, e.module_id, m.name AS module_name, e.created_at
|
||||||
FROM entry_photos ep
|
FROM entry_photos ep
|
||||||
JOIN entries e ON e.id = ep.entry_id
|
JOIN entries e ON e.id = ep.entry_id
|
||||||
|
LEFT JOIN modules m ON m.id = e.module_id
|
||||||
WHERE ${condStr}
|
WHERE ${condStr}
|
||||||
ORDER BY e.student_name, e.created_at DESC, ep.sort_order ASC, ep.id ASC`,
|
ORDER BY e.student_name, e.created_at DESC, ep.sort_order ASC, ep.id ASC`,
|
||||||
params
|
params
|
||||||
@@ -10861,8 +10864,9 @@ app.get('/api/groups/:id/export/files', requireAuth, async (req, res) => {
|
|||||||
: null,
|
: null,
|
||||||
includeOriginals
|
includeOriginals
|
||||||
? pool.query(
|
? pool.query(
|
||||||
`SELECT e.photo_original_path, e.student_name, e.created_at
|
`SELECT e.photo_original_path, e.student_name, e.module_id, m.name AS module_name, e.created_at
|
||||||
FROM entries e
|
FROM entries e
|
||||||
|
LEFT JOIN modules m ON m.id = e.module_id
|
||||||
WHERE ${condStr} AND e.photo_original_path IS NOT NULL
|
WHERE ${condStr} AND e.photo_original_path IS NOT NULL
|
||||||
ORDER BY e.student_name, e.created_at DESC`,
|
ORDER BY e.student_name, e.created_at DESC`,
|
||||||
params
|
params
|
||||||
@@ -10877,40 +10881,50 @@ app.get('/api/groups/:id/export/files', requireAuth, async (req, res) => {
|
|||||||
const zip = createZipWriter();
|
const zip = createZipWriter();
|
||||||
const folderOf = name => String(name || '').replace(/[\\/:*?"<>|]/g, '_').replace(/^[\s.]+/, '').slice(0, 80) || 'Без имени';
|
const folderOf = name => String(name || '').replace(/[\\/:*?"<>|]/g, '_').replace(/^[\s.]+/, '').slice(0, 80) || 'Без имени';
|
||||||
const seenFolders = new Set();
|
const seenFolders = new Set();
|
||||||
const uniqueFolder = base => {
|
const ensureDir = dirPath => {
|
||||||
let folder = base;
|
if (seenFolders.has(dirPath)) return;
|
||||||
let n = 1;
|
seenFolders.add(dirPath);
|
||||||
while (seenFolders.has(folder)) {
|
zip.addDir(dirPath);
|
||||||
n++;
|
|
||||||
folder = `${base} (${n})`;
|
|
||||||
}
|
|
||||||
seenFolders.add(folder);
|
|
||||||
return folder;
|
|
||||||
};
|
};
|
||||||
const folderByStudent = new Map();
|
const ensureStudentModuleDir = (studentName, moduleName) => {
|
||||||
const ensureFolder = studentName => {
|
const student = folderOf(studentName || 'Без имени');
|
||||||
const key = String(studentName || '');
|
const module = folderOf(moduleName || 'Без модуля');
|
||||||
if (folderByStudent.has(key)) return folderByStudent.get(key);
|
ensureDir(student);
|
||||||
const folder = uniqueFolder(folderOf(key));
|
ensureDir(`${student}/${module}`);
|
||||||
folderByStudent.set(key, folder);
|
return `${student}/${module}`;
|
||||||
zip.addDir(folder);
|
};
|
||||||
return folder;
|
const ensurePhotoStudentDir = studentName => {
|
||||||
|
const student = folderOf(studentName || 'Без имени');
|
||||||
|
ensureDir('Фото записи');
|
||||||
|
ensureDir(`Фото записи/${student}`);
|
||||||
|
return `Фото записи/${student}`;
|
||||||
};
|
};
|
||||||
const filesBuilt = [];
|
const filesBuilt = [];
|
||||||
const seenFiles = new Map();
|
const seenFiles = new Map();
|
||||||
const seenPaths = new Set();
|
const seenPaths = new Set();
|
||||||
async function addEntryFile(row, subDir) {
|
async function addEntryFile(row, kind) {
|
||||||
const stored = photoRefKey(row.path);
|
const stored = photoRefKey(row.path);
|
||||||
if (!stored) return false;
|
if (!stored) return false;
|
||||||
if (seenPaths.has(stored)) return false;
|
if (seenPaths.has(stored)) return false;
|
||||||
const data = await storage.getBuffer(stored);
|
const data = await storage.getBuffer(stored);
|
||||||
if (!data) return false;
|
if (!data) return false;
|
||||||
seenPaths.add(stored);
|
seenPaths.add(stored);
|
||||||
const folder = ensureFolder(row.student_name);
|
let baseFolder, subDir;
|
||||||
|
if (kind === 'works') {
|
||||||
|
baseFolder = ensureStudentModuleDir(row.student_name, row.module_name);
|
||||||
|
subDir = null;
|
||||||
|
} else if (kind === 'originals') {
|
||||||
|
baseFolder = ensurePhotoStudentDir(row.student_name);
|
||||||
|
subDir = 'Оригиналы';
|
||||||
|
} else {
|
||||||
|
baseFolder = ensurePhotoStudentDir(row.student_name);
|
||||||
|
subDir = null;
|
||||||
|
}
|
||||||
|
if (subDir) ensureDir(`${baseFolder}/${subDir}/`);
|
||||||
const base = String(row.name || 'file').replace(/[\\/:*?"<>|]/g, '_').replace(/^[\s.]+/, '').slice(0, 120) || 'file';
|
const base = String(row.name || 'file').replace(/[\\/:*?"<>|]/g, '_').replace(/^[\s.]+/, '').slice(0, 120) || 'file';
|
||||||
const ext = path.extname(base);
|
const ext = path.extname(base);
|
||||||
const stem = ext ? base.slice(0, -ext.length) : base;
|
const stem = ext ? base.slice(0, -ext.length) : base;
|
||||||
const keyPath = folder + '/' + (subDir ? subDir + '/' : '');
|
const keyPath = subDir ? `${baseFolder}/${subDir}/` : `${baseFolder}/`;
|
||||||
let saved = base;
|
let saved = base;
|
||||||
let n = 1;
|
let n = 1;
|
||||||
while (seenFiles.has(keyPath + saved)) {
|
while (seenFiles.has(keyPath + saved)) {
|
||||||
@@ -10918,28 +10932,24 @@ app.get('/api/groups/:id/export/files', requireAuth, async (req, res) => {
|
|||||||
saved = `${stem}(${n})${ext}`;
|
saved = `${stem}(${n})${ext}`;
|
||||||
}
|
}
|
||||||
seenFiles.set(keyPath + saved, true);
|
seenFiles.set(keyPath + saved, true);
|
||||||
const zipPath = subDir ? `${folder}/${subDir}/${saved}` : `${folder}/${saved}`;
|
const zipPath = subDir ? `${baseFolder}/${subDir}/${saved}` : `${baseFolder}/${saved}`;
|
||||||
if (subDir && !seenFiles.has(folder + '/' + subDir + '/')) {
|
|
||||||
seenFiles.set(folder + '/' + subDir + '/', true);
|
|
||||||
zip.addDir(`${folder}/${subDir}`);
|
|
||||||
}
|
|
||||||
zip.addFile(zipPath, data, new Date(row.created_at || Date.now()));
|
zip.addFile(zipPath, data, new Date(row.created_at || Date.now()));
|
||||||
filesBuilt.push({ folder: subDir ? `${folder}/${subDir}` : folder, file: saved, original: row.name, size: data.length, created_at: row.created_at || null });
|
filesBuilt.push({ folder: subDir ? `${baseFolder}/${subDir}` : baseFolder, file: saved, original: row.name, module: row.module_name || null, student: row.student_name || null, size: data.length, created_at: row.created_at || null });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
let worksAdded = 0;
|
let worksAdded = 0;
|
||||||
let photosAdded = 0;
|
let photosAdded = 0;
|
||||||
let originalsAdded = 0;
|
let originalsAdded = 0;
|
||||||
for (const f of fileRows) if (await addEntryFile(f, null)) worksAdded++;
|
for (const f of fileRows) if (await addEntryFile(f, 'works')) worksAdded++;
|
||||||
for (const p of photoRows) {
|
for (const p of photoRows) {
|
||||||
if (await addEntryFile({ name: `photo_${new Date(p.created_at || Date.now()).toISOString().slice(0, 10)}.jpg`, path: p.photo_path, student_name: p.student_name, created_at: p.created_at }, 'Фото записи')) photosAdded++;
|
if (await addEntryFile({ name: `photo_${new Date(p.created_at || Date.now()).toISOString().slice(0, 10)}.jpg`, path: p.photo_path, student_name: p.student_name, module_id: p.module_id, module_name: p.module_name, created_at: p.created_at }, 'photos')) photosAdded++;
|
||||||
}
|
}
|
||||||
for (const p of entryPhotoRows) {
|
for (const p of entryPhotoRows) {
|
||||||
const storedName = String(p.photo_path).split('/').pop();
|
const storedName = String(p.photo_path).split('/').pop();
|
||||||
if (await addEntryFile({ name: storedName || 'photo.jpg', path: p.photo_path, student_name: p.student_name, created_at: p.created_at }, 'Фото записи')) photosAdded++;
|
if (await addEntryFile({ name: storedName || 'photo.jpg', path: p.photo_path, student_name: p.student_name, module_id: p.module_id, module_name: p.module_name, created_at: p.created_at }, 'photos')) photosAdded++;
|
||||||
}
|
}
|
||||||
for (const p of originalRows) {
|
for (const p of originalRows) {
|
||||||
if (await addEntryFile({ name: `original_${new Date(p.created_at || Date.now()).toISOString().slice(0, 10)}.jpg`, path: p.photo_original_path, student_name: p.student_name, created_at: p.created_at }, 'Фото записи/Оригиналы')) originalsAdded++;
|
if (await addEntryFile({ name: `original_${new Date(p.created_at || Date.now()).toISOString().slice(0, 10)}.jpg`, path: p.photo_original_path, student_name: p.student_name, module_id: p.module_id, module_name: p.module_name, created_at: p.created_at }, 'originals')) originalsAdded++;
|
||||||
}
|
}
|
||||||
const meta = {
|
const meta = {
|
||||||
exported_at: new Date().toISOString(),
|
exported_at: new Date().toISOString(),
|
||||||
|
|||||||
Reference in New Issue
Block a user