Skip to content

Commit

Permalink
refactor : move schema constants to db/constant.js file (foyzulka…
Browse files Browse the repository at this point in the history
  • Loading branch information
aninda052 committed Aug 6, 2023
1 parent 9855ac4 commit 9c9c5eb
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 25 deletions.
3 changes: 1 addition & 2 deletions client/src/sections/@dashboard/products/VideoCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export default function VideoCard({ video }) {
console.log('clicked', video);
};

let videoDuration = 0
if(duration) videoDuration = duration
const videoDuration = duration ?? 0;

return (
<Card onClick={onClickHandler}>
Expand Down
2 changes: 1 addition & 1 deletion server/scripts/seed-data/video.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { faker } = require('@faker-js/faker');
const { MongoManager } = require('../../src/modules/db/mongo');
const { VIDEO_STATUS, VIDEO_VISIBILITIES } = require('../../src/modules/db/schemas/videos');
const { VIDEO_STATUS, VIDEO_VISIBILITIES } = require('../../src/modules/db/constant');

const getFakeVideosData = (x) => {
const videos = [];
Expand Down
26 changes: 26 additions & 0 deletions server/src/modules/db/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

const PUBLIC_VISIBILITY = 'Public';
const PRIVATE_VISIBILITY = 'Private';
const UNLISTED_VISIBILITY = 'Unlisted';

const VIDEO_VISIBILITIES = [PUBLIC_VISIBILITY, PRIVATE_VISIBILITY, UNLISTED_VISIBILITY];



const PENDING_STATUS = "pending";
const PROCESSED_STATUS = "processed";
const PUBLISHED_STATUS = "published"

const VIDEO_STATUS = [PENDING_STATUS, PROCESSED_STATUS, PUBLISHED_STATUS];


module.exports = {
PENDING_STATUS,
PROCESSED_STATUS,
PUBLISHED_STATUS,
VIDEO_STATUS,
PUBLIC_VISIBILITY,
PRIVATE_VISIBILITY,
UNLISTED_VISIBILITY,
VIDEO_VISIBILITIES
}
5 changes: 1 addition & 4 deletions server/src/modules/db/schemas/videos.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { baseSchema, ensureCollection } = require('./common');

const VIDEO_VISIBILITIES = ['Public', 'Private', 'Unlisted'];
const VIDEO_STATUS = ["pending", "processed", "published"];
const { VIDEO_STATUS, VIDEO_VISIBILITIES } = require('../constant');

const name = 'videos';

Expand Down Expand Up @@ -144,6 +143,4 @@ const updateSchema = async (db) => {

module.exports = {
updateSchema,
VIDEO_STATUS,
VIDEO_VISIBILITIES,
};
12 changes: 8 additions & 4 deletions server/src/modules/models/video/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
const { validate } = require('./request');
const { VIDEO_QUEUE_EVENTS: QUEUE_EVENTS } = require('../../queues/constants');
const { addQueueItem } = require('../../queues/queue');
const { getVideoDuration } = require('../../queues/video-processor');
const { getVideoMetadata } = require('../../queues/video-processor');

const BASE_URL = `/api/videos`;

Expand Down Expand Up @@ -136,15 +136,19 @@ const setupRoutes = (app) => {
app.post(`${BASE_URL}/upload`, uploadProcessor, async (req, res) => {
try {

const videoDuration = await getVideoDuration(`./${req.file.path}`)
const videoMetadata = await getVideoMetadata(`./${req.file.path}`)
let videoDuration = 0
if (typeof videoMetadata != Error){
videoDuration = parseInt(videoMetadata.format.duration)
}

const dbPayload = {
...req.body,
fileName: req.file.filename,
originalName: req.file.originalname,
originalName: req.file.originalname,
recordingDate: new Date(),
videoLink: req.file.path,
viewCount:0,
status: "pending",
duration:videoDuration
};
console.log('dbPayload', dbPayload);
Expand Down
6 changes: 4 additions & 2 deletions server/src/modules/models/video/handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const eventEmitter = require('../../../event-manager').getInstance();
const { VIDEO_QUEUE_EVENTS } = require('../../queues/constants');
const { updateHistory, update } = require('./service');
const { PUBLISHED_STATUS } = require('../../db/constant')

const setup = () => {
// eventEmitter.on(VIDEO_QUEUE_EVENTS.VIDEO_UPLOADED, (data) => {
Expand All @@ -27,10 +28,11 @@ const setup = () => {
hlsPath: data.path,
});

const video = await update({
await update({
_id: data.id,
status: "published"
status: PUBLISHED_STATUS
});

return;
}

Expand Down
5 changes: 3 additions & 2 deletions server/src/modules/models/video/service.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { ObjectId } = require('mongodb');
const { Video } = require('../../db/collections');
const { PENDING_STATUS } = require('../../db/constant')


// TODO: add logging

const insert = async (document) => {
try {
return await Video.insert(document);
return await Video.insert({status: PENDING_STATUS, ...document}); // assigning default satus for all new videos
} catch (error) {
return error;
}
Expand All @@ -15,7 +17,6 @@ const update = async (document) => {
try {
return await Video.update(document);
} catch (error) {
console.error(error);
return error;
}
};
Expand Down
18 changes: 8 additions & 10 deletions server/src/modules/queues/video-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,21 @@ const processMp4ToHls = async (filePath, outputFolder, jobData) => {
return;
};

const getVideoDuration = (filePath) => {
const getVideoMetadata = (filePath) => {

// if any error occour return 0.0 second as default video duration
// else return video duration
// if any error occour return error
// else return video meta data
return new Promise((resolve,reject) => {
let duration = 0.0
ffmpeg.ffprobe(filePath, function(err, metadata) {
// getting video duration in second
if(!err){
duration = parseInt(metadata.format.duration)
}
resolve(duration)
if(err){
resolve(err);
};
resolve(metadata);
});
})


}


module.exports = { processRawFileToMp4, processMp4ToHls, generateThumbnail, getVideoDuration };
module.exports = { processRawFileToMp4, processMp4ToHls, generateThumbnail, getVideoMetadata };

0 comments on commit 9c9c5eb

Please sign in to comment.