blob: da92c179d60d16f5b7d2e8f48ff282a5763c562f [file] [log] [blame]
rootcd436562025-05-08 14:09:19 +00001package tracker;
root0d8b11f2025-05-15 14:10:43 +00002import java.io.File;
rootd4959a82025-05-27 07:07:37 +00003import java.nio.file.Files;
4import java.nio.file.Path;
5import java.nio.file.Paths;
6import java.nio.file.StandardCopyOption;
rootff0769a2025-05-18 17:24:41 +00007import java.util.HashMap;
8import java.util.Map;
root0d8b11f2025-05-15 14:10:43 +00009import javax.persistence.EntityManager;
10import javax.persistence.EntityManagerFactory;
11import javax.persistence.EntityTransaction;
12import javax.persistence.Persistence;
13import com.querydsl.jpa.impl.JPAUpdateClause;
rootf35409f2025-05-19 04:41:57 +000014import entity.QUserPT;
rootd4959a82025-05-27 07:07:37 +000015import entity.Seed;
16import entity.SeedDownload;
rootff0769a2025-05-18 17:24:41 +000017import entity.TransRecord;
18import entity.config;
rootd4959a82025-05-27 07:07:37 +000019import entity.TTorent;
20import java.time.LocalDateTime;
root0d8b11f2025-05-15 14:10:43 +000021public class Tracker implements TrackerInterface {
rootff0769a2025-05-18 17:24:41 +000022 private final EntityManagerFactory emf;
rootff0769a2025-05-18 17:24:41 +000023 // 默认构造:产线数据库
24 public Tracker() {
25 config cfg = new config();
26 Map<String,Object> props = new HashMap<>();
27 props.put("javax.persistence.jdbc.url",
28 "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database);
29 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
30 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
31 this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
32 }
rootff0769a2025-05-18 17:24:41 +000033 // 测试传入:测试库
34 public Tracker(EntityManagerFactory emf) {
35 this.emf = emf;
36 }
root0d8b11f2025-05-15 14:10:43 +000037 @Override
38 public boolean AddUpLoad(String userid, int upload) {
39 EntityManager em = emf.createEntityManager();
40 EntityTransaction tx = em.getTransaction();
41 try {
42 tx.begin();
43 QUserPT q = QUserPT.userPT;
44 long updated = new JPAUpdateClause(em, q)
45 .where(q.userid.eq(userid))
46 .set(q.upload, q.upload.add(upload))
47 .execute();
48 tx.commit();
rootff0769a2025-05-18 17:24:41 +000049 // 成功时 updated>0 返回 false,失败时返回 true
50 return updated <= 0;
root0d8b11f2025-05-15 14:10:43 +000051 } catch(Exception e) {
52 if (tx.isActive()) tx.rollback();
rootff0769a2025-05-18 17:24:41 +000053 return true;
root0d8b11f2025-05-15 14:10:43 +000054 } finally {
55 em.close();
56 }
57 }
root0d8b11f2025-05-15 14:10:43 +000058 @Override
rootff0769a2025-05-18 17:24:41 +000059 public boolean ReduceUpLoad(String userid, int upload){
60 EntityManager em = emf.createEntityManager();
61 EntityTransaction tx = em.getTransaction();
62 try {
63 tx.begin();
64 QUserPT q = QUserPT.userPT;
65 long updated = new JPAUpdateClause(em, q)
66 .where(q.userid.eq(userid))
67 .set(q.upload, q.upload.subtract(upload))
68 .execute();
69 tx.commit();
70 // 成功时 updated>0 返回 false,失败时返回 true
71 return updated <= 0;
72 } catch(Exception e) {
73 if (tx.isActive()) tx.rollback();
74 return true;
75 } finally {
76 em.close();
77 }
78 }
root0d8b11f2025-05-15 14:10:43 +000079 @Override
rootf35409f2025-05-19 04:41:57 +000080 public boolean AddDownload(String userid, int download) {
81 EntityManager em = emf.createEntityManager();
82 EntityTransaction tx = em.getTransaction();
83 try {
84 tx.begin();
85 QUserPT q = QUserPT.userPT;
86 long updated = new JPAUpdateClause(em, q)
87 .where(q.userid.eq(userid))
88 .set(q.download, q.download.add(download))
89 .execute();
90 tx.commit();
91 return updated <= 0;
92 } catch(Exception e) {
93 if (tx.isActive()) tx.rollback();
94 return true;
95 } finally {
96 em.close();
97 }
98 }
root0d8b11f2025-05-15 14:10:43 +000099 @Override
rootf35409f2025-05-19 04:41:57 +0000100 public boolean ReduceDownload(String userid, int download) {
101 EntityManager em = emf.createEntityManager();
102 EntityTransaction tx = em.getTransaction();
103 try {
104 tx.begin();
105 QUserPT q = QUserPT.userPT;
106 long updated = new JPAUpdateClause(em, q)
107 .where(q.userid.eq(userid))
108 .set(q.download, q.download.subtract(download))
109 .execute();
110 tx.commit();
111 return updated <= 0;
112 } catch(Exception e) {
113 if (tx.isActive()) tx.rollback();
114 return true;
115 } finally {
116 em.close();
117 }
118 }
root0d8b11f2025-05-15 14:10:43 +0000119 @Override
rootf35409f2025-05-19 04:41:57 +0000120 public boolean AddMagic(String userid, int magic) {
121 EntityManager em = emf.createEntityManager();
122 EntityTransaction tx = em.getTransaction();
123 try {
124 tx.begin();
125 QUserPT q = QUserPT.userPT;
126 long updated = new JPAUpdateClause(em, q)
127 .where(q.userid.eq(userid))
128 .set(q.magic, q.magic.add(magic))
129 .execute();
130 tx.commit();
131 return updated <= 0;
132 } catch(Exception e) {
133 if (tx.isActive()) tx.rollback();
134 return true;
135 } finally {
136 em.close();
137 }
138 }
root0d8b11f2025-05-15 14:10:43 +0000139 @Override
rootf35409f2025-05-19 04:41:57 +0000140 public boolean ReduceMagic(String userid, int magic) {
141 EntityManager em = emf.createEntityManager();
142 EntityTransaction tx = em.getTransaction();
143 try {
144 tx.begin();
145 QUserPT q = QUserPT.userPT;
146 long updated = new JPAUpdateClause(em, q)
147 .where(q.userid.eq(userid))
148 .set(q.magic, q.magic.subtract(magic))
149 .execute();
150 tx.commit();
151 return updated <= 0;
152 } catch(Exception e) {
153 if (tx.isActive()) tx.rollback();
154 return true;
155 } finally {
156 em.close();
157 }
158 }
rootd4959a82025-05-27 07:07:37 +0000159 @Override
160 public int SaveTorrent(String seedid, File TTorent){
161 try {
162 Path storageDir = Paths.get(config.TORRENT_STORAGE_DIR);
163 if (!Files.exists(storageDir)) {
164 Files.createDirectories(storageDir);
165 }
166 String filename = TTorent.getName();
167 Path target = storageDir.resolve(seedid + "_" + filename);
168 Files.copy(TTorent.toPath(), target, StandardCopyOption.REPLACE_EXISTING);
rootd4959a82025-05-27 07:07:37 +0000169 EntityManager em = emf.createEntityManager();
170 EntityTransaction tx = em.getTransaction();
171 try {
172 tx.begin();
173 Seed seed = em.find(Seed.class, seedid);
174 seed.url = target.toString();
175 em.merge(seed);
176 tx.commit();
177 return 0;
178 } catch (Exception e) {
179 if (tx.isActive()) tx.rollback();
180 return 1;
181 } finally {
182 em.close();
183 }
184 } catch (Exception e) {
185 return 1;
186 }
187 }
root0d8b11f2025-05-15 14:10:43 +0000188 @Override
rootd4959a82025-05-27 07:07:37 +0000189 public File GetTTorent(String seedid, String userid, String ip) {
190 EntityManager em = emf.createEntityManager();
191 EntityTransaction tx = em.getTransaction();
192 File file = null;
193 try {
194 Seed seed = em.find(Seed.class, seedid);
195 if (seed == null || seed.url == null) {
196 return null;
197 }
198 file = new File(seed.url);
199 if (!file.exists()) {
200 return null;
201 }
202 tx.begin();
203 SeedDownload sd = new SeedDownload();
204 sd.seedId = seedid;
205 sd.userId = userid;
206 sd.clientIp = ip;
207 LocalDateTime now = LocalDateTime.now();
208 sd.downloadStart = now;
209 sd.downloadEnd = now;
210 em.persist(sd);
211 tx.commit();
212 } catch (Exception e) {
213 if (tx.isActive()) tx.rollback();
214 // ignore persistence errors and still return the file
215 } finally {
216 em.close();
217 }
218 return file;
219 }
root0d8b11f2025-05-15 14:10:43 +0000220 @Override
rootf35409f2025-05-19 04:41:57 +0000221 public int AddRecord(TransRecord rd){
222 EntityManager em = emf.createEntityManager();
223 EntityTransaction tx = em.getTransaction();
224 try {
225 tx.begin();
226 em.persist(rd);
227 tx.commit();
228 // 持久化成功,返回1表示插入成功
229 return 1;
230 } catch (Exception e) {
231 if (tx.isActive()) tx.rollback();
232 return -1;
233 } finally {
234 em.close();
235 }
236 }
root33a7d952025-05-18 17:24:41 +0000237}