按 GID 查找画廊下载文件

This commit is contained in:
root 2026-07-11 15:36:14 +08:00
parent ce21d8724e
commit ad1d96290c

View File

@ -69,33 +69,18 @@ public class MultiThreadedHTTPServer {
} }
else { else {
String filePath = "/root/gallery/gallery"; String filePath = "/root/gallery/gallery";
String path = URLDecoder.decode(requestParts[1].split("\\?")[0], StandardCharsets.UTF_8); String gid = paramMap.get("gid");
if(!path.contains(".")){ file = gid == null ? null : findGalleryZipByGid(new File(filePath), gid);
file = new File("/root/abc");
}else {
String name = path.substring(0, path.lastIndexOf('.'));
name = filePath + name + "/" + name + ".zip";
file = new File(name);
//该文件不存在 //兼容没有gid参数的旧下载链接再尝试按链接中的文件名查找
if(!file.isFile()){ if(file == null){
String gid = paramMap.get("gid"); String path = URLDecoder.decode(requestParts[1].split("\\?")[0], StandardCharsets.UTF_8);
if(path.contains(".")){
//文件不存在的情况下gid也不存在直接404 String name = path.substring(0, path.lastIndexOf('.'));
if(gid == null) name = filePath + name + "/" + name + ".zip";
file = new File("/root/abc"); file = new File(name);
}else{
//gid存在的情况下尝试查找对应的文件 file = new File("/root/abc");
else {
File[] galleryDirectories = (new File(filePath)).listFiles();
assert galleryDirectories != null;
for (File galleryDirectory : galleryDirectories)
if (galleryDirectory.getName().contains(gid)) {
file = new File(galleryDirectory.getAbsolutePath(), galleryDirectory.getName() + ".zip");
break;
}
}
} }
} }
} }
@ -140,6 +125,28 @@ public class MultiThreadedHTTPServer {
} }
} }
private static File findGalleryZipByGid(File galleryRoot, String gid){
File[] galleryDirectories = galleryRoot.listFiles(File::isDirectory);
if(galleryDirectories == null)
return null;
String marker = "[" + gid;
for(File galleryDirectory : galleryDirectories){
String name = galleryDirectory.getName();
int markerIndex = name.lastIndexOf(marker);
if(markerIndex < 0)
continue;
int suffixIndex = markerIndex + marker.length();
if(suffixIndex >= name.length() || (name.charAt(suffixIndex) != ']' && name.charAt(suffixIndex) != '-'))
continue;
File zip = new File(galleryDirectory, name + ".zip");
if(zip.isFile())
return zip;
}
return null;
}
public static Map<String, String> parseRequestLine(String requestLine) { public static Map<String, String> parseRequestLine(String requestLine) {
Map<String, String> queryParams = new HashMap<>(); Map<String, String> queryParams = new HashMap<>();