Ajout de la route de copie

This commit is contained in:
Quentin Millardet
2024-01-06 22:16:59 +01:00
parent 132a889dae
commit 02cb1cd738
6 changed files with 149 additions and 0 deletions

View File

@@ -16,6 +16,12 @@ Content-Type: application/x-www-form-urlencoded
device = /dev/diskC
### Post information of unkow device
POST {{host}}/copy/server
Content-Type: application/x-www-form-urlencoded
device=/dev/disk3&mountpoint=Test&lightshowName=Ghostbuster
### Send POST request with body as parameters

View File

@@ -7,6 +7,7 @@ var logger = require('morgan');
var deviceRouter = require('./routes/device');
var usersRouter = require('./routes/users');
var lightshowRouter = require('./routes/lightshow');
var copieManagerRouter = require('./routes/copieManager');
var app = express();
@@ -23,6 +24,7 @@ app.use(express.static(path.join(__dirname, 'public')));
app.use('/devices', deviceRouter);
app.use('/users', usersRouter);
app.use('/lightshow', lightshowRouter);
app.use('/copy', copieManagerRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {

View File

@@ -0,0 +1,27 @@
var express = require('express');
var router = express.Router();
const CopyManagerService = require('../service/CopyManagerService')
/* GET users listing. */
router.post('/server', async function(req, res, next) {
//console.log(req.body.hasOwnProperty('device'))
let device = req.body.device;
let mountpoint = req.body.mountpoint.replace(/\\/g, '');
let lightshowName = req.body.lightshowName.replace(/\\/g, '');
try {
let copyManagerService = new CopyManagerService();
await copyManagerService.copyFromDisk(
device,
mountpoint,
lightshowName
)
res.status(200).json({})
} catch (e){
res.status(500).json({'message' : e.stack})
}
});
module.exports = router;

View File

@@ -0,0 +1,58 @@
const fs = require('fs');
const LigthshowManager = require('./LightshowService')
const DeviceManager = require('./deviceinformation');
const { join } = require('node:path');
class CopyManagerService{
async copyFromDisk(device, mountPointLabel, lightshowName ){
let lightshowManagerService = new LigthshowManager();
if (!lightshowManagerService.isExistLightshowOnServer(lightshowName)){
throw new Error('Ce ligtshow n\'existe pas')
}
let ligthsowAudioFile = lightshowManagerService.getAudioFileName(lightshowName);
let ligthsowFseqFile = lightshowManagerService.getFseqLigthshow(lightshowName);
let baseDevice = await this.#getDeviceBaseWithLigthShow(device, mountPointLabel);
await this.#moveTeslaCamDir(device, mountPointLabel)
if (this.#isAlreadyExistLightshow(baseDevice, lightshowName)){
throw new Error('Already exist Lighttshow on device, please check file')
}
fs.mkdirSync(baseDevice, { recursive: true })
fs.copyFileSync(ligthsowAudioFile, baseDevice+lightshowName + '.mp3' )
fs.copyFileSync(ligthsowFseqFile, baseDevice + lightshowName + '.fseq' )
}
#isAlreadyExistLightshow(deviceMountPouint, lightshowName){
let ligthshowBase = deviceMountPouint + lightshowName;
let fseq = fs.existsSync( ligthshowBase+ '.fseq')
let wav = fs.existsSync(ligthshowBase + '.wav')
let mp3 = fs.existsSync(ligthshowBase + '.mp3')
return mp3 || wav || fseq;
}
async #moveTeslaCamDir(device, mountPointLabel){
let deviceManager = new DeviceManager();
let deviceMountPoint = await deviceManager.getMountPoint(device, mountPointLabel);
let teslacamDir = deviceMountPoint + '/TeslaCam/'
if(fs.existsSync(teslacamDir)){
let date = new Date();
fs.renameSync(teslacamDir, deviceMountPoint + '/TeslaCam-old/' )
}
return true;
}
async #getDeviceBaseWithLigthShow(device, mountPointLabel){
let deviceManager = new DeviceManager();
let deviceMountPoint = await deviceManager.getMountPoint(device, mountPointLabel);
return deviceMountPoint + '/Lightshow/'
}
}
module.exports = CopyManagerService

View File

@@ -1,6 +1,8 @@
const { PrismaClient } = require('@prisma/client')
const fs = require('fs')
const prisma = new PrismaClient()
const usbLightshow = process.env.LIGHTSHOW_DIR || '';
class LigthshowService{
@@ -22,6 +24,42 @@ class LigthshowService{
// },
// })
// }
isExistLightshowOnServer(ligthshowName){
let audio = this.getAudioFileName(ligthshowName) !== null;
let fseq = this.getFseqLigthshow(ligthshowName) !== null;
return audio && fseq;
}
#getBaseFile(ligthshowName){
return usbLightshow + '/' + ligthshowName
}
getAudioFileName(ligthshowName){
let base = this.#getBaseFile(ligthshowName)
let extensions = [ 'mp3', 'wav']
let filename = null;
extensions.forEach((ext) => {
let tmpfileName = base + '.' + ext
if (fs.existsSync(tmpfileName) && filename === null){
filename = tmpfileName;
}else if (fs.existsSync(tmpfileName) && filename !== null){
throw new Error('Multiple audio file for the same Lightshow')
}
})
return filename;
}
getFseqLigthshow(ligthshowName){
let filename = null;
let base2 = usbLightshow + '/' + ligthshowName
let tmp = base2 + '.fseq'
if (fs.existsSync(tmp)){
filename = tmp;
}
return filename;
}
}

View File

@@ -28,5 +28,23 @@ class Deviceinformation {
})
return element;
}
async getMountPoint( deviceName, pointName){
const drivelist = require('drivelist');
const drives =await drivelist.list()
let element = null;
drives.forEach((drive) => {
if (drive.device === deviceName ){
drive.mountpoints.forEach((mountPoint) => {
if (mountPoint.label === pointName){
element = mountPoint.path
}
})
}
})
return element;
}
}
module.exports = Deviceinformation