Skip to content

Commit

Permalink
Merge pull request #77 from AVtheking/course_Ankit
Browse files Browse the repository at this point in the history
Course ankit
  • Loading branch information
AVtheking authored Nov 21, 2023
2 parents a661c5c + ba10624 commit 6ff615b
Show file tree
Hide file tree
Showing 67 changed files with 94 additions and 34 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:20-alpine

WORKDIR /usr/src/app

COPY package*.json ./


RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]
8 changes: 7 additions & 1 deletion controllers/course_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ const courseCtrl = {
select: "_id username name profileimg domain bio",
})
.populate({
path: "videos",
path: "videos.video",
select:
"_id videoTitle videoUrl videoDuration videoUrl_144p videoUrl_360p videoUrl_720p",
})
.populate({
path: "preview",
select:
"_id videoTitle videoUrl videoDuration videoUrl_144p videoUrl_360p videoUrl_720p",
});

if (!course) {
return next(new ErrorHandler(400, "No course found"));
}
Expand Down
77 changes: 57 additions & 20 deletions controllers/teacher_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const teacherCtrl = {

const existingTitle = await Course.findOne({ title });
if (existingTitle) {
fs.unlinkSync("public/thumbnail" + "/" + req.file.filename);
return next(
new ErrorHandler(400, "Please select different course title")
);
Expand All @@ -158,12 +159,16 @@ const teacherCtrl = {
},
});
} catch (e) {
fs.unlinksync("public/thumbnail" + "/" + req.file.filename);
next(e);
}
},

uploadVideo_toCourse: async (req, res, next) => {
let noteFilePath, videoFilePath, inputFilePath, inputFileName;
let noteFilePath = null,
videoFilePath,
inputFilePath,
inputFileName;
try {
const courseid = req.params.courseId;
// console.log(courseid);
Expand All @@ -173,10 +178,24 @@ const teacherCtrl = {
const { videoTitle } = req.body;
const result2 = await videoSchema.validateAsync({ videoTitle });
const videotitle = result2.videoTitle;
const notesfile = req.files.notes;
const videofile = req.files.video;

let course = await Course.findById(courseId);
if (notesfile) {
noteFilePath = "public/course_notes" + "/" + notesfile[0].filename;
}
if (!videofile) {
if (noteFilePath != null) {
fs.unlinkSync(noteFilePath);
}
return next(new ErrorHandler(400, "Please upload a video file"));
}
videoFilePath = "public/course_videos" + "/" + videofile[0].filename;

if (!course) {
fs.unlinkSync(noteFilePath);
fs.unlinkSync(videoFilePath);
return next(
new ErrorHandler(
400,
Expand All @@ -186,19 +205,17 @@ const teacherCtrl = {
}

if (!course.createdBy.equals(req.user._id)) {
fs.unlinkSync(noteFilePath);
fs.unlinkSync(videoFilePath);
return next(new ErrorHandler(400, "You are not the creater of course"));
}
if (course.isPublished) {
fs.unlinkSync(noteFilePath);
fs.unlinkSync(videoFilePath);
return next(new ErrorHandler(400, "Course is already published"));
}

const notesfile = req.files.notes;
const videofile = req.files.video;

noteFilePath = "public/course_notes" + "/" + notesfile[0].filename;
videoFilePath = "public/course_videos" + "/" + videofile[0].filename;

course.notes.push(noteFilePath);
// course.notes.push(noteFilePath);

//Video Conversion using worker threads
const conversionPromise = resolutions.map((resolution) => {
Expand Down Expand Up @@ -234,7 +251,10 @@ const teacherCtrl = {
videoDuration: du,
});
video = await video.save();
course.videos.push(video._id);
course.videos.push({
video: video._id,
note: noteFilePath,
});

course = await course.save();

Expand Down Expand Up @@ -283,7 +303,7 @@ const teacherCtrl = {
course.isPublished = true;
course.price = price;
course.duration = duration;
course.preview=courses.videos[0];
course.preview = course.videos[0].video;
await course.save();

let existingCategory = await Category.findOne({ name: category });
Expand All @@ -292,10 +312,10 @@ const teacherCtrl = {
name: category,
courses: [courseId],
});
newCategory.save();
await newCategory.save();
} else {
existingCategory.courses.push(course.Id);
existingCategory.save();
existingCategory.courses.push(course._id);
await existingCategory.save();
}
res.json({
success: true,
Expand Down Expand Up @@ -348,7 +368,7 @@ const teacherCtrl = {
if (!req.file) {
return next(new ErrorHandler(400, "Please upload a file"));
}
fs.unlinkSync(course.thumbnail);
fs.unlinkSync("public/thumbnail" + "/" + req.file.filename);
course.thumbnail = "thumbnail" + "/" + req.file.filename;
// await Course.findByIdAndUpdate(courseId, {
// thumbnail: "public/thumbnail" + "/" + req.file.filename,
Expand All @@ -359,6 +379,7 @@ const teacherCtrl = {
message: "Thumbnail changed successfully",
});
} catch (e) {
fs.unlinkSync("public/thumbnail" + "/" + req.file.filename);
next(e);
}
},
Expand All @@ -376,23 +397,34 @@ const teacherCtrl = {
new ErrorHandler(400, "You are not the creater of the course")
);
}

const category = await Category.findOne({ name: course.category });
const catCourseIndex = category.courses.indexOf(courseId);
if (catCourseIndex != -1) {
category.courses.splice(catCourseIndex, 1);
await category.save();
}

const courseIndex = req.user.createdCourse.indexOf(courseId);
req.user.createdCourse.splice(courseIndex, 1);
await req.user.save();
fs.unlinkSync(course.thumbnail);
fs.unlinkSync("public" + "/" + course.thumbnail);
for (const videoId of course.videos) {
const video = await Video.findById(videoId);
const video = await Video.findById(videoId.video);

if (video) {
await Video.findByIdAndDelete(videoId);
await Video.findByIdAndDelete(videoId.video);
fs.unlinkSync(video.videoUrl);
fs.unlinkSync(video.videoUrl_144p);
fs.unlinkSync(video.videoUrl_360p);
fs.unlinkSync(video.videoUrl_720p);
}
}
for (const note of course.notes) {
fs.unlinkSync(note);
for (const notes of course.videos) {
if (notes.note == null) {
continue;
}
fs.unlinkSync(notes.note);
}
await Course.findByIdAndDelete(courseId);
res.json({
Expand All @@ -410,16 +442,18 @@ const teacherCtrl = {

const result = await videoSchema.validateAsync(req.body);
const videoTitle = result.videoTitle;
videoFilePath = "public/course_videos" + "/" + req.file.filename;
let course = await Course.findById(courseId);
if (!course) {
fs.unlinkSync(videoFilePath);
return next(new ErrorHandler(404, "Course not found"));
}
if (!course.createdBy.equals(req.user._id)) {
fs.unlinkSync(videoFilePath);
return next(
new ErrorHandler(400, "You are not the creater of the course")
);
}
videoFilePath = "public/course_videos" + "/" + req.file.filename;
const conversionPromise = resolutions.map((resolution) => {
inputFilePath = videoFilePath;
inputFileName = path.basename(
Expand Down Expand Up @@ -456,6 +490,9 @@ const teacherCtrl = {
message: "Lecture added successfully",
});
} catch (e) {
if (videoFilePath) {
fs.unlinkSync(videoFilePath);
}
next(e);
}
},
Expand Down
4 changes: 2 additions & 2 deletions controllers/user_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const userCtrl = {
},
});
} catch (error) {
fs.unlinksync("public/thumbnail" + "/" + req.file.filename);
next(error);
}
},
Expand Down Expand Up @@ -342,15 +343,14 @@ const userCtrl = {
});
if (!user) {
return next(new ErrorHandler(404, "No user found"));

}
res.json({
success: true,
message: "user found",
data: {
user,
},
})
});
} catch (e) {
next(e);
}
Expand Down
25 changes: 14 additions & 11 deletions models/course.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ const courseSchema = new mongoose.Schema(

videos: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Video",
},
],
notes: [
{
type: String,
video: {
type: mongoose.Schema.Types.ObjectId,
ref: "Video",
},
note: {
type: String,
},
},
],


category: {
type: String,
Expand Down Expand Up @@ -62,10 +63,12 @@ const courseSchema = new mongoose.Schema(
type: Number,
default: 0,
},
preview: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Video",
}],
preview: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Video",
},
],
isPublished: {
type: Boolean,
default: false,
Expand Down
Binary file removed public/course_notes/notes-1700111331571.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700111347410.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700111776357.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700111779975.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700112240474.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700112246899.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700112374400.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700112381985.pdf
Binary file not shown.
File renamed without changes.
Binary file removed public/course_videos/video-1700111347381-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111347381-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111347381-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111347381.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111776327-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111776327-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111776327-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111776327.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111779942-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111779942-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111779942-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111779942.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112240436-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112240436-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112240436-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112240436.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112246857-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112246857-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112246857-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112246857.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112374357-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112374357-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112374357-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112374357.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112381936-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112381936-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112381936-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112381936.mp4
Binary file not shown.
Binary file removed public/thumbnail/1699903999908.png
Binary file not shown.
Binary file removed public/thumbnail/1699904006114.png
Binary file not shown.
Binary file removed public/thumbnail/1699904068553.png
Binary file not shown.
Binary file removed public/thumbnail/1699904098595.png
Binary file not shown.
Binary file removed public/thumbnail/1699904125867.png
Binary file not shown.
Binary file removed public/thumbnail/1699964145332.jpg
Binary file not shown.
Binary file removed public/thumbnail/1699964184412.jpg
Binary file not shown.
Binary file removed public/thumbnail/1699966011371.png
Binary file not shown.
Binary file removed public/thumbnail/1699966015776.png
Binary file not shown.
Binary file removed public/thumbnail/1699966026546.png
Binary file not shown.
Binary file removed public/thumbnail/1700111313385.png
Binary file not shown.
Binary file removed public/thumbnail/1700112188957.png
Binary file not shown.
Binary file removed public/thumbnail/1700112196894.png
Binary file not shown.
Binary file removed public/thumbnail/1700112361069.png
Binary file not shown.
Binary file removed public/thumbnail/1700300919360.jpg
Binary file not shown.
File renamed without changes
File renamed without changes

0 comments on commit 6ff615b

Please sign in to comment.