streamby-core
Version:
StreamBy middleware framework for media storage management
252 lines (251 loc) • 12.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportRouter = exportRouter;
const express_1 = require("express");
const manager_1 = require("../../models/manager");
const auth_1 = require("../../utils/auth");
const export_1 = require("../../services/export");
const connectionManager_1 = require("../../adapters/database/connectionManager");
const mongodb_1 = require("mongodb");
function exportRouter(config) {
const router = (0, express_1.Router)();
const Project = (0, manager_1.getModel)('projects');
router.get('/projects/:id/exports/:export_id', async (req, res) => {
try {
const auth = await config.authProvider(req);
if (!auth ||
!auth.userId ||
!auth.role) {
return res.status(401).json({ message: 'Unauthorized' });
}
const projectId = req.params.id;
const exportId = req.params.export_id;
const project = await Project.findOne({ _id: projectId });
if (!project) {
return res.status(404).json({ message: 'Project not found' });
}
if (!(0, auth_1.isProjectMember)(project, auth.userId)) {
return res.status(403).json({ message: 'Unauthorized access' });
}
const exportMetadata = project.exports.find((e) => e.id.toString() === exportId);
if (!exportMetadata) {
return res.status(404).json({ message: 'Export not found in this project' });
}
const targetDb = config.databases?.find(db => db.type === project.dbType);
if (!targetDb) {
return res.status(500).json({ message: `Database connection not found for type ${project.dbType}` });
}
const connection = (0, connectionManager_1.getConnection)(targetDb.id);
let data;
if (project.dbType === 'nosql') {
const db = connection.client.db();
if (exportMetadata.type === 'raw') {
const rawData = await db.collection(exportMetadata.collectionName).findOne({ _id: new mongodb_1.ObjectId(exportId) });
data = rawData ? { json: rawData.json, name: rawData.name, method: rawData.method, collectionName: rawData.collectionName, createdAt: rawData.createdAt, updatedAt: rawData.updatedAt, type: exportMetadata.type } : null;
}
else {
data = await db.collection(exportMetadata.collectionName).find({ __metadata: { $exists: false } }).toArray();
}
}
else if (project.dbType === 'sql') {
// ... SQL implementation needed
}
if (!data) {
return res.status(404).json({ message: 'Export data not found' });
}
res.json({ data, message: 'Export data fetched successfully' });
}
catch (err) {
res.status(500).json({ message: 'Failed to fetch export data', details: err.message });
}
});
router.post('/projects/:id/exports', async (req, res) => {
try {
const auth = await config.authProvider(req);
if (!auth ||
!auth.userId ||
!auth.role) {
return res.status(401).json({ message: 'Unauthorized' });
}
const projectId = req.params.id;
const { name, description, collectionName, jsonData } = req.body;
if (!name || !collectionName || !jsonData) {
return res.status(400).json({ message: 'Missing export name, collectionName, or jsonData' });
}
const project = await Project.findOne({ _id: projectId });
if (!project || !(0, auth_1.isProjectMember)(project, auth.userId)) {
return res.status(403).json({ message: 'Unauthorized project access' });
}
const result = await (0, export_1.createExport)(config, projectId, name, collectionName, jsonData, project.dbType);
res.status(201).json({ data: result, message: result.message });
}
catch (err) {
res.status(500).json({ message: 'Failed to create raw export', details: err.message });
}
});
router.patch('/projects/:id/exports/:export_id', async (req, res) => {
try {
const auth = await config.authProvider(req);
if (!auth || !auth.userId) {
return res.status(401).json({ message: 'Unauthorized' });
}
const { id: projectId, export_id: exportId } = req.params;
const { name, collectionName, description, jsonData } = req.body;
if (!name || !collectionName || !jsonData) {
return res.status(400).json({ message: 'Missing name, collectionName, or jsonData' });
}
const project = await Project.findOne({ _id: projectId });
if (!project || !(0, auth_1.isProjectMember)(project, auth.userId)) {
return res.status(403).json({ message: 'Unauthorized project access' });
}
const result = await (0, export_1.updateExport)(config, projectId, exportId, name, collectionName, jsonData, project.dbType);
res.status(200).json({ data: result, message: result.message });
}
catch (err) {
res.status(500).json({ message: 'Failed to update raw export', details: err.message });
}
});
router.delete('/projects/:id/exports/:export_id', async (req, res) => {
try {
const auth = await config.authProvider(req);
if (!auth || !auth.userId) {
return res.status(401).json({ message: 'Unauthorized' });
}
const { id: projectId, export_id: exportId } = req.params;
const project = await Project.findOne({ _id: projectId });
if (!project || !(0, auth_1.isProjectMember)(project, auth.userId)) {
return res.status(403).json({ message: 'Unauthorized project access' });
}
const exportMetadata = project.exports.find((e) => e.id.toString() === exportId);
if (!exportMetadata) {
return res.status(404).json({ message: 'Export not found in this project' });
}
await (0, export_1.deleteExport)(config, projectId, exportId, project.dbType, exportMetadata.collectionName);
res.status(200).json({ message: 'Export deleted successfully' });
}
catch (err) {
res.status(500).json({ message: 'Failed to delete export', details: err.message });
}
});
router.get('/:projectId/public-export/:exportName', async (req, res) => {
try {
const { projectId, exportName } = req.params;
const project = await Project.findOne({ _id: projectId });
if (!project) {
return res.status(404).json({ message: 'Project not found' });
}
const exportMetadata = project.exports.find((e) => e.name === exportName);
if (!exportMetadata) {
return res.status(404).json({ message: 'Export not found in this project' });
}
const targetDb = config.databases?.find(db => db.type === project.dbType);
if (!targetDb) {
return res.status(500).json({ message: `Database connection not found for type ${project.dbType}` });
}
const connection = (0, connectionManager_1.getConnection)(targetDb.id);
let data;
if (project.dbType === 'nosql') {
const db = connection.client.db();
if (exportMetadata.type === 'raw') {
const rawData = await db.collection(exportMetadata.collectionName).findOne({ _id: new mongodb_1.ObjectId(exportMetadata.id) });
data = rawData ? rawData.json : null;
}
else {
data = await db.collection(exportMetadata.collectionName).find({ __metadata: { $exists: false } }).toArray();
}
}
else if (project.dbType === 'sql') {
// SQL implementation for public exports will go here
return res.status(501).json({ message: 'SQL public exports not yet implemented' });
}
if (!data) {
return res.status(404).json({ message: 'Export data not found' });
}
res.json(data); // Return the raw data directly
}
catch (err) {
res.status(500).json({ message: 'Failed to fetch public export data', details: err.message });
}
});
router.get('/:projectId/public-export/:exportName', async (req, res) => {
try {
const { projectId, exportName } = req.params;
const project = await Project.findOne({ _id: projectId });
if (!project) {
return res.status(404).json({ message: 'Project not found' });
}
const exportMetadata = project.exports.find((e) => e.name === exportName);
if (!exportMetadata) {
return res.status(404).json({ message: 'Export not found in this project' });
}
const targetDb = config.databases?.find(db => db.type === project.dbType);
if (!targetDb) {
return res.status(500).json({ message: `Database connection not found for type ${project.dbType}` });
}
const connection = (0, connectionManager_1.getConnection)(targetDb.id);
let data;
if (project.dbType === 'nosql') {
const db = connection.client.db();
if (exportMetadata.type === 'raw') {
const rawData = await db.collection(exportMetadata.collectionName).findOne({ _id: new mongodb_1.ObjectId(exportMetadata.id) });
data = rawData ? rawData.json : null;
}
else {
data = await db.collection(exportMetadata.collectionName).find({ __metadata: { $exists: false } }).toArray();
}
}
else if (project.dbType === 'sql') {
// SQL implementation for public exports will go here
return res.status(501).json({ message: 'SQL public exports not yet implemented' });
}
if (!data) {
return res.status(404).json({ message: 'Export data not found' });
}
res.json(data); // Return the raw data directly
}
catch (err) {
res.status(500).json({ message: 'Failed to fetch public export data', details: err.message });
}
});
router.get('/:projectId/public-export/:exportName', async (req, res) => {
try {
const { projectId, exportName } = req.params;
const project = await Project.findOne({ _id: projectId });
if (!project) {
return res.status(404).json({ message: 'Project not found' });
}
const exportMetadata = project.exports.find((e) => e.name === exportName);
if (!exportMetadata) {
return res.status(404).json({ message: 'Export not found in this project' });
}
const targetDb = config.databases?.find(db => db.type === project.dbType);
if (!targetDb) {
return res.status(500).json({ message: `Database connection not found for type ${project.dbType}` });
}
const connection = (0, connectionManager_1.getConnection)(targetDb.id);
let data;
if (project.dbType === 'nosql') {
const db = connection.client.db();
if (exportMetadata.type === 'raw') {
const rawData = await db.collection(exportMetadata.collectionName).findOne({ _id: new mongodb_1.ObjectId(exportMetadata.id) });
data = rawData ? rawData.json : null;
}
else {
data = await db.collection(exportMetadata.collectionName).find({ __metadata: { $exists: false } }).toArray();
}
}
else if (project.dbType === 'sql') {
// SQL implementation for public exports will go here
return res.status(501).json({ message: 'SQL public exports not yet implemented' });
}
if (!data) {
return res.status(404).json({ message: 'Export data not found' });
}
res.json(data); // Return the raw data directly
}
catch (err) {
res.status(500).json({ message: 'Failed to fetch public export data', details: err.message });
}
});
return router;
}