Skip to content
Open
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 @@ -20,6 +20,9 @@
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializer;
import com.nageoffer.ai.ragent.framework.context.UserContext;
import com.nageoffer.ai.ragent.framework.exception.ClientException;
import lombok.RequiredArgsConstructor;
Expand All @@ -33,6 +36,7 @@
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
Expand All @@ -47,7 +51,18 @@
public final class IdempotentSubmitAspect {

private final RedissonClient redissonClient;
private final Gson gson = new Gson();
private final Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(
MultipartFile.class,
(JsonSerializer<MultipartFile>) (file, type, context) -> {
JsonObject json = new JsonObject();
json.addProperty("name", file.getName());
json.addProperty("originalFilename", file.getOriginalFilename());
json.addProperty("contentType", file.getContentType());
json.addProperty("size", file.getSize());
return json;
})
.create();

@Value("${app.eval.enabled:false}")
private boolean evalEnabled;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.nageoffer.ai.ragent.framework.idempotent;

import org.aspectj.lang.ProceedingJoinPoint;
import org.junit.jupiter.api.Test;
import org.redisson.api.RedissonClient;
import org.springframework.mock.web.MockMultipartFile;

import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class IdempotentSubmitAspectTest {

@Test
void shouldGenerateDigestFromMultipartMetadataWithoutReadingContent() throws Exception {
byte[] firstContent = new byte[20 * 1024 * 1024];
byte[] differentContent = firstContent.clone();
differentContent[differentContent.length - 1] = 1;
MockMultipartFile first = new MockMultipartFile("file", "first.pdf", "application/pdf", firstContent);
MockMultipartFile sameMetadata =
new MockMultipartFile("file", "first.pdf", "application/pdf", differentContent);
MockMultipartFile differentName =
new MockMultipartFile("file", "second.pdf", "application/pdf", firstContent);

assertEquals(calcArgsMd5(first), calcArgsMd5(sameMetadata));
assertNotEquals(calcArgsMd5(first), calcArgsMd5(differentName));
}

private String calcArgsMd5(MockMultipartFile file) throws Exception {
ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
when(joinPoint.getArgs()).thenReturn(new Object[]{file});
IdempotentSubmitAspect aspect = new IdempotentSubmitAspect(mock(RedissonClient.class));
Method method = IdempotentSubmitAspect.class.getDeclaredMethod("calcArgsMD5", ProceedingJoinPoint.class);
method.setAccessible(true);
return (String) method.invoke(aspect, joinPoint);
}
}