Skip to content

Commit

Permalink
Optimize the structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuo894 committed Feb 6, 2024
1 parent 69a28e4 commit bbc0636
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 122 deletions.
129 changes: 10 additions & 119 deletions server/src/main/java/wallpaper/video/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
import com.google.gson.Gson;
import lombok.Cleanup;
import lombok.SneakyThrows;
import wallpaper.video.entity.Project;
import wallpaper.video.entity.ProjectVo;
import wallpaper.video.entity.SearchDto;
import wallpaper.video.util.DistUtil;
import wallpaper.video.util.JSON;
import wallpaper.video.util.ProjectUtil;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -33,27 +38,6 @@

public class Main {

private final static Gson gson = new Gson();

private final static Function<File, Project> getEntity = file -> {
String json = FileUtil.readUtf8String(file);
return gson.fromJson(json, Project.class)
.setPath(file.getParent())
.setId(file.getParentFile().getName());
};

private final static List<Project> files = new ArrayList<>();

private static synchronized void loadList() {
List<Project> projectList = FileUtil.loopFiles(path, file -> file.getName().equals("project.json"))
.stream()
.map(getEntity)
.filter(project -> "video".equalsIgnoreCase(project.getType()))
.collect(Collectors.toList());
files.clear();
files.addAll(projectList);
}

private static String path = "Z:\\SteamLibrary\\steamapps\\workshop\\content\\431960";

@SneakyThrows
Expand All @@ -76,105 +60,12 @@ public static void main(String[] args) {
}
}

loadList();

WatchMonitor watchMonitor = WatchMonitor.createAll(path, new SimpleWatcher() {
@Override
public void onCreate(WatchEvent<?> event, Path currentPath) {
loadList();
}

@Override
public void onModify(WatchEvent<?> event, Path currentPath) {
loadList();
}

@Override
public void onDelete(WatchEvent<?> event, Path currentPath) {
loadList();
}
});
watchMonitor.start();

URL dist = ResourceUtil.getResource("dist");
File root = new File(dist.getFile());
if (dist.getProtocol().equals("jar")) {
root = new File("").getAbsoluteFile();
JarFile jarFile = URLUtil.getJarFile(dist);
List<JarEntry> distList = ListUtil.toList(jarFile.entries());
for (ZipEntry jarEntry : distList) {
String name = jarEntry.getName();
if (!name.startsWith("dist")) {
continue;
}
if (jarEntry.isDirectory()) {
FileUtil.mkdir(root + File.separator + name);
continue;
}
@Cleanup
InputStream inputStream = jarFile.getInputStream(jarEntry);
System.out.println(new File(root + File.separator + name));
FileUtil.writeFromStream(inputStream, root + File.separator + name);
}
}
ProjectUtil.setPath(path);
ProjectUtil.loadList();
ProjectUtil.startWatch();

HttpUtil.createServer(port)
.addAction("/api/list", (req, res) -> {
List<Project> list = files;
String body = req.getBody();
SearchDto dto = gson.fromJson(body, SearchDto.class);

String text = dto.getText();

if (StrUtil.isNotBlank(text)) {
list = list.stream()
.filter(item ->
StrUtil.containsIgnoreCase(item.getId(), text) ||
StrUtil.containsIgnoreCase(item.getTitle(), text) ||
StrUtil.containsIgnoreCase(item.getDescription(), text) ||
StrUtil.containsIgnoreCase(item.getType(), text) ||
CollUtil.contains(item.getTags(), text)
).collect(Collectors.toList());
}


List<ProjectVo> projectVos = BeanUtil.copyToList(list, ProjectVo.class);
res.write(gson.toJson(projectVos), ContentType.JSON.getValue());
res.close();
})
.addAction("/api/preview", (req, res) -> {
ListValueMap<String, String> params = req.getParams();
String id = params.get("id", 0);
Optional<Project> first = files.stream().filter(item -> item.getId().equals(id)).findFirst();
if (first.isEmpty()) {
return;
}
Project project = first.get();
String file = project.getPath() + File.separator + project.getPreview();

res.setContentType("image/gif");
res.write(new File(file));
res.close();
})
.addAction("/api/play", (req, res) -> {
ListValueMap<String, String> params = req.getParams();
String id = params.get("id", 0);
Optional<Project> first = files.stream().filter(item -> item.getId().equals(id)).findFirst();
if (first.isEmpty()) {
return;
}
Project project = first.get();
String file = project.getPath() + File.separator + project.getFile();

res.setHeader("Accept-Ranges", "bytes");
res.setHeader("Content-Type", "video/mp4");
res.setHeader("Content-Disposition", "inline;filename=" + project.getFile());
res.setHeader("Content-Length", String.valueOf(new File(file).length()));

res.write(new File(file));
res.close();
})
.setRoot(root + File.separator + "dist")
.setRoot(DistUtil.getDistFile())
.start();
}
}
45 changes: 45 additions & 0 deletions server/src/main/java/wallpaper/video/action/ListAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package wallpaper.video.action;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import cn.hutool.http.server.action.Action;
import wallpaper.video.entity.Project;
import wallpaper.video.entity.ProjectVo;
import wallpaper.video.entity.SearchDto;
import wallpaper.video.util.JSON;
import wallpaper.video.util.ProjectUtil;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class ListAction implements Action {
@Override
public void doAction(HttpServerRequest req, HttpServerResponse res) throws IOException {
List<Project> list = ProjectUtil.FILES;
String body = req.getBody();
SearchDto dto = JSON.fromJson(body, SearchDto.class);

String text = dto.getText();

if (StrUtil.isNotBlank(text)) {
list = list.stream()
.filter(item ->
StrUtil.containsIgnoreCase(item.getId(), text) ||
StrUtil.containsIgnoreCase(item.getTitle(), text) ||
StrUtil.containsIgnoreCase(item.getDescription(), text) ||
StrUtil.containsIgnoreCase(item.getType(), text) ||
CollUtil.contains(item.getTags(), text)
).collect(Collectors.toList());
}


List<ProjectVo> projectVos = BeanUtil.copyToList(list, ProjectVo.class);
res.write(JSON.toJson(projectVos), ContentType.JSON.getValue());
res.close();
}
}
34 changes: 34 additions & 0 deletions server/src/main/java/wallpaper/video/action/PlayAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package wallpaper.video.action;

import cn.hutool.core.map.multi.ListValueMap;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import cn.hutool.http.server.action.Action;
import wallpaper.video.entity.Project;
import wallpaper.video.util.ProjectUtil;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

public class PlayAction implements Action {
@Override
public void doAction(HttpServerRequest req, HttpServerResponse res) throws IOException {
ListValueMap<String, String> params = req.getParams();
String id = params.get("id", 0);
Optional<Project> first = ProjectUtil.FILES.stream().filter(item -> item.getId().equals(id)).findFirst();
if (first.isEmpty()) {
return;
}
Project project = first.get();
String file = project.getPath() + File.separator + project.getFile();

res.setHeader("Accept-Ranges", "bytes");
res.setHeader("Content-Type", "video/mp4");
res.setHeader("Content-Disposition", "inline;filename=" + project.getFile());
res.setHeader("Content-Length", String.valueOf(new File(file).length()));

res.write(new File(file));
res.close();
}
}
30 changes: 30 additions & 0 deletions server/src/main/java/wallpaper/video/action/PreviewAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package wallpaper.video.action;

import cn.hutool.core.map.multi.ListValueMap;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import cn.hutool.http.server.action.Action;
import wallpaper.video.entity.Project;
import wallpaper.video.util.ProjectUtil;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

public class PreviewAction implements Action {
@Override
public void doAction(HttpServerRequest req, HttpServerResponse res) throws IOException {
ListValueMap<String, String> params = req.getParams();
String id = params.get("id", 0);
Optional<Project> first = ProjectUtil.FILES.stream().filter(item -> item.getId().equals(id)).findFirst();
if (first.isEmpty()) {
return;
}
Project project = first.get();
String file = project.getPath() + File.separator + project.getPreview();

res.setContentType("image/gif");
res.write(new File(file));
res.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wallpaper.video;
package wallpaper.video.entity;

import lombok.Data;
import lombok.experimental.Accessors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wallpaper.video;
package wallpaper.video.entity;

import lombok.Data;
import lombok.experimental.Accessors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wallpaper.video;
package wallpaper.video.entity;

import lombok.Data;
import lombok.experimental.Accessors;
Expand Down
58 changes: 58 additions & 0 deletions server/src/main/java/wallpaper/video/util/DistUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package wallpaper.video.util;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.URLUtil;
import lombok.Cleanup;
import lombok.SneakyThrows;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

public class DistUtil {
@SneakyThrows
public static File getDistFile() {
URL dist = ResourceUtil.getResource("dist");
if (Objects.isNull(dist)) {
return null;
}
File root = new File(dist.getFile());
String protocol = dist.getProtocol();
if (protocol.equals("jar")) {
root = new File("").getAbsoluteFile();

File distFile = new File(root + File.separator + "dist");

if (distFile.exists()) {
return distFile;
}

JarFile jarFile = URLUtil.getJarFile(dist);
List<JarEntry> distList = ListUtil.toList(jarFile.entries());
for (ZipEntry jarEntry : distList) {
String name = jarEntry.getName();
if (!name.startsWith("dist")) {
continue;
}
if (jarEntry.isDirectory()) {
FileUtil.mkdir(root + File.separator + name);
continue;
}
@Cleanup
InputStream inputStream = jarFile.getInputStream(jarEntry);
System.out.println(new File(root + File.separator + name));
FileUtil.writeFromStream(inputStream, root + File.separator + name);
}
return distFile;
}

return root;
}
}
17 changes: 17 additions & 0 deletions server/src/main/java/wallpaper/video/util/JSON.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package wallpaper.video.util;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class JSON {
public final static Gson gson = new com.google.gson.Gson();

public static <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
return gson.fromJson(json,classOfT);
}

public static String toJson(Object src) {
return gson.toJson(src);
}

}
Loading

0 comments on commit bbc0636

Please sign in to comment.