11package com .programming .streaming .controller ;
22
3- import com .programming . streaming . model .Video ;
3+ import com .mongodb . client . gridfs . model .GridFSFile ;
44import com .programming .streaming .service .VideoService ;
5-
6- import lombok .AllArgsConstructor ;
7-
5+ import com .programming .streaming .service .VideoService .VideoWithStream ;
86import org .springframework .beans .factory .annotation .Autowired ;
9- import org .springframework .core .io .ByteArrayResource ;
7+ import org .springframework .core .io .InputStreamResource ;
8+ import org .springframework .http .ContentDisposition ;
109import org .springframework .http .HttpHeaders ;
1110import org .springframework .http .HttpStatus ;
1211import org .springframework .http .MediaType ;
1312import org .springframework .http .ResponseEntity ;
1413import org .springframework .web .bind .annotation .*;
1514import org .springframework .web .multipart .MultipartFile ;
15+ import org .springframework .web .util .UriUtils ;
1616
1717import java .io .IOException ;
18+ import java .io .InputStream ;
19+ import java .nio .charset .StandardCharsets ;
1820import java .sql .Timestamp ;
19- import org . springframework . web . bind . annotation . PutMapping ;
20- import org . springframework . web . bind . annotation . PathVariable ;
21+ import java . util . Map ;
22+
2123@ RestController
2224@ RequestMapping ("/video" )
23- @ AllArgsConstructor
2425public class VideoController {
2526
26- @ GetMapping ("/" )
27- public String getVideo () {
28- return "Video service" ;
29- }
30-
31-
3227 @ Autowired
3328 private VideoService videoService ;
3429
30+ @ CrossOrigin (origins = "*" )
3531 @ PostMapping ("/upload" )
3632 public ResponseEntity <?> upload (@ RequestParam ("file" ) MultipartFile file ,
3733 @ RequestParam ("userID" ) String userID ,
34+ @ RequestParam ("description" ) String description ,
35+ @ RequestParam ("userName" ) String userName ,
36+ @ RequestParam ("videoName" ) String videoName ,
3837 @ RequestParam ("thumbnail" ) MultipartFile thumbnailFile ) throws IOException {
3938 byte [] thumbnail = thumbnailFile .getBytes ();
4039 Timestamp timestamp = new Timestamp (System .currentTimeMillis ());
41- return new ResponseEntity <>(videoService .addVideo (file , userID , thumbnail , timestamp ), HttpStatus .OK );
40+ return new ResponseEntity <>(videoService .addVideo (file , userID , thumbnail , timestamp , description , userName , videoName ), HttpStatus .OK );
4241 }
4342
43+
44+ @ CrossOrigin (origins = "*" )
4445 @ GetMapping ("/get/{id}" )
45- public ResponseEntity <ByteArrayResource > download (@ PathVariable String id ) throws IOException {
46- Video loadFile = videoService .getVideo (id );
46+ public ResponseEntity <?> download (@ PathVariable String id ,
47+ @ RequestHeader (value = HttpHeaders .RANGE , required = false ) String rangeHeader ) throws IOException {
48+ VideoWithStream videoWithStream = videoService .getVideoWithStream (id );
49+ if (videoWithStream == null ) {
50+ return new ResponseEntity <>(HttpStatus .NOT_FOUND );
51+ }
52+
53+ GridFSFile gridFSFile = videoWithStream .getGridFSFile ();
54+ InputStream inputStream = videoWithStream .getInputStream ();
55+
56+ long fileSize = gridFSFile .getLength ();
57+ HttpHeaders headers = new HttpHeaders ();
58+ headers .setContentType (MediaType .parseMediaType (gridFSFile .getMetadata ().get ("_contentType" ).toString ()));
59+ headers .setContentDisposition (ContentDisposition .builder ("inline" )
60+ .filename (UriUtils .encodePath (gridFSFile .getFilename (), StandardCharsets .UTF_8 ))
61+ .build ());
62+
63+ if (rangeHeader == null ) {
64+ return ResponseEntity .ok ()
65+ .headers (headers )
66+ .contentLength (fileSize )
67+ .body (new InputStreamResource (inputStream ));
68+ }
69+
70+ String [] ranges = rangeHeader .replace ("bytes=" , "" ).split ("-" );
71+ long rangeStart = Long .parseLong (ranges [0 ]);
72+ long rangeEnd = ranges .length > 1 ? Long .parseLong (ranges [1 ]) : fileSize - 1 ;
73+ if (rangeEnd > fileSize - 1 ) {
74+ rangeEnd = fileSize - 1 ;
75+ }
76+ long rangeLength = rangeEnd - rangeStart + 1 ;
77+
78+ inputStream .skip (rangeStart );
79+ InputStreamResource inputStreamResource = new InputStreamResource (
80+ new LimitedInputStream (inputStream , rangeLength ));
81+
82+ headers .add ("Content-Range" , "bytes " + rangeStart + "-" + rangeEnd + "/" + fileSize );
83+ headers .setContentLength (rangeLength );
84+
85+ return ResponseEntity .status (HttpStatus .PARTIAL_CONTENT )
86+ .headers (headers )
87+ .body (inputStreamResource );
88+ }
89+
90+ @ CrossOrigin (origins = "*" )
91+ @ GetMapping ("/getDetails/{videoId}" )
92+ public Map <String , Object > getVideoDetails (@ PathVariable String videoId ) {
93+ return videoService .getDetails (videoId );
94+ }
4795
48- return ResponseEntity . ok ( )
49- . contentType ( MediaType . parseMediaType ( loadFile . getFileType ()) )
50- . header ( HttpHeaders . CONTENT_DISPOSITION , "attachment; filename= \" " + loadFile . getFilename () + " \" " )
51- . body ( new ByteArrayResource ( loadFile . getFile ()) );
96+ @ CrossOrigin ( origins = "*" )
97+ @ GetMapping ( "/getDetailsByUserId/{userId}" )
98+ public ResponseEntity <?> getVideoDetailsByUserId ( @ PathVariable String userId ) {
99+ return new ResponseEntity <>( videoService . getDetailsByUserId ( userId ), HttpStatus . OK );
52100 }
53101
102+ @ CrossOrigin (origins = "*" )
54103 @ GetMapping ("/getAllIds" )
55104 public ResponseEntity <?> getAllID () {
56105 return new ResponseEntity <>(videoService .getAllVideoIDs (), HttpStatus .OK );
57106 }
58107
108+ @ CrossOrigin (origins = "*" )
59109 @ GetMapping ("/listIdThumbnail" )
60110 public ResponseEntity <?> listIdThumbnail () {
61111 return new ResponseEntity <>(videoService .listIdThumbnail (), HttpStatus .OK );
62112 }
63113
114+ @ CrossOrigin (origins = "*" )
115+ @ GetMapping ("/getThumbnailIdByUserId/{id}" )
116+ public ResponseEntity <?> getThumbnailIdByUserId (@ PathVariable String id ) {
117+ return new ResponseEntity <>(videoService .getThumbnailIdByUserId (id ), HttpStatus .OK );
118+ }
119+
120+ @ CrossOrigin (origins = "*" )
64121 @ GetMapping ("/getVideoIdFromThumbnailId/{id}" )
65122 public ResponseEntity <?> getVideoIdFromThumbnailId (@ PathVariable String id ) {
66123 return new ResponseEntity <>(videoService .getVideoIdFromThumbnailId (id ), HttpStatus .OK );
67124 }
68125
126+ @ CrossOrigin (origins = "*" )
69127 @ PutMapping ("/updateViews/{id}" )
70128 public ResponseEntity <?> updateViews (@ PathVariable String id ) {
71129 videoService .updateViews (id );
72130 return new ResponseEntity <>(HttpStatus .OK );
73131 }
74132
133+ @ CrossOrigin (origins = "*" )
75134 @ PutMapping ("/updateLikes/{id}" )
76135 public ResponseEntity <?> updateLikes (@ PathVariable String id ) {
77136 videoService .updateLikes (id );
78137 return new ResponseEntity <>(HttpStatus .OK );
79138 }
80139
140+ @ CrossOrigin (origins = "*" )
81141 @ PutMapping ("/updateDislikes/{id}" )
82142 public ResponseEntity <?> updateDislikes (@ PathVariable String id ) {
83143 videoService .updateDislikes (id );
84144 return new ResponseEntity <>(HttpStatus .OK );
85145 }
86146
147+ private static class LimitedInputStream extends InputStream {
148+ private final InputStream in ;
149+ private long remaining ;
150+
151+ public LimitedInputStream (InputStream in , long limit ) {
152+ this .in = in ;
153+ this .remaining = limit ;
154+ }
155+
156+ @ Override
157+ public int read () throws IOException {
158+ if (remaining <= 0 ) {
159+ return -1 ;
160+ }
161+ int result = in .read ();
162+ if (result != -1 ) {
163+ remaining --;
164+ }
165+ return result ;
166+ }
167+
168+ @ Override
169+ public int read (byte [] b , int off , int len ) throws IOException {
170+ if (remaining <= 0 ) {
171+ return -1 ;
172+ }
173+ int toRead = (int ) Math .min (len , remaining );
174+ int result = in .read (b , off , toRead );
175+ if (result != -1 ) {
176+ remaining -= result ;
177+ }
178+ return result ;
179+ }
180+ }
181+
182+
183+ //Hanlde Subcri
184+ @ CrossOrigin (origins = "*" )
185+ @ PostMapping ("/subscribe" )
186+ public ResponseEntity <?> subscribe (@ RequestParam ("subscriberId" ) String subscriberId ,
187+ @ RequestParam ("subscribedToId" ) String subscribedToId ) {
188+ videoService .subscribe (subscriberId , subscribedToId );
189+ return new ResponseEntity <>(HttpStatus .OK );
190+ }
191+
192+ @ CrossOrigin (origins = "*" )
193+ @ PostMapping ("/unsubscribe" )
194+ public ResponseEntity <?> unsubscribe (@ RequestParam ("subscriberId" ) String subscriberId ,
195+ @ RequestParam ("subscribedToId" ) String subscribedToId ) {
196+ videoService .unsubscribe (subscriberId , subscribedToId );
197+ return new ResponseEntity <>(HttpStatus .OK );
198+ }
199+
200+ @ CrossOrigin (origins = "*" )
201+ @ GetMapping ("/isSubscribed" )
202+ public ResponseEntity <Boolean > isSubscribed (@ RequestParam ("subscriberId" ) String subscriberId ,
203+ @ RequestParam ("subscribedToId" ) String subscribedToId ) {
204+ boolean isSubscribed = videoService .isSubscribed (subscriberId , subscribedToId );
205+ return new ResponseEntity <>(isSubscribed , HttpStatus .OK );
206+ }
207+
208+ @ CrossOrigin (origins = "*" )
209+ @ GetMapping ("/getSubscriberCount" )
210+ public ResponseEntity <Long > getSubscriberCount (@ RequestParam ("userId" ) String userId ) {
211+ long subscriberCount = videoService .getSubscriberCount (userId );
212+ return new ResponseEntity <>(subscriberCount , HttpStatus .OK );
213+ }
214+
215+
216+ //Handle Like
217+ @ CrossOrigin (origins = "*" )
218+ @ PostMapping ("/like" )
219+ public ResponseEntity <?> like (@ RequestParam ("likerToId" ) String likerToId ,
220+ @ RequestParam ("likedToId" ) String likedToId ) {
221+ videoService .like (likerToId , likedToId );
222+ return new ResponseEntity <>(HttpStatus .OK );
223+ }
224+
225+ @ CrossOrigin (origins = "*" )
226+ @ PostMapping ("/unlike" )
227+ public ResponseEntity <?> unlike (@ RequestParam ("likerToId" ) String likerToId ,
228+ @ RequestParam ("likedToId" ) String likedToId ) {
229+ videoService .unlike (likerToId , likedToId );
230+ return new ResponseEntity <>(HttpStatus .OK );
231+ }
232+
233+ @ CrossOrigin (origins = "*" )
234+ @ GetMapping ("/isLiked" )
235+ public ResponseEntity <Boolean > isLiked (@ RequestParam ("likerToId" ) String likerToId ,
236+ @ RequestParam ("likedToId" ) String likedToId ) {
237+ boolean isLiked = videoService .isLiked (likerToId , likedToId );
238+ return new ResponseEntity <>(isLiked , HttpStatus .OK );
239+ }
240+
241+ @ CrossOrigin (origins = "*" )
242+ @ GetMapping ("/getLikeCount" )
243+ public ResponseEntity <Long > getLikeCount (@ RequestParam ("videoId" ) String videoId ) {
244+ long likeCount = videoService .getLikeCount (videoId );
245+ return new ResponseEntity <>(likeCount , HttpStatus .OK );
246+ }
247+
248+
249+ @ CrossOrigin (origins = "*" )
250+ @ GetMapping ("/getIdFromLikerToId/{likerToId}" )
251+ public ResponseEntity <?> getIdFromLikerToId (@ PathVariable String likerToId ) {
252+ return new ResponseEntity <>(videoService .getLikedToIdsFromLikerToId (likerToId ), HttpStatus .OK );
253+ }
87254
88255}
0 commit comments