71 lines
1.6 KiB
JavaScript
71 lines
1.6 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const PROJECTS_DIR = path.join('.');
|
|
|
|
function createFolder(name){
|
|
const projectDir = path.join(PROJECTS_DIR, `${name}`);
|
|
if (!fs.existsSync(projectDir)) {
|
|
fs.mkdirSync(projectDir, { recursive: true });
|
|
}
|
|
return projectDir;
|
|
}
|
|
|
|
function deleteFolder(name){
|
|
const projectDir = path.join(PROJECTS_DIR, `${name
|
|
}`);
|
|
if (fs.existsSync
|
|
(projectDir)) {
|
|
fs.rmSync(projectDir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
function scanAllImages(dir = 'storage') {
|
|
const projectDir = path.join(PROJECTS_DIR, dir);
|
|
let results = [];
|
|
|
|
function scanDirectory(directory) {
|
|
const files = fs.readdirSync(directory);
|
|
files.forEach(file => {
|
|
const filePath = path.join(directory, file);
|
|
const stat = fs.statSync(filePath);
|
|
if (stat.isDirectory()) {
|
|
scanDirectory(filePath);
|
|
} else if (file.endsWith('.jpg')) {
|
|
results.push(filePath);
|
|
}
|
|
});
|
|
}
|
|
|
|
scanDirectory(projectDir);
|
|
return results;
|
|
}
|
|
|
|
function saveFile(filePath, content) {
|
|
// Ensure content is a buffer
|
|
if (Buffer.isBuffer(content)) {
|
|
fs.writeFileSync(filePath, content);
|
|
} else {
|
|
throw new Error('Content must be a buffer');
|
|
}
|
|
}
|
|
|
|
function getFile(name){
|
|
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
|
return fs
|
|
.readFileSync(filePath);
|
|
}
|
|
|
|
function deleteFile(name){
|
|
const filePath = path.join(PROJECTS_DIR, `${name}`);
|
|
if (fs.existsSync(filePath))
|
|
fs.rmSync(filePath);
|
|
}
|
|
|
|
module.exports = {
|
|
createFolder,
|
|
deleteFolder,
|
|
scanAllImages,
|
|
saveFile,
|
|
getFile,
|
|
deleteFile
|
|
}; |