From 9c9c5ebf54625d704b23761aef7c5aef5600b51a Mon Sep 17 00:00:00 2001 From: aninda052 Date: Mon, 7 Aug 2023 00:29:47 +0600 Subject: [PATCH] refactor : move `schema constants` to `db/constant.js` file (#28) --- .../sections/@dashboard/products/VideoCard.js | 3 +-- server/scripts/seed-data/video.js | 2 +- server/src/modules/db/constant.js | 26 +++++++++++++++++++ server/src/modules/db/schemas/videos.js | 5 +--- server/src/modules/models/video/controller.js | 12 ++++++--- server/src/modules/models/video/handler.js | 6 +++-- server/src/modules/models/video/service.js | 5 ++-- server/src/modules/queues/video-processor.js | 18 ++++++------- 8 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 server/src/modules/db/constant.js diff --git a/client/src/sections/@dashboard/products/VideoCard.js b/client/src/sections/@dashboard/products/VideoCard.js index 3c38b9a..244f9b8 100644 --- a/client/src/sections/@dashboard/products/VideoCard.js +++ b/client/src/sections/@dashboard/products/VideoCard.js @@ -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 ( diff --git a/server/scripts/seed-data/video.js b/server/scripts/seed-data/video.js index 6dd4862..3490bc5 100644 --- a/server/scripts/seed-data/video.js +++ b/server/scripts/seed-data/video.js @@ -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 = []; diff --git a/server/src/modules/db/constant.js b/server/src/modules/db/constant.js new file mode 100644 index 0000000..d61fb98 --- /dev/null +++ b/server/src/modules/db/constant.js @@ -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 +} \ No newline at end of file diff --git a/server/src/modules/db/schemas/videos.js b/server/src/modules/db/schemas/videos.js index 97ffbf0..5936fc7 100644 --- a/server/src/modules/db/schemas/videos.js +++ b/server/src/modules/db/schemas/videos.js @@ -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'; @@ -144,6 +143,4 @@ const updateSchema = async (db) => { module.exports = { updateSchema, - VIDEO_STATUS, - VIDEO_VISIBILITIES, }; diff --git a/server/src/modules/models/video/controller.js b/server/src/modules/models/video/controller.js index 402e917..cc50cbf 100644 --- a/server/src/modules/models/video/controller.js +++ b/server/src/modules/models/video/controller.js @@ -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`; @@ -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); diff --git a/server/src/modules/models/video/handler.js b/server/src/modules/models/video/handler.js index b66c864..35b8e56 100644 --- a/server/src/modules/models/video/handler.js +++ b/server/src/modules/models/video/handler.js @@ -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) => { @@ -27,10 +28,11 @@ const setup = () => { hlsPath: data.path, }); - const video = await update({ + await update({ _id: data.id, - status: "published" + status: PUBLISHED_STATUS }); + return; } diff --git a/server/src/modules/models/video/service.js b/server/src/modules/models/video/service.js index e82d47d..ac44025 100644 --- a/server/src/modules/models/video/service.js +++ b/server/src/modules/models/video/service.js @@ -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; } @@ -15,7 +17,6 @@ const update = async (document) => { try { return await Video.update(document); } catch (error) { - console.error(error); return error; } }; diff --git a/server/src/modules/queues/video-processor.js b/server/src/modules/queues/video-processor.js index 87cc697..13958cd 100644 --- a/server/src/modules/queues/video-processor.js +++ b/server/src/modules/queues/video-processor.js @@ -106,18 +106,16 @@ 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); }); }) @@ -125,4 +123,4 @@ const getVideoDuration = (filePath) => { } -module.exports = { processRawFileToMp4, processMp4ToHls, generateThumbnail, getVideoDuration }; \ No newline at end of file +module.exports = { processRawFileToMp4, processMp4ToHls, generateThumbnail, getVideoMetadata }; \ No newline at end of file