Skip to content

Commit

Permalink
Configured Services
Browse files Browse the repository at this point in the history
  • Loading branch information
earliest777 committed Dec 29, 2021
1 parent 358e485 commit 728ba8e
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ dist
.tern-port

serviceAccountKey.json
uploads/*
uploads/*
tools/*
66 changes: 20 additions & 46 deletions api/controller/admin-controller.js
Original file line number Diff line number Diff line change
@@ -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,
}
22 changes: 22 additions & 0 deletions api/controller/session.js
Original file line number Diff line number Diff line change
@@ -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;
Empty file removed api/middleware/auth.js
Empty file.
2 changes: 1 addition & 1 deletion api/routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 1 addition & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
});
Expand Down
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
17 changes: 17 additions & 0 deletions services/bento4-service.js
Original file line number Diff line number Diff line change
@@ -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);
});
File renamed without changes.
69 changes: 67 additions & 2 deletions services/video-service.js
Original file line number Diff line number Diff line change
@@ -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()
}
}

module.exports = {
videoManager,
multerUpload,
};

0 comments on commit 728ba8e

Please sign in to comment.