Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main <- develop #97

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,7 @@ void processMemoTag(AiCreateStructureResponse aiCreateStructureResponse, String
}

@Async("AsyncMemoTagExecutor")
public void updateEmbeddingAndMetadata(
Memo memo,
String content,
List<String> imageUrls,
List<String> voiceUrls,
String userId
) {
public void updateEmbeddingAndMetadata(Memo memo, String content, List<String> imageUrls, List<String> voiceUrls) {
AiCreateEmbeddingResponse aiCreateEmbeddingResponse = null;

boolean isContentChanged = !content.equals(memo.getContent());
Expand All @@ -161,9 +155,12 @@ public void updateEmbeddingAndMetadata(
? aiCreateMetadataResponse.embeddingMetadata() : memo.getEmbeddingMetadata();

memo.update(
content,
imageUrls,
voiceUrls,
metadata,
embedding,
embeddingMetadata,
metadata
embeddingMetadata
);
memoService.updateMemo(memo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public UpdateMemoResponse updateMemo(String memoId, UpdateMemoRequest request, S
Memo memo = memoService.getMemo(memoId, userId);

processDeletedFiles(memo, imageUrls, voiceUrls, userId);
asyncMemoTagService.updateEmbeddingAndMetadata(memo, content, imageUrls, voiceUrls, userId);
asyncMemoTagService.updateEmbeddingAndMetadata(memo, content, imageUrls, voiceUrls);

memo.update(
content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,23 @@ public void update(
}

public void update(
String updatedContent,
List<String> updatedImageUrls,
List<String> updatedVoiceUrls
String content,
List<String> imageUrls,
List<String> voiceUrls
) {
this.content = updatedContent;
this.imageUrls = updatedImageUrls;
this.voiceUrls = updatedVoiceUrls;
this.content = content;
this.imageUrls = imageUrls;
this.voiceUrls = voiceUrls;
this.updatedAt = LocalDateTime.now();
}

public void update(
String content,
List<String> imageUrls,
List<String> voiceUrls,
String metadata,
List<Double> embedding,
List<Double> embeddingMetadata,
String metadata
List<Double> embeddingMetadata
) {
this.metadata = metadata;
this.embedding = embedding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AllowedFileTypeValidator implements ConstraintValidator<AllowedFileType, Object> {

private Set<AllowedFileTypeEnum> allowedTypes;
private Set<String> allowedExtensions;

@Override
public void initialize(AllowedFileType constraintAnnotation) {
allowedTypes = Arrays.stream(constraintAnnotation.value())
allowedExtensions = Arrays.stream(constraintAnnotation.value())
.flatMap(type -> type.getExtensions().stream())
.collect(Collectors.toSet());
}

Expand All @@ -42,8 +41,7 @@ public boolean isValid(Object value, ConstraintValidatorContext context) {

private boolean isValidFile(MultipartFile file) {
String fileExtension = getFileExtension(file.getOriginalFilename());
String mimeType = file.getContentType();
return !file.isEmpty() && isValidExtension(fileExtension) && isValidMimeType(fileExtension, mimeType);
return !file.isEmpty() && isValidExtension(fileExtension);
}

private boolean isValidList(List<?> list) {
Expand All @@ -52,23 +50,13 @@ private boolean isValidList(List<?> list) {
}

return list.stream().allMatch(item ->
(item instanceof String && isValidExtension(getFileExtension((String)item))) ||
(item instanceof MultipartFile && isValidFile((MultipartFile)item))
(item instanceof String && isValidExtension(getFileExtension((String) item))) ||
(item instanceof MultipartFile && isValidFile((MultipartFile) item))
);
}

private boolean isValidExtension(String fileExtension) {
return fileExtension != null && allowedTypes.stream()
.anyMatch(type -> type.getFileTypes().containsKey(fileExtension));
}

private boolean isValidMimeType(String fileExtension, String mimeType) {
return fileExtension != null && mimeType != null && allowedTypes.stream()
.anyMatch(type -> {
List<String> allowedMimeTypes = type.getFileTypes().get(fileExtension);
return allowedMimeTypes != null && allowedMimeTypes.stream()
.anyMatch(mimeType::equalsIgnoreCase);
});
return fileExtension != null && allowedExtensions.contains(fileExtension.toLowerCase());
}

private String getFileExtension(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
package com.example.oatnote.web.validation.enums;

import java.util.List;
import java.util.Map;
import java.util.Set;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum AllowedFileTypeEnum {
IMAGE(Map.of(
"jpg", List.of("image/jpeg"),
"jpeg", List.of("image/jpeg"),
"png", List.of("image/png"),
"webp", List.of("image/webp"),
"gif", List.of("image/gif")
)),
VOICE(Map.of(
"mp3", List.of("audio/mpeg"),
"mpeg", List.of("audio/mpeg"),
"mpga", List.of("audio/mpeg"),
"m4a", List.of("audio/m4a"),
"wav", List.of("audio/wav"),
"webm", List.of("audio/webm", "video/webm"),
"mp4", List.of("video/mp4"),
"mkv", List.of("video/x-matroska")
)),
TXT(Map.of(
"txt", List.of("text/plain")
)),
CSV(Map.of(
"csv", List.of("text/csv")
));
IMAGE(Set.of("jpg", "jpeg", "png", "webp", "gif")),
VOICE(Set.of("mp3", "mpeg", "mpga", "m4a", "wav", "webm")),
TXT(Set.of("txt")),
CSV(Set.of("csv"));

private final Map<String, List<String>> fileTypes;
private final Set<String> extensions;
}
Loading