diff --git a/.gitignore b/.gitignore index a24e215..c1828ca 100644 --- a/.gitignore +++ b/.gitignore @@ -104,4 +104,5 @@ dist .tern-port serviceAccountKey.json -uploads/* \ No newline at end of file +uploads/* +tools/* \ No newline at end of file diff --git a/api/controller/admin-controller.js b/api/controller/admin-controller.js index 55b52ad..f43dc0b 100644 --- a/api/controller/admin-controller.js +++ b/api/controller/admin-controller.js @@ -1,59 +1,33 @@ -const multer = require('multer'); -const fs = require('fs').promises; -const path = require('path'); -const config = require('../../config'); - -const storage = multer.diskStorage({ - destination: async (req, file, cb) => { - const filePath = path.join(__dirname, `../../uploads/${ Date.now() }`); - - /* Multer by default doesn't makes file if doesn't exisst, so use FS*/ - await fs.mkdir(filePath, {recursive: true}); - cb(null, filePath); - }, - - filename: (req, file, cb) => { - cb(null, `${file.originalname}`); - } -}); - -const upload = multer({ storage: storage }); - +const session = require('./session'); +const {videoManager, multerUpload} = require('../../services/video-service'); +const { response } = require('express'); const getVideoStatus = async (req, res) => { - const videoId = req.params.videoId; - res.send(videoId); -}; -const uploadVideo = async (req, res) => { - const file = req.body.file; - console.log(file); - res.send("Ok File!"); + const sessionObj = await session.restore(); + console.log(sessionObj); }; -const deleteVideo = async (req, res) => { - const videoId = req.params.videoId; - res.send(videoId); -}; -// const createSession = (req, res, next) => { +const uploadVideo = async (req, res, next) => { -// }; - -// app.post('/upload', upload.single('file'), (req, res) => { -// console.log(req.file); -// console.log(Math.floor(req.file.size/1024/1024) + "MB"); -// res.send('This is Upload Video Route!'); -// }); + req.locals = await session.generate(); + const response = await videoManager.uploadVideo(req, res); + + if(!response.status) { + next(response.data); + return; + } + + res.status(response.status).send(response.data); +}; -// const _uploadVideo = (req, res, next) => { -// // console.log(req.file.); -// res.send('This is Upload Video Route!'); -// }; +const upload = [uploadVideo]; -// const uploadVideo = [upload.single(('avatar')), _uploadVideo]; +const deleteVideo = async (req, res) => { +}; module.exports = { getVideoStatus, - uploadVideo, + upload, deleteVideo, } \ No newline at end of file diff --git a/api/controller/session.js b/api/controller/session.js new file mode 100644 index 0000000..d99804a --- /dev/null +++ b/api/controller/session.js @@ -0,0 +1,22 @@ +const genuuid = require('uuid').v4; + +async function generateSession() { + + const sessionObj = { + id: genuuid(), + time: Date.now(), + } + + return sessionObj; +} + +async function restoreSession() { + +} + +const session = { + generate: generateSession, + restore: restoreSession, +} + +module.exports = session; \ No newline at end of file diff --git a/api/middleware/auth.js b/api/middleware/auth.js deleted file mode 100644 index e69de29..0000000 diff --git a/api/routes/admin.js b/api/routes/admin.js index 3d3e895..4e73412 100644 --- a/api/routes/admin.js +++ b/api/routes/admin.js @@ -2,7 +2,7 @@ const app = require('express').Router(); const adminController = require('../controller/admin-controller'); app.get('/:videoId/status', adminController.getVideoStatus); -app.post('/upload', adminController.uploadVideo); +app.post('/upload', adminController.upload); app.delete('/:videoId/delete', adminController.deleteVideo); module.exports = app; \ No newline at end of file diff --git a/app.js b/app.js index c13467a..060fe49 100644 --- a/app.js +++ b/app.js @@ -4,7 +4,6 @@ const path = require('path'); const logger = require('./loaders/logger'); const config = require('./config'); const routes = require('./api'); -const bucketStorage = require('./loaders/firebase-storage'); const app = express(); @@ -26,8 +25,7 @@ app.use(config.api.prefix, routes()); /* Error Handling */ app.use((err, req, res, next) => { - logger.error(err.stack); - logger.error(err.message, {url: req.url, err}); + logger.error(err, err.stack); res.status(500).send({error: "Internal Server Error!"}); next; }); diff --git a/package-lock.json b/package-lock.json index fa96bd0..cc804bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "firebase": "^9.6.1", "firebase-admin": "^10.0.1", "multer": "^1.4.4", + "uuid": "^8.3.2", "winston": "^3.3.3" } }, @@ -3059,7 +3060,6 @@ "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "optional": true, "bin": { "uuid": "dist/bin/uuid" } @@ -5790,8 +5790,7 @@ "uuid": { "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "optional": true + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" }, "vary": { "version": "1.1.2", diff --git a/package.json b/package.json index 575319c..4756349 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "firebase": "^9.6.1", "firebase-admin": "^10.0.1", "multer": "^1.4.4", + "uuid": "^8.3.2", "winston": "^3.3.3" } } diff --git a/services/bento4-service.js b/services/bento4-service.js new file mode 100644 index 0000000..f08b923 --- /dev/null +++ b/services/bento4-service.js @@ -0,0 +1,17 @@ +const { exec } = require('child_process'); + +const ls = exec('dir', { cwd: '../../Tools/Bento4-SDK' }, function (error, stdout, stderr) { + + if(error) { + console.log(error.stack); + console.log('Error Code: ' + error.code); + console.log('Signal received: ' + error.signal); + } + + console.log('Child Process STDOUT: ' + stdout); + console.log('Child Process STDERR: ' + stderr); +}); + +ls.on('exit', function(code) { + console.log('Child process exited with exit code ' + code); +}); \ No newline at end of file diff --git a/loaders/firebase-storage.js b/services/firebase-storage.js similarity index 100% rename from loaders/firebase-storage.js rename to services/firebase-storage.js diff --git a/services/video-service.js b/services/video-service.js index 71096dd..00dc8c5 100644 --- a/services/video-service.js +++ b/services/video-service.js @@ -1,9 +1,74 @@ +const multer = require('multer'); +const fs = require('fs').promises; +const path = require('path'); +const logger = require('../loaders/logger'); + +const storage = multer.diskStorage({ + destination: async (req, file, cb) => { + const filePath = path.join(__dirname, `../uploads/${ req.locals.id }`); + + /* Multer by default doesn't makes file if doesn't exisst, so use FS*/ + await fs.mkdir(filePath, {recursive: true}); + cb(null, filePath); + }, + + filename: (req, file, cb) => { + cb(null, `${ Date.now() + path.extname(file.originalname) }`); + } +}); + +const multerUpload = multer({ + storage: storage, + limits: { fileSize: 1 * 1024 * 1024 }, +}).single('file'); + class videoManager { - // uploadVideo(); + static async uploadVideo(req, res) { + + let response = { + status: '', + data: {}, + } + + multerUpload(req, res, function (err) { + + if(err) { + logger.error(err, {err}); + + if(err instanceof multer.MulterError) { + response.status = 400; + response.data = { + error: err.message, + } + } + + videoManager.cleanUp(req); + return; + } + //ye kaam asyc ho nahi raha phir bhi response khaali print ho rha hai + + response.status = 200; + response.data = req.locals; + }); + + console.log(response); + return response; + } + + static async cleanUp(req) { + const filePath = path.join(__dirname, `../uploads/${ req.locals.id }`); + await fs.rm(filePath, {recursive: true}); + } + // getVideoStatus(); // deleteVideo(); // scheduleDeleteJob(); // fileValidations(); // afterJob() -} \ No newline at end of file +} + +module.exports = { + videoManager, + multerUpload, +}; \ No newline at end of file