Model Update
This commit is contained in:
@@ -2,6 +2,8 @@ import { PrintJob } from '$lib/models/PrintJob';
|
||||
import { connectDB } from '$lib/server/db';
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { statSync, existsSync } from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
if (!locals.user) throw redirect(303, '/login');
|
||||
@@ -16,7 +18,31 @@ export const load: PageServerLoad = async ({ locals }) => {
|
||||
.sort({ date: -1 })
|
||||
.lean();
|
||||
|
||||
// Add file sizes to each model
|
||||
const modelsWithSizes = printsWithSTL.map(print => {
|
||||
let fileSize = 0;
|
||||
if (print.stl_file) {
|
||||
try {
|
||||
const filePath = path.join('static', print.stl_file);
|
||||
if (existsSync(filePath)) {
|
||||
const stats = statSync(filePath);
|
||||
fileSize = stats.size;
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore file read errors
|
||||
}
|
||||
}
|
||||
return {
|
||||
...print,
|
||||
fileSize
|
||||
};
|
||||
});
|
||||
|
||||
// Calculate total storage
|
||||
const totalStorage = modelsWithSizes.reduce((sum, m) => sum + m.fileSize, 0);
|
||||
|
||||
return {
|
||||
models: JSON.parse(JSON.stringify(printsWithSTL))
|
||||
models: JSON.parse(JSON.stringify(modelsWithSizes)),
|
||||
totalStorage
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user