Skip to content

Commit

Permalink
feat: Socket image 예시 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
redcarrot1 committed Dec 27, 2023
1 parent 4e54b99 commit 9b6c8c0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/main/java/solution/gdsc/PathPal/MainController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package solution.gdsc.PathPal;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@RestController
public class MainController {

@Value("${image.path}")
private String path;

@GetMapping
public String index() {
return "hello world!";
Expand All @@ -17,4 +27,16 @@ public String index() {
public TestResponse test(@RequestBody TestRequest testRequest) {
return TestResponse.from(testRequest.getName());
}

@GetMapping("/image/{imageId}")
public ResponseEntity<Resource> viewImg(@PathVariable(name = "imageId") Integer imageId) throws IOException {
final String fileFullPath = path + imageId + ".jpeg";
System.out.println(fileFullPath);
Path path = new File(fileFullPath).toPath();

FileSystemResource resource = new FileSystemResource(path);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(Files.probeContentType(path)))
.body(resource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package solution.gdsc.PathPal.global.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer {

@Autowired
private WebSocketHandler webSocketHandler;

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler, "/socket") // Handshake 주소
.setAllowedOrigins("*");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package solution.gdsc.PathPal.global.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.BinaryWebSocketHandler;

import javax.imageio.stream.FileImageOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class WebSocketHandler extends BinaryWebSocketHandler {

@Value("${image.path}")
private String path;

private final Set<WebSocketSession> sessions = ConcurrentHashMap.newKeySet();

private final AtomicInteger imageNumber = new AtomicInteger(1);

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
session.setBinaryMessageSizeLimit(1024 * 1024 * 10);
sessions.add(session);
}

@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
byte[] bytes = message.getPayload().array();

String responseMessage = "";
try {
int imageId = imageNumber.get();
String fileFullName = path + imageId + ".jpeg";
System.out.println(fileFullName);

FileImageOutputStream imageOutput = new FileImageOutputStream(new File(fileFullName));
imageOutput.write(bytes, 0, bytes.length);
imageOutput.close();
responseMessage = "성공적으로 저장했습니다. 받은 바이트 크기=" + bytes.length + ", savedImageId=" + imageId;
imageNumber.incrementAndGet();
} catch (Exception e) {
responseMessage = "저장에 실패했습니다. 받은 바이트 크기=" + bytes.length;
}
try {
session.sendMessage(new TextMessage(responseMessage));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
sessions.remove(session);
}
}

0 comments on commit 9b6c8c0

Please sign in to comment.