Compare commits

...

2 Commits

Author SHA1 Message Date
chuzhongzai
6f0049442f 去除个人订阅文件 2023-12-28 16:09:34 +08:00
chuzhongzai
1516226192 前端获取任务进度方式由轮询改为Websocket;去除无用任务状态; 2023-12-28 16:07:22 +08:00
9 changed files with 107 additions and 33 deletions

View File

@ -110,6 +110,11 @@
<version>6.9.1</version> <version>6.9.1</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.lionsoul</groupId> <groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId> <artifactId>ip2region</artifactId>

View File

@ -0,0 +1,22 @@
package com.lion.lionwebsite.Configuration;
import com.lion.lionwebsite.Service.WebSocketService;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
@EnableWebSocket
public class WebsocketConfiguration implements WebSocketConfigurer {
WebSocketService webSocketService;
public WebsocketConfiguration(WebSocketService webSocketService) {
this.webSocketService = webSocketService;
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketService, "/ws/").setAllowedOriginPatterns("*");
}
}

View File

@ -43,7 +43,6 @@ public class GalleryManageController {
case "gid" -> galleryManageService.selectTaskByGid(Integer.parseInt(param)); case "gid" -> galleryManageService.selectTaskByGid(Integer.parseInt(param));
case "all" -> galleryManageService.selectAllGallery(userId); case "all" -> galleryManageService.selectAllGallery(userId);
case "name" -> galleryManageService.selectGalleryByName(param); case "name" -> galleryManageService.selectGalleryByName(param);
case "undone" -> galleryManageService.selectUnDoneGallery();
case "downloader" -> galleryManageService.selectGalleryByDownloader(AuthCode); case "downloader" -> galleryManageService.selectGalleryByDownloader(AuthCode);
default -> Response._failure("参数错误"); default -> Response._failure("参数错误");
}; };

View File

@ -2,10 +2,8 @@ package com.lion.lionwebsite.Controller;
import com.lion.lionwebsite.Service.LocalServiceImpl; import com.lion.lionwebsite.Service.LocalServiceImpl;
import com.lion.lionwebsite.Service.PersonalServiceImpl; import com.lion.lionwebsite.Service.PersonalServiceImpl;
import com.lion.lionwebsite.Util.FileDownload;
import com.lion.lionwebsite.Util.Response; import com.lion.lionwebsite.Util.Response;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -32,11 +30,6 @@ public class PersonalController {
this.localService = localService; this.localService = localService;
} }
@GetMapping("/sub/self")
public void sub(HttpServletResponse response, HttpServletRequest request){
FileDownload.export(request, response, "sub/sub.txt");
}
@GetMapping("/files") @GetMapping("/files")
public String file(String path) throws IOException { public String file(String path) throws IOException {
return personalService.getFiles(path); return personalService.getFiles(path);

View File

@ -6,17 +6,11 @@ import lombok.Data;
@Data @Data
public class GalleryTask { public class GalleryTask {
public static byte DOWNLOADING = 1; public static byte DOWNLOADING = 1;
public static byte DOWNLOAD_COMPLETE = 2; public static byte DOWNLOAD_COMPLETE = 2;
public static byte COMPRESSING = 3;
public static byte DOWNLOAD_QUEUED = 3;
public static byte COMPRESS_COMPLETE = 4; public static byte COMPRESS_COMPLETE = 4;
public static byte COMPRESSING = 5;
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL)
private String name; private String name;

View File

@ -123,6 +123,7 @@ public class GalleryManageService {
if (gallery.getStatus().equals("已提交")) { if (gallery.getStatus().equals("已提交")) {
response.success(gallery.toString()); response.success(gallery.toString());
gallery.setDownloader(user.getId()); gallery.setDownloader(user.getId());
gallery.set_download(true);
galleryMapper.insertGallery(gallery); galleryMapper.insertGallery(gallery);
long usedAmount = Long.parseLong(configurationMapper.selectConfiguration(CustomConfiguration.WEEK_USED_AMOUNT).getValue()); long usedAmount = Long.parseLong(configurationMapper.selectConfiguration(CustomConfiguration.WEEK_USED_AMOUNT).getValue());
@ -230,23 +231,6 @@ public class GalleryManageService {
return response.toJSONString(); return response.toJSONString();
} }
/**
* 查询未完成的本子
*
* @return 查询结果
*/
public String selectUnDoneGallery() {
Response response = Response.generateResponse();
Gallery[] galleries = galleryMapper.selectUnDoneGalleries();
if (galleries.length > 0)
response.success(new ObjectMapper().valueToTree(galleries).toString());
else
response.failure();
return response.toJSONString();
}
/** /**
* 通过本子名查询本子 * 通过本子名查询本子
* *

View File

@ -52,13 +52,16 @@ public class RemoteService {
AtomicInteger atomicInteger; AtomicInteger atomicInteger;
public RemoteService(GalleryMapper galleryMapper, PushService pushService){ WebSocketService webSocketService;
public RemoteService(GalleryMapper galleryMapper, PushService pushService, WebSocketService webSocketService){
this.galleryMapper = galleryMapper; this.galleryMapper = galleryMapper;
this.pushService = pushService; this.pushService = pushService;
atomicInteger = new AtomicInteger(0); atomicInteger = new AtomicInteger(0);
eventLoopGroup = new DefaultEventLoop(); eventLoopGroup = new DefaultEventLoop();
downloadThread = Executors.newCachedThreadPool(); downloadThread = Executors.newCachedThreadPool();
promiseHashMap = new HashMap<>(); promiseHashMap = new HashMap<>();
this.webSocketService = webSocketService;
if(!initChannel()){ //如果远程服务器连接失败则开启本地监听 if(!initChannel()){ //如果远程服务器连接失败则开启本地监听
monitor = new Thread(this::monitorFunc); monitor = new Thread(this::monitorFunc);
@ -190,6 +193,7 @@ public class RemoteService {
log.info(gallery.getName() + "下载进度:" + gallery.getProceeding() + "/" + gallery.getPages()); log.info(gallery.getName() + "下载进度:" + gallery.getProceeding() + "/" + gallery.getPages());
galleryMapper.updateGallery(gallery); galleryMapper.updateGallery(gallery);
} }
webSocketService.updateTaskProcessing(galleryTasks);
} }
else if(msg instanceof ResponseMessage rsm) else if(msg instanceof ResponseMessage rsm)
promiseHashMap.get(rsm.messageId).setSuccess(rsm); promiseHashMap.get(rsm.messageId).setSuccess(rsm);

View File

@ -0,0 +1,73 @@
package com.lion.lionwebsite.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.lion.lionwebsite.Domain.GalleryTask;
import com.lion.lionwebsite.Util.CustomUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.*;
import java.util.ArrayList;
@Service
public class WebSocketService implements WebSocketHandler {
ArrayList<WebSocketSession> sessions;
ObjectMapper objectMapper;
public WebSocketService() {
sessions = new ArrayList<>();
objectMapper = CustomUtil.objectMapper;
}
public void updateTaskProcessing(GalleryTask[] galleryTasks){
if(sessions.isEmpty())
return;
for (GalleryTask galleryTask : galleryTasks)
if(galleryTask.getStatus() == GalleryTask.COMPRESS_COMPLETE) {
sessions.forEach(s -> {
try {
s.sendMessage(new TextMessage("{\"type\": \"fullUpdate\"}"));
} catch (Exception ignore) {
}
});
return;
}
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("type", "updateTasks");
objectNode.set("data", objectMapper.valueToTree(galleryTasks));
System.out.println(objectNode);
sessions.forEach(s -> {
try {
s.sendMessage(new TextMessage(objectNode.toString()));
}catch (Exception ignore){}
});
}
@Override
public void afterConnectionEstablished(@NotNull WebSocketSession session) {}
@Override
public void handleMessage(@NotNull WebSocketSession session, @NotNull WebSocketMessage<?> message) throws Exception {
if(message.getPayload().toString().equals("DownloaderWebsocket"))
sessions.add(session);
else
session.close();
}
@Override
public void handleTransportError(@NotNull WebSocketSession session, @NotNull Throwable exception) {}
@Override
public void afterConnectionClosed(@NotNull WebSocketSession session, @NotNull CloseStatus closeStatus) {
sessions.remove(session);
}
@Override
public boolean supportsPartialMessages() {
return false;
}
}

Binary file not shown.