添加dockerfile,修改torrent表结构,实现原文件名存储,修改post表结构,关联torrent_id
Change-Id: Ie82f559d859e378b5991eb90880e95a0ffcf775d
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..497f1e0
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,14 @@
+# 使用官方OpenJDK基础镜像(推荐Alpine轻量版)
+FROM eclipse-temurin:17-jdk-alpine
+
+# 设置工作目录
+WORKDIR /app
+
+# 复制构建好的JAR文件到镜像中
+COPY target/G8Backend-0.0.1-SNAPSHOT.jar app.jar
+
+# 暴露Spring Boot默认端口(根据实际项目修改)
+EXPOSE 8080
+
+# 启动命令
+ENTRYPOINT ["java", "-jar", "app.jar"]
\ No newline at end of file
diff --git a/src/main/java/com/example/g8backend/controller/TorrentController.java b/src/main/java/com/example/g8backend/controller/TorrentController.java
index 0c016f4..7640e42 100644
--- a/src/main/java/com/example/g8backend/controller/TorrentController.java
+++ b/src/main/java/com/example/g8backend/controller/TorrentController.java
@@ -34,11 +34,23 @@
User user = userService.getById(userId);
String passkey = user.getPasskey();
+ String fileName = multipartFile.getOriginalFilename();
+
+ try {
+ assert fileName != null;
+ } catch (AssertionError e) {
+ return ResponseEntity.badRequest().body("文件名不能为空");
+ }
+
+ if (!fileName.endsWith(".torrent")) {
+ return ResponseEntity.badRequest().body("文件格式不正确,请上传.torrent格式的文件");
+ }
+
File tempFile = File.createTempFile("upload-", ".torrent");
multipartFile.transferTo(tempFile);
try {
- torrentService.handleTorrentUpload(tempFile, userId, passkey);
+ torrentService.handleTorrentUpload(tempFile, fileName, userId, passkey);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
diff --git a/src/main/java/com/example/g8backend/entity/Torrent.java b/src/main/java/com/example/g8backend/entity/Torrent.java
index cb9a4eb..e52df8a 100644
--- a/src/main/java/com/example/g8backend/entity/Torrent.java
+++ b/src/main/java/com/example/g8backend/entity/Torrent.java
@@ -15,6 +15,7 @@
private Long torrentId;
private Long userId;
private String torrentName;
+ private String filePath;
private String infoHash;
private Double fileSize;
diff --git a/src/main/java/com/example/g8backend/mapper/TorrentMapper.java b/src/main/java/com/example/g8backend/mapper/TorrentMapper.java
index f78a68b..6c3d215 100644
--- a/src/main/java/com/example/g8backend/mapper/TorrentMapper.java
+++ b/src/main/java/com/example/g8backend/mapper/TorrentMapper.java
@@ -7,8 +7,9 @@
@Mapper
public interface TorrentMapper extends BaseMapper<Torrent> {
- int insertTorrent (@Param("userId") Long userId,
+ void insertTorrent (@Param("userId") Long userId,
@Param("torrentName") String torrentName,
+ @Param("filePath") String filePath,
@Param("infoHash") String infoHash,
@Param("fileSize") Double fileSize);
Torrent getTorrentByInfoHash (@Param("infoHash") String infoHash);
diff --git a/src/main/java/com/example/g8backend/service/ITorrentService.java b/src/main/java/com/example/g8backend/service/ITorrentService.java
index 87622c8..517887c 100644
--- a/src/main/java/com/example/g8backend/service/ITorrentService.java
+++ b/src/main/java/com/example/g8backend/service/ITorrentService.java
@@ -7,7 +7,7 @@
import java.io.IOException;
public interface ITorrentService extends IService<Torrent> {
- Torrent handleTorrentUpload(File file, Long userId, String passkey) throws IOException;
+ Torrent handleTorrentUpload(File file, String fileName,Long userId, String passkey) throws IOException;
File handleTorrentDownload(Torrent torrent, String passkey) throws IOException;
Torrent findByInfoHash(String infoHash);
Torrent findByTorrentId(Long torrentId);
diff --git a/src/main/java/com/example/g8backend/service/impl/TorrentServiceImpl.java b/src/main/java/com/example/g8backend/service/impl/TorrentServiceImpl.java
index 199ff2a..43855a4 100644
--- a/src/main/java/com/example/g8backend/service/impl/TorrentServiceImpl.java
+++ b/src/main/java/com/example/g8backend/service/impl/TorrentServiceImpl.java
@@ -20,7 +20,7 @@
String tracker = "http://127.0.0.1:8080/tracker/announce/";
@Override
- public Torrent handleTorrentUpload(File file, Long userId, String passkey) throws IOException, IllegalArgumentException {
+ public Torrent handleTorrentUpload(File file, String fileName, Long userId, String passkey) throws IOException, IllegalArgumentException {
// 修改 announce 字段
byte[] modifiedBytes = TorrentUtil.injectTracker(file, tracker + passkey);
@@ -44,7 +44,7 @@
}
// 插入数据库
- torrentMapper.insertTorrent(userId, file.getName(), infoHash, fileSize);
+ torrentMapper.insertTorrent(userId, fileName, file.getName(), infoHash, fileSize);
// 构建返回实体
Torrent torrent = new Torrent();
@@ -57,7 +57,7 @@
@Override
public File handleTorrentDownload(Torrent torrent, String passkey) throws IOException {
- File torrentFile = new File("uploaded-torrents/" + torrent.getTorrentName());
+ File torrentFile = new File("uploaded-torrents/" + torrent.getFilePath());
byte[] modifiedBytes = TorrentUtil.injectTracker(torrentFile, tracker + passkey);
File tempFile = File.createTempFile("user_torrent_", ".torrent");
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 4a4d894..4e44282 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -3,7 +3,7 @@
spring.datasource.url=jdbc:mysql://localhost:3306/g8backend
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.sql.init.mode=always
-logging.level.root=DEBUG
+#logging.level.root=DEBUG
mybatis-plus.mapper-locations=classpath*:/mapper/**/*.xml
diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql
index 607d353..a6860f1 100644
--- a/src/main/resources/data.sql
+++ b/src/main/resources/data.sql
@@ -1,11 +1,66 @@
-# # 后面统一数据库数据用
-#
-# # tags表父标签
-# INSERT INTO `tags` (tag_id, tag_name, parent_id) VALUES (1, '电影', NULL);
-# INSERT INTO `tags` (tag_id, tag_name, parent_id) VALUES (2, '游戏', NULL);
-# INSERT INTO `tags` (tag_id, tag_name, parent_id) VALUES (3, '音乐', NULL);
-#
-# # tags表子标签
-# INSERT INTO `tags` (tag_id, tag_name, parent_id) VALUES (4, '动作', 1);
-# INSERT INTO `tags` (tag_id, tag_name, parent_id) VALUES (5, '喜剧', 1);
-# INSERT INTO `tags` (tag_id, tag_name, parent_id) VALUES (6, '科幻', 1);
\ No newline at end of file
+-- Game 分类
+INSERT IGNORE INTO tags (tag_id, tag_name, parent_id) VALUES (0, 'Game', NULL);
+INSERT IGNORE INTO tags (tag_id, tag_name, parent_id) VALUES
+ (1, 'android', 0),
+ (2, 'mac', 0),
+ (3, 'pc', 0),
+ (4, 'ios', 0),
+ (5, 'other', 0),
+ (6, 'action', 0),
+ (7, 'adventure', 0),
+ (8, 'leisure', 0),
+ (9, 'riddle', 0),
+ (10, 'sport', 0),
+ (11, 'strategy', 0),
+ (12, 'table', 0);
+
+-- video 分类
+INSERT IGNORE INTO tags (tag_id, tag_name, parent_id) VALUES (20, 'video', NULL);
+INSERT IGNORE INTO tags (tag_id, tag_name, parent_id) VALUES
+ (21, 'chinese', 20),
+ (22, 'America', 20),
+ (23, 'Japan', 20),
+ (24, 'Korea', 20),
+ (25, 'Europe', 20),
+ (26, 'other', 20),
+ (27, 'Short', 20),
+ (28, 'plot', 20),
+ (29, 'comedy', 20),
+ (30, 'love', 20),
+ (31, 'action', 20),
+ (32, 'terror', 20),
+ (33, 'science fiction', 20),
+ (34, 'commit a crime', 20),
+ (35, 'Thriller', 20);
+
+-- music 分类
+INSERT IGNORE INTO tags (tag_id, tag_name, parent_id) VALUES (40, 'music', NULL);
+INSERT IGNORE INTO tags (tag_id, tag_name, parent_id) VALUES
+ (41, 'chinese', 40),
+ (42, 'America', 40),
+ (43, 'Japan', 40),
+ (44, 'Korea', 40),
+ (45, 'Europe', 40),
+ (46, 'other', 40),
+ (47, 'rap', 40),
+ (48, 'Electric sound', 40),
+ (49, 'Guofeng', 40),
+ (50, 'motion', 40),
+ (51, 'ballad', 40),
+ (52, 'Rock and roll', 40),
+ (53, 'classical', 40);
+
+-- software 分类
+INSERT IGNORE INTO tags (tag_id, tag_name, parent_id) VALUES (60, 'software', NULL);
+INSERT IGNORE INTO tags (tag_id, tag_name, parent_id) VALUES
+ (61, 'android', 60),
+ (62, 'mac', 60),
+ (63, 'pc', 60),
+ (64, 'ios', 60),
+ (65, 'other', 60),
+ (66, 'life', 60),
+ (67, 'shopping', 60),
+ (68, 'video', 60),
+ (69, 'music', 60),
+ (70, 'read', 60),
+ (71, 'system', 60);
diff --git a/src/main/resources/mapper/TorrentMapper.xml b/src/main/resources/mapper/TorrentMapper.xml
index 9b53d29..243fd92 100644
--- a/src/main/resources/mapper/TorrentMapper.xml
+++ b/src/main/resources/mapper/TorrentMapper.xml
@@ -4,8 +4,8 @@
<mapper namespace="com.example.g8backend.mapper.TorrentMapper">
<insert id="insertTorrent" >
- INSERT INTO torrents (user_id, torrent_name, info_hash, file_size)
- VALUES (#{userId}, #{torrentName}, UNHEX(#{infoHash}), #{fileSize})
+ INSERT INTO torrents (user_id, torrent_name, file_path, info_hash, file_size)
+ VALUES (#{userId}, #{torrentName}, #{filePath}, UNHEX(#{infoHash}), #{fileSize})
</insert>
<select id="getTorrentByInfoHash" resultType="com.example.g8backend.entity.Torrent">
@@ -13,6 +13,7 @@
torrent_id,
user_id,
torrent_name,
+ file_path,
HEX(info_hash) AS infoHash,
file_size
FROM torrents
@@ -24,6 +25,7 @@
torrent_id,
user_id,
torrent_name,
+ file_path,
HEX(info_hash) AS infoHash,
file_size
FROM torrents
diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql
index 5cb15a5..09276b4 100644
--- a/src/main/resources/schema.sql
+++ b/src/main/resources/schema.sql
@@ -21,6 +21,7 @@
`torrent_id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`torrent_name` VARCHAR(255) NOT NULL,
+ `file_path` VARCHAR(255) NOT NULL,
`info_hash` BINARY(20) NOT NULL,
`file_size` FLOAT NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`)
@@ -46,17 +47,19 @@
`view_count` INT DEFAULT 0 COMMENT '浏览数',
`post_title` VARCHAR(255) NOT NULL,
`post_content` TEXT NOT NULL,
+ `torrent_id` INT DEFAULT NULL,
`post_type` ENUM('resource', 'discussion') NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_calculated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后热度计算时间',
FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`),
+ FOREIGN KEY (`torrent_id`) REFERENCES `torrents`(`torrent_id`),
INDEX `idx_hot_score` (`hot_score`), -- 新增热度索引
INDEX `idx_post_type` (`post_type`) -- 新增类型索引
);
-- 标签表(保持不变)
CREATE TABLE IF NOT EXISTS `tags`(
- `tag_id` INT AUTO_INCREMENT PRIMARY KEY,
- `tag_name` VARCHAR(255) NOT NULL UNIQUE,
+ `tag_id` INT PRIMARY KEY,
+ `tag_name` VARCHAR(255) NOT NULL,
`parent_id` INT DEFAULT NULL,
FOREIGN KEY (`parent_id`) REFERENCES `tags`(`tag_id`)
);
@@ -120,7 +123,7 @@
FOREIGN KEY (`post_id`) REFERENCES `posts`(`post_id`),
INDEX `idx_user_view_time` (`user_id`, `view_time` DESC) -- 新增用户浏览时间索引
);
-CREATE TABLE user_tag_preference (
+CREATE TABLE IF NOT EXISTS user_tag_preference (
user_id INT NOT NULL COMMENT '用户ID',
tag_id INT NOT NULL COMMENT '标签ID',
weight DOUBLE DEFAULT 1.0 COMMENT '偏好权重(浏览越多权重越高)',
diff --git a/src/test/java/com/example/g8backend/service/TorrentServiceTest.java b/src/test/java/com/example/g8backend/service/TorrentServiceTest.java
index 6ddca58..fd6d88a 100644
--- a/src/test/java/com/example/g8backend/service/TorrentServiceTest.java
+++ b/src/test/java/com/example/g8backend/service/TorrentServiceTest.java
@@ -83,12 +83,13 @@
.thenReturn(mockedBytes);
mockedStatic.when(() -> TorrentUtil.getInfoHash(any(File.class)))
.thenReturn(expectedInfoHash);
- Torrent torrent = torrentService.handleTorrentUpload(torrentFile, userId, passkey);
+ Torrent torrent = torrentService.handleTorrentUpload(torrentFile,"test.torrent", userId, passkey);
// 验证调用
verify(torrentMapper, times(1))
.insertTorrent(eq(1L),
anyString(),
+ anyString(),
eq("modified-info-hash"),
eq(torrentFile.length()/1024.0/1024.0));