Ajout de la route de copie
This commit is contained in:
@@ -16,6 +16,12 @@ Content-Type: application/x-www-form-urlencoded
|
|||||||
|
|
||||||
device = /dev/diskC
|
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
|
### Send POST request with body as parameters
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ var logger = require('morgan');
|
|||||||
var deviceRouter = require('./routes/device');
|
var deviceRouter = require('./routes/device');
|
||||||
var usersRouter = require('./routes/users');
|
var usersRouter = require('./routes/users');
|
||||||
var lightshowRouter = require('./routes/lightshow');
|
var lightshowRouter = require('./routes/lightshow');
|
||||||
|
var copieManagerRouter = require('./routes/copieManager');
|
||||||
|
|
||||||
var app = express();
|
var app = express();
|
||||||
|
|
||||||
@@ -23,6 +24,7 @@ app.use(express.static(path.join(__dirname, 'public')));
|
|||||||
app.use('/devices', deviceRouter);
|
app.use('/devices', deviceRouter);
|
||||||
app.use('/users', usersRouter);
|
app.use('/users', usersRouter);
|
||||||
app.use('/lightshow', lightshowRouter);
|
app.use('/lightshow', lightshowRouter);
|
||||||
|
app.use('/copy', copieManagerRouter);
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
app.use(function(req, res, next) {
|
app.use(function(req, res, next) {
|
||||||
|
|||||||
27
server/routes/copieManager.js
Normal file
27
server/routes/copieManager.js
Normal 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;
|
||||||
58
server/service/CopyManagerService.js
Normal file
58
server/service/CopyManagerService.js
Normal 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
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
const { PrismaClient } = require('@prisma/client')
|
const { PrismaClient } = require('@prisma/client')
|
||||||
|
const fs = require('fs')
|
||||||
|
|
||||||
const prisma = new PrismaClient()
|
const prisma = new PrismaClient()
|
||||||
|
const usbLightshow = process.env.LIGHTSHOW_DIR || '';
|
||||||
class LigthshowService{
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -28,5 +28,23 @@ class Deviceinformation {
|
|||||||
})
|
})
|
||||||
return element;
|
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
|
module.exports = Deviceinformation
|
||||||
Reference in New Issue
Block a user