mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-17 21:24:11 +00:00
feat: upload file
This commit is contained in:
+2
-1
@@ -7,10 +7,10 @@ const {
|
||||
dialog,
|
||||
ipcMain,
|
||||
shell,
|
||||
Notification,
|
||||
} = require('electron');
|
||||
const path = require('path');
|
||||
const { electron } = require('process');
|
||||
const { Notification } = require('electron');
|
||||
|
||||
const env = process.env.NODE_ENV || 'prod';
|
||||
|
||||
@@ -100,6 +100,7 @@ function createWindow() {
|
||||
// TODO: what are recommended alternatives to node integration?
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
enableRemoteModule: true,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ async function deleteFile(file) {
|
||||
|
||||
// parses version 1 of the data system
|
||||
async function parsev1(jsonData) {
|
||||
let numEntries = 0;
|
||||
if ('events' in jsonData) {
|
||||
let events = [];
|
||||
let ids = [];
|
||||
@@ -51,15 +52,19 @@ async function parsev1(jsonData) {
|
||||
isPublic: e.isPublic,
|
||||
id: e.id,
|
||||
});
|
||||
numEntries++;
|
||||
} else if (e.type === 'delay') {
|
||||
events.push({ ...delayDef, duration: e.duration });
|
||||
numEntries++;
|
||||
} else if (e.type === 'block') {
|
||||
events.push({ ...blockDef });
|
||||
numEntries++;
|
||||
}
|
||||
}
|
||||
// write to db
|
||||
db.data.events = events;
|
||||
db.write();
|
||||
console.log(`Uploaded file with ${numEntries} entries`);
|
||||
}
|
||||
|
||||
if ('event' in jsonData) {
|
||||
@@ -99,15 +104,7 @@ export const dbDownload = async (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
// Create controller for POST request to '/ontime/db'
|
||||
// Returns -
|
||||
export const dbUpload = async (req, res) => {
|
||||
if (!req.file) {
|
||||
res.status(400).send({ message: 'File not found' });
|
||||
return;
|
||||
}
|
||||
|
||||
const file = req.file.path;
|
||||
const upload = async (file, req, res) => {
|
||||
if (!fs.existsSync(file)) {
|
||||
res.status(500).send({ message: 'Upload failed' });
|
||||
return;
|
||||
@@ -115,11 +112,8 @@ export const dbUpload = async (req, res) => {
|
||||
|
||||
try {
|
||||
// get file
|
||||
let rawdata = fs.readFileSync(file);
|
||||
let uploadedJson = JSON.parse(rawdata);
|
||||
|
||||
// delete file
|
||||
deleteFile(file);
|
||||
const rawdata = fs.readFileSync(file);
|
||||
const uploadedJson = JSON.parse(rawdata);
|
||||
|
||||
// check version
|
||||
if (uploadedJson.settings.version === 1) parsev1(uploadedJson);
|
||||
@@ -134,3 +128,25 @@ export const dbUpload = async (req, res) => {
|
||||
res.status(400).send({ message: error });
|
||||
}
|
||||
};
|
||||
|
||||
// Create controller for POST request to '/ontime/db'
|
||||
// Returns -
|
||||
export const dbUpload = async (req, res) => {
|
||||
if (!req.file) {
|
||||
res.status(400).send({ message: 'File not found' });
|
||||
return;
|
||||
}
|
||||
|
||||
const file = req.file.path;
|
||||
upload(file, req, res);
|
||||
};
|
||||
|
||||
// Create controller for POST request to '/ontime/dbpath'
|
||||
// Returns -
|
||||
export const dbPathToUpload = async (req, res) => {
|
||||
if (!req.body.path) {
|
||||
res.status(400).send({ message: 'Path to file not found' });
|
||||
return;
|
||||
}
|
||||
upload(req.body.path, req, res);
|
||||
};
|
||||
|
||||
@@ -2,10 +2,17 @@ import express from 'express';
|
||||
import uploadJson from '../utils/upload.js';
|
||||
export const router = express.Router();
|
||||
|
||||
import { dbDownload, dbUpload } from '../controllers/ontimeController.js';
|
||||
import {
|
||||
dbDownload,
|
||||
dbUpload,
|
||||
dbPathToUpload,
|
||||
} from '../controllers/ontimeController.js';
|
||||
|
||||
// create route between controller and '/ontime/db' endpoint
|
||||
router.get('/db', dbDownload);
|
||||
|
||||
// create route between controller and '/ontime/db' endpoint
|
||||
router.post('/db', uploadJson, dbUpload);
|
||||
|
||||
// create route between controller and '/ontime/dbpath' endpoint
|
||||
router.post('/dbpath', dbPathToUpload);
|
||||
|
||||
Reference in New Issue
Block a user