Skip to content

Commit 0a6b19f

Browse files
committed
refactor : 파일 오류 수정
클래스로 파일 직렬화 역직렬화를 했는데, 오류가 생겼다. 권한 부족으로 인해서 베이스 코드로 수정
1 parent 63be6f4 commit 0a6b19f

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/controller/message/MessageController.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ public ResponseEntity<List<Message>> findByAllChannel(
3737
}
3838

3939
@RequestMapping(
40-
method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE,
41-
MediaType.MULTIPART_FORM_DATA_VALUE}
40+
method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}
4241
)
4342
public ResponseEntity<Void> createMessage(
4443
@RequestPart(name = "messageCreateRequest") MessageCreateRequest messageCreateRequest,
45-
@RequestPart(required = false, name = "binaryContentCreateRequest") List<MultipartFile> binaryContentCreateRequest
44+
@RequestPart(name = "binary-content-create-request", required = false) List<MultipartFile> binaryContentCreateRequest
4645
) {
4746
List<BinaryContentCreateRequest> binaryContents = binaryContentCreateRequest.stream()
4847
.map(file -> {

codeit-bootcamp-spring/1-sprint-mission/src/main/java/com/sprint/mission/discodeit/repository/file/FileBinaryContentRepository.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ private Path resolvePath(UUID id) {
4646
@Override
4747
public BinaryContent save(BinaryContent binaryContent) {
4848
Path path = resolvePath(binaryContent.getId());
49-
SERIALIZATION.serialize(DIRECTORY, binaryContent);
49+
try (
50+
FileOutputStream fos = new FileOutputStream(path.toFile());
51+
ObjectOutputStream oos = new ObjectOutputStream(fos)
52+
) {
53+
oos.writeObject(binaryContent);
54+
} catch (IOException ioException) {
55+
throw new RuntimeException(ioException);
56+
}
5057
return binaryContent;
5158
}
5259

@@ -55,7 +62,14 @@ public Optional<BinaryContent> findById(UUID id) {
5562
BinaryContent binaryContentNullable = null;
5663
Path path = resolvePath(id);
5764
if (Files.exists(path)) {
58-
binaryContentNullable = SERIALIZATION.deserialize(path);
65+
try (
66+
FileInputStream fis = new FileInputStream(path.toFile());
67+
ObjectInputStream ois = new ObjectInputStream(fis)
68+
) {
69+
binaryContentNullable = (BinaryContent) ois.readObject();
70+
} catch (IOException | ClassNotFoundException exception) {
71+
throw new RuntimeException(exception);
72+
}
5973
}
6074
return Optional.ofNullable(binaryContentNullable);
6175
}
@@ -65,7 +79,16 @@ public List<BinaryContent> findAllByIdIn(List<UUID> ids) {
6579
try (Stream<Path> paths = Files.list(DIRECTORY)) {
6680
return paths
6781
.filter(path -> path.toString().endsWith(EXTENSION))
68-
.map(SERIALIZATION::deserialize)
82+
.map(path -> {
83+
try (
84+
FileInputStream fis = new FileInputStream(path.toFile());
85+
ObjectInputStream ois = new ObjectInputStream(fis)
86+
) {
87+
return (BinaryContent) ois.readObject();
88+
} catch (IOException | ClassNotFoundException e) {
89+
throw new RuntimeException(e);
90+
}
91+
})
6992
.filter(content -> ids.contains(content.getId()))
7093
.toList();
7194
} catch (IOException e) {

0 commit comments

Comments
 (0)