Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion controllers/cronograma.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
import db from '../firebase.js';
import { db, bucket } from '../firebase.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

// acho que precisava configurar manualmente, qualquer coisa eu mudo
const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Baixar todos os cronogramas
export const downloadAllCronogramas = async (req, res) => {
try {
const directory = path.join(__dirname, '..', 'Downloads', 'cronogramasSiproj');

if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}

const [files] = await bucket.getFiles({ prefix: 'cronogramas/'});

if (files.length === 0){
return res.status(400).json({ message: 'Nenhum cronograma foi encontrado'});
}

for (const file of files) {
const filePath = path.join(directory, file.name.split('/').pop());
await file.download({destination: filePath});
console.log(`${file.name} baixado para ${filePath}`);
}

res.status(200).json({
message: `Cronogramas baixados para ${directory} com sucesso`,
files: files.map(file => file.name)
})
} catch (error) {
res.status(500).send(error.message);
}
}

//Pegar todos os cronogramas
export const getAllCronograma = async (req, res) => {
Expand Down
6 changes: 4 additions & 2 deletions firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const serviceAccount = {
};

admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
credential: admin.credential.cert(serviceAccount),
storageBucket: process.env.FIREBASE_STORAGE_BUCKET
});

const db = admin.firestore();
const bucket = admin.storage().bucket();

export default db;
export default { db, bucket };
7 changes: 6 additions & 1 deletion routes/cronograma.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import {
getByAsCronograma,
postCronograma,
putCronograma,
getAllLogs
getAllLogs,
downloadAllCronogramas
} from '../controllers/cronograma.js';

const router = express.Router();

//BAIXAR CRONOGRAMAS
// GET ALL CRONOGRAMAS https://apisiproj.vercel.app/cronograma/download
router.get('/download', downloadAllCronogramas);

// CRONOGRAMA CRUD
//GET ALL https://apisiproj.vercel.app/cronograma
router.get('/', getAllCronograma);
Expand Down