Skip to content

Commit 6c783a7

Browse files
committed
Change video response to Resource (instead of byte array)
1 parent ef9c220 commit 6c783a7

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

backend/src/main/java/urjc/ovteaching/openvidu/OpenViduComponent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
import org.apache.http.util.EntityUtils;
3636
import org.springframework.beans.factory.annotation.Autowired;
3737
import org.springframework.beans.factory.annotation.Value;
38+
import org.springframework.core.io.Resource;
3839
import org.springframework.core.io.ResourceLoader;
3940
import org.springframework.http.HttpStatus;
4041
import org.springframework.stereotype.Component;
4142
import org.springframework.web.client.HttpClientErrorException;
4243

43-
import com.google.common.io.ByteStreams;
4444
import com.google.gson.JsonArray;
4545
import com.google.gson.JsonObject;
4646

@@ -326,9 +326,9 @@ public List<Recording> getRecordings(Room room) {
326326
}
327327
}
328328

329-
public byte[] getVideo(String videoId) throws IOException {
329+
public Resource getVideo(String videoId) throws IOException {
330330
if(RECORDING_ENABLED) {
331-
return ByteStreams.toByteArray(resourceLoader.getResource("file:" + RECORDING_PATH + "/" + videoId + "/" + videoId + ".mp4").getInputStream());
331+
return resourceLoader.getResource("file:" + RECORDING_PATH + "/" + videoId + "/" + videoId + ".mp4");
332332
}
333333
return null;
334334
}

backend/src/main/java/urjc/ovteaching/openvidu/RecordingController.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import java.util.List;
55

66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.core.io.Resource;
78
import org.springframework.http.HttpHeaders;
89
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.MediaType;
911
import org.springframework.http.ResponseEntity;
1012
import org.springframework.web.bind.annotation.CrossOrigin;
1113
import org.springframework.web.bind.annotation.GetMapping;
@@ -164,7 +166,7 @@ public ResponseEntity<List<Recording>> getRecordings(@PathVariable String roomNa
164166
* @return the video in byte array
165167
*/
166168
@GetMapping("/room/{roomName}/recording/{video}")
167-
public ResponseEntity<byte[]> getVideo(@PathVariable String roomName, @PathVariable String video) {
169+
public ResponseEntity<Resource> getVideo(@PathVariable String roomName, @PathVariable String video) {
168170
Room room = roomServ.findByName(roomName);
169171
User user = userServ.findByName(this.userComponent.getLoggedUser().getName());
170172

@@ -178,8 +180,11 @@ public ResponseEntity<byte[]> getVideo(@PathVariable String roomName, @PathVaria
178180
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
179181
}
180182
try {
181-
byte[] bytes = this.openViduComponent.getVideo(video);
182-
return new ResponseEntity<>(bytes, new HttpHeaders(), HttpStatus.OK);
183+
Resource resource = this.openViduComponent.getVideo(video);
184+
return ResponseEntity.ok()
185+
.contentType(MediaType.parseMediaType("video/mp4"))
186+
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
187+
.body(resource);
183188
} catch(IOException e) {
184189
e.printStackTrace();
185190
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

backend/src/test/java/urjc/ovteaching/RecordingTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
2121
import org.springframework.boot.test.context.SpringBootTest;
2222
import org.springframework.boot.test.mock.mockito.MockBean;
23+
import org.springframework.core.io.ResourceLoader;
2324
import org.springframework.http.MediaType;
2425
import org.springframework.security.test.context.support.WithMockUser;
2526
import org.springframework.test.context.junit4.SpringRunner;
2627
import org.springframework.test.web.servlet.MockMvc;
2728
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
2829

30+
import com.google.common.io.ByteStreams;
31+
2932
import urjc.ovteaching.openvidu.OpenViduComponent;
3033
import urjc.ovteaching.openvidu.RecordingController;
3134
import urjc.ovteaching.rooms.Room;
@@ -42,6 +45,9 @@ public class RecordingTests {
4245

4346
@Autowired
4447
private MockMvc mvc;
48+
49+
@Autowired
50+
ResourceLoader resourceLoader;
4551

4652
@Spy
4753
private final RecordingController recordingController = new RecordingController();
@@ -366,12 +372,13 @@ public void getRecordingsOpenViduError() throws Exception {
366372

367373
@Test
368374
public void getVideo() throws Exception {
369-
given(this.openviduComponent.getVideo("testRoom")).willReturn(new byte[] {1, 2, 3});
375+
given(this.openviduComponent.getVideo("testRoom")).willReturn(resourceLoader.getResource("classpath:json/initialData.json"));
370376

371377
mvc.perform(MockMvcRequestBuilders.get("/ovTeachingApi/room/testRoom/recording/testRoom")
372378
.contentType(MediaType.APPLICATION_JSON))
373379
.andExpect(status().isOk())
374-
.andExpect(content().bytes(new byte[] {1, 2, 3}));
380+
.andExpect(content().contentType("video/mp4"))
381+
.andExpect(content().bytes(ByteStreams.toByteArray(resourceLoader.getResource("classpath:json/initialData.json").getInputStream())));
375382

376383
verify(openviduComponent).getVideo("testRoom");
377384
}

0 commit comments

Comments
 (0)