video.controller.ts217 lines, 6.3 KB
| 1 | import { NextFunction, Request, Response } from "express"; |
| 2 | import { VideoService } from "../services/video.service"; |
| 3 | import { successResponse } from "../utils/response"; |
| 4 | import { AuthService } from "../services/auth.service"; |
| 5 | import { JobsService } from "../services/jobs.service"; |
| 6 | import { StatusCodes } from "http-status-codes/build/cjs"; |
| 7 | import { AppError } from "../utils/errors"; |
| 8 | import { SubscriptionService } from "../services/subscription.service"; |
| 9 | |
| 10 | export class VideoController { |
| 11 | static async getVideoInfo(req: Request, res: Response, next: NextFunction) { |
| 12 | try { |
| 13 | const { url } = req.body; |
| 14 | const videoInfo = await VideoService.getVideoInfo(url); |
| 15 | |
| 16 | res.json(successResponse(videoInfo)); |
| 17 | } catch (error) { |
| 18 | next(error); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | static async downloadAudio(req: Request, res: Response, next: NextFunction) { |
| 23 | try { |
| 24 | const { url } = req.body; |
| 25 | const videoInfo = await VideoService.getVideoInfo(url); |
| 26 | const audioPath = await VideoService.downloadAudio(url); |
| 27 | |
| 28 | res.json( |
| 29 | successResponse({ |
| 30 | ...videoInfo, |
| 31 | audioPath, |
| 32 | message: "Audio downloaded successfully", |
| 33 | }) |
| 34 | ); |
| 35 | } catch (error) { |
| 36 | next(error); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static async transcribeVideo( |
| 41 | req: Request, |
| 42 | res: Response, |
| 43 | next: NextFunction |
| 44 | ) { |
| 45 | try { |
| 46 | const { url } = req.body; |
| 47 | const userId = req.user?.userId; |
| 48 | |
| 49 | const user = await AuthService.getUserById(userId!); |
| 50 | const videoInfo = await VideoService.getVideoInfo(url); |
| 51 | |
| 52 | // Check subscription limits before processing |
| 53 | |
| 54 | const durationInSeconds = videoInfo.duration; |
| 55 | await SubscriptionService.checkSubscriptionLimits( |
| 56 | userId!, |
| 57 | durationInSeconds |
| 58 | ); |
| 59 | |
| 60 | // create a background job to transcribe the video |
| 61 | const { jobId } = await JobsService.addTranscriptionJob( |
| 62 | url, |
| 63 | videoInfo, |
| 64 | user |
| 65 | ); |
| 66 | |
| 67 | // Increment usage after successful job creation |
| 68 | |
| 69 | await SubscriptionService.incrementUsage(userId!, durationInSeconds); |
| 70 | |
| 71 | res.json( |
| 72 | successResponse({ |
| 73 | jobId, |
| 74 | videoInfo, |
| 75 | message: "Transcription job created successfully", |
| 76 | }) |
| 77 | ); |
| 78 | } catch (error) { |
| 79 | next(error); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static async getTranscriptionStatus( |
| 84 | req: Request, |
| 85 | res: Response, |
| 86 | next: NextFunction |
| 87 | ) { |
| 88 | try { |
| 89 | const { jobId } = req.params; |
| 90 | const status = await JobsService.getJobStatus(jobId); |
| 91 | res.json(successResponse(status)); |
| 92 | } catch (error) { |
| 93 | next(error); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static async getUserVideos(req: Request, res: Response, next: NextFunction) { |
| 98 | try { |
| 99 | const userId = req.user?.userId; |
| 100 | const videos = await VideoService.getUserVideos(userId!); |
| 101 | |
| 102 | const transformedVideos = videos.map((video) => ({ |
| 103 | id: video.id, |
| 104 | url: video.url, |
| 105 | title: video.title, |
| 106 | description: video.description, |
| 107 | duration: video.duration, |
| 108 | author: video.author, |
| 109 | thumbnail: video.thumbnail, |
| 110 | status: video.status, |
| 111 | createdAt: video.createdAt, |
| 112 | updatedAt: video.updatedAt, |
| 113 | transcription: video.transcription |
| 114 | ? { |
| 115 | text: video.transcription.text, |
| 116 | confidence: video.transcription.confidence, |
| 117 | isMusic: video.transcription.isMusic, |
| 118 | createdAt: video.transcription.createdAt, |
| 119 | } |
| 120 | : null, |
| 121 | analysis: video.analysis |
| 122 | ? { |
| 123 | summary: video.analysis.summary, |
| 124 | keyPoints: video.analysis.keyPoints, |
| 125 | sentiment: video.analysis.sentiment, |
| 126 | topics: video.analysis.topics, |
| 127 | suggestedTags: video.analysis.suggestedTags, |
| 128 | chapters: video.analysis.chapters, |
| 129 | createdAt: video.analysis.createdAt, |
| 130 | } |
| 131 | : null, |
| 132 | })); |
| 133 | |
| 134 | res.json(successResponse(transformedVideos)); |
| 135 | } catch (error) { |
| 136 | next(error); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static async getVideoById(req: Request, res: Response, next: NextFunction) { |
| 141 | try { |
| 142 | const { id } = req.params; |
| 143 | const userId = req.user?.userId; |
| 144 | |
| 145 | const video = await VideoService.getVideoById(id, userId!); |
| 146 | if (!video) { |
| 147 | throw new AppError(StatusCodes.NOT_FOUND, "Video not found"); |
| 148 | } |
| 149 | |
| 150 | // transoform the response to include only necessary fields |
| 151 | const transformedVideo = { |
| 152 | id: video.id, |
| 153 | url: video.url, |
| 154 | title: video.title, |
| 155 | description: video.description, |
| 156 | duration: video.duration, |
| 157 | author: video.author, |
| 158 | thumbnail: video.thumbnail, |
| 159 | status: video.status, |
| 160 | createdAt: video.createdAt, |
| 161 | updatedAt: video.updatedAt, |
| 162 | transcription: video.transcription |
| 163 | ? { |
| 164 | text: video.transcription.text, |
| 165 | confidence: video.transcription.confidence, |
| 166 | isMusic: video.transcription.isMusic, |
| 167 | createdAt: video.transcription.createdAt, |
| 168 | } |
| 169 | : null, |
| 170 | analysis: video.analysis |
| 171 | ? { |
| 172 | summary: video.analysis.summary, |
| 173 | keyPoints: video.analysis.keyPoints, |
| 174 | sentiment: video.analysis.sentiment, |
| 175 | topics: video.analysis.topics, |
| 176 | suggestedTags: video.analysis.suggestedTags, |
| 177 | chapters: video.analysis.chapters, |
| 178 | createdAt: video.analysis.createdAt, |
| 179 | } |
| 180 | : null, |
| 181 | }; |
| 182 | |
| 183 | res.json(successResponse(transformedVideo)); |
| 184 | } catch (error) { |
| 185 | next(error); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static async getAllJobs(req: Request, res: Response, next: NextFunction) { |
| 190 | try { |
| 191 | const userId = req.user?.userId; |
| 192 | const jobs = await JobsService.getAllJobs(userId!); |
| 193 | res.json(successResponse(jobs)); |
| 194 | } catch (error) { |
| 195 | next(error); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static async cancelJob(req: Request, res: Response, next: NextFunction) { |
| 200 | try { |
| 201 | const { jobId } = req.params; |
| 202 | // Optional: Add check if req.user.userId matches job.data.userId for authorization |
| 203 | const success = await JobsService.cancelJob(jobId); |
| 204 | if (success) { |
| 205 | res.json(successResponse({ message: "Job cancelled successfully" })); |
| 206 | } else { |
| 207 | // This case might not be reachable if service throws errors |
| 208 | throw new AppError( |
| 209 | StatusCodes.INTERNAL_SERVER_ERROR, |
| 210 | "Failed to cancel job" |
| 211 | ); |
| 212 | } |
| 213 | } catch (error) { |
| 214 | next(error); |
| 215 | } |
| 216 | } |
| 217 | } |