add RequestRange
This commit is contained in:
@@ -7,6 +7,7 @@ import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.bean.timi.TimiResponse;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
import com.imyeyu.spring.bean.RequestRange;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@@ -16,6 +17,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Locale;
|
||||
@@ -117,6 +119,12 @@ public class TimiSpring {
|
||||
return getRequest().getRequestURI();
|
||||
}
|
||||
|
||||
public static String cutURIStartAt(String flag) {
|
||||
int indexOf = getURI().indexOf(flag);
|
||||
TimiException.requiredTrue(-1 < indexOf, "not found flag: %s".formatted(flag));
|
||||
return getURI().substring(indexOf + flag.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 HttpServlet 回调
|
||||
*
|
||||
@@ -357,4 +365,26 @@ public class TimiSpring {
|
||||
public static boolean isLocalIP() {
|
||||
return getRequestIP().startsWith("127");
|
||||
}
|
||||
|
||||
public static RequestRange requestRange(long fileLength) throws IOException {
|
||||
HttpServletResponse resp = getResponse();
|
||||
|
||||
String range = getRequestAttrAsString("Range");
|
||||
if (range == null || !range.startsWith("bytes=")) {
|
||||
return null;
|
||||
}
|
||||
// 处理 bytes=0-999 格式
|
||||
String rangeValue = range.substring("bytes=".length());
|
||||
String[] ranges = rangeValue.split("-");
|
||||
TimiException.requiredTrue(2 == ranges.length, "Invalid Range format");
|
||||
long start = Long.parseLong(ranges[0]);
|
||||
long end = ranges[1].isEmpty() ? fileLength - 1 : Long.parseLong(ranges[1]);
|
||||
// 验证范围有效性
|
||||
if (start < 0 || fileLength <= end || end < start) {
|
||||
resp.setHeader("Content-Range", "bytes */" + fileLength);
|
||||
resp.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
|
||||
return null;
|
||||
}
|
||||
return new RequestRange(start, end);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user