41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { getPostsDirectory } from '@/lib/postsDirectory';
|
|
|
|
const postsDirectory = getPostsDirectory();
|
|
|
|
function getFolderStats(folderPath: string) {
|
|
const fullPath = path.join(postsDirectory, folderPath);
|
|
let items = 0;
|
|
let size = 0;
|
|
let created = '';
|
|
try {
|
|
const stat = fs.statSync(fullPath);
|
|
created = stat.birthtime.toISOString();
|
|
const walk = (dir: string) => {
|
|
const files = fs.readdirSync(dir);
|
|
for (const file of files) {
|
|
const filePath = path.join(dir, file);
|
|
const stats = fs.statSync(filePath);
|
|
if (stats.isDirectory()) {
|
|
walk(filePath);
|
|
} else {
|
|
items++;
|
|
size += stats.size;
|
|
}
|
|
}
|
|
};
|
|
walk(fullPath);
|
|
} catch {
|
|
// ignore errors
|
|
}
|
|
return { created, items, size };
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const folderPath = searchParams.get('path') || '';
|
|
const stats = getFolderStats(folderPath);
|
|
return NextResponse.json(stats);
|
|
}
|