blob: 210b9bfb630cecc685554d71ecabf4f6c33a8f46 [file] [log] [blame]
rootcd436562025-05-08 14:09:19 +00001package tracker;
2
root0d8b11f2025-05-15 14:10:43 +00003import java.io.File;
rootd4959a82025-05-27 07:07:37 +00004import java.nio.file.Files;
5import java.nio.file.Path;
6import java.nio.file.Paths;
7import java.nio.file.StandardCopyOption;
rootff0769a2025-05-18 17:24:41 +00008import java.util.HashMap;
9import java.util.Map;
root0d8b11f2025-05-15 14:10:43 +000010import javax.persistence.EntityManager;
11import javax.persistence.EntityManagerFactory;
12import javax.persistence.EntityTransaction;
13import javax.persistence.Persistence;
14import com.querydsl.jpa.impl.JPAUpdateClause;
rootf35409f2025-05-19 04:41:57 +000015import entity.QUserPT;
rootd4959a82025-05-27 07:07:37 +000016import entity.Seed;
17import entity.SeedDownload;
rootff0769a2025-05-18 17:24:41 +000018import entity.TransRecord;
19import entity.config;
rootd4959a82025-05-27 07:07:37 +000020import entity.TTorent;
21import java.time.LocalDateTime;
root0d8b11f2025-05-15 14:10:43 +000022
23public class Tracker implements TrackerInterface {
rootff0769a2025-05-18 17:24:41 +000024 private final EntityManagerFactory emf;
25
26 // 默认构造:产线数据库
27 public Tracker() {
28 config cfg = new config();
29 Map<String,Object> props = new HashMap<>();
30 props.put("javax.persistence.jdbc.url",
31 "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database);
32 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
33 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
34 this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
35 }
36
37 // 测试传入:测试库
38 public Tracker(EntityManagerFactory emf) {
39 this.emf = emf;
40 }
root0d8b11f2025-05-15 14:10:43 +000041
42 @Override
43 public boolean AddUpLoad(String userid, int upload) {
44 EntityManager em = emf.createEntityManager();
45 EntityTransaction tx = em.getTransaction();
46 try {
47 tx.begin();
48 QUserPT q = QUserPT.userPT;
49 long updated = new JPAUpdateClause(em, q)
50 .where(q.userid.eq(userid))
51 .set(q.upload, q.upload.add(upload))
52 .execute();
53 tx.commit();
rootff0769a2025-05-18 17:24:41 +000054 // 成功时 updated>0 返回 false,失败时返回 true
55 return updated <= 0;
root0d8b11f2025-05-15 14:10:43 +000056 } catch(Exception e) {
57 if (tx.isActive()) tx.rollback();
rootff0769a2025-05-18 17:24:41 +000058 return true;
root0d8b11f2025-05-15 14:10:43 +000059 } finally {
60 em.close();
61 }
62 }
63
64 @Override
rootff0769a2025-05-18 17:24:41 +000065 public boolean ReduceUpLoad(String userid, int upload){
66 EntityManager em = emf.createEntityManager();
67 EntityTransaction tx = em.getTransaction();
68 try {
69 tx.begin();
70 QUserPT q = QUserPT.userPT;
71 long updated = new JPAUpdateClause(em, q)
72 .where(q.userid.eq(userid))
73 .set(q.upload, q.upload.subtract(upload))
74 .execute();
75 tx.commit();
76 // 成功时 updated>0 返回 false,失败时返回 true
77 return updated <= 0;
78 } catch(Exception e) {
79 if (tx.isActive()) tx.rollback();
80 return true;
81 } finally {
82 em.close();
83 }
84 }
root0d8b11f2025-05-15 14:10:43 +000085
86 @Override
rootf35409f2025-05-19 04:41:57 +000087 public boolean AddDownload(String userid, int download) {
88 EntityManager em = emf.createEntityManager();
89 EntityTransaction tx = em.getTransaction();
90 try {
91 tx.begin();
92 QUserPT q = QUserPT.userPT;
93 long updated = new JPAUpdateClause(em, q)
94 .where(q.userid.eq(userid))
95 .set(q.download, q.download.add(download))
96 .execute();
97 tx.commit();
98 return updated <= 0;
99 } catch(Exception e) {
100 if (tx.isActive()) tx.rollback();
101 return true;
102 } finally {
103 em.close();
104 }
105 }
root0d8b11f2025-05-15 14:10:43 +0000106
107 @Override
rootf35409f2025-05-19 04:41:57 +0000108 public boolean ReduceDownload(String userid, int download) {
109 EntityManager em = emf.createEntityManager();
110 EntityTransaction tx = em.getTransaction();
111 try {
112 tx.begin();
113 QUserPT q = QUserPT.userPT;
114 long updated = new JPAUpdateClause(em, q)
115 .where(q.userid.eq(userid))
116 .set(q.download, q.download.subtract(download))
117 .execute();
118 tx.commit();
119 return updated <= 0;
120 } catch(Exception e) {
121 if (tx.isActive()) tx.rollback();
122 return true;
123 } finally {
124 em.close();
125 }
126 }
root0d8b11f2025-05-15 14:10:43 +0000127
128 @Override
rootf35409f2025-05-19 04:41:57 +0000129 public boolean AddMagic(String userid, int magic) {
130 EntityManager em = emf.createEntityManager();
131 EntityTransaction tx = em.getTransaction();
132 try {
133 tx.begin();
134 QUserPT q = QUserPT.userPT;
135 long updated = new JPAUpdateClause(em, q)
136 .where(q.userid.eq(userid))
137 .set(q.magic, q.magic.add(magic))
138 .execute();
139 tx.commit();
140 return updated <= 0;
141 } catch(Exception e) {
142 if (tx.isActive()) tx.rollback();
143 return true;
144 } finally {
145 em.close();
146 }
147 }
root0d8b11f2025-05-15 14:10:43 +0000148
149 @Override
rootf35409f2025-05-19 04:41:57 +0000150 public boolean ReduceMagic(String userid, int magic) {
151 EntityManager em = emf.createEntityManager();
152 EntityTransaction tx = em.getTransaction();
153 try {
154 tx.begin();
155 QUserPT q = QUserPT.userPT;
156 long updated = new JPAUpdateClause(em, q)
157 .where(q.userid.eq(userid))
158 .set(q.magic, q.magic.subtract(magic))
159 .execute();
160 tx.commit();
161 return updated <= 0;
162 } catch(Exception e) {
163 if (tx.isActive()) tx.rollback();
164 return true;
165 } finally {
166 em.close();
167 }
168 }
169
rootd4959a82025-05-27 07:07:37 +0000170 @Override
171 public int SaveTorrent(String seedid, File TTorent){
172 try {
173 Path storageDir = Paths.get(config.TORRENT_STORAGE_DIR);
174 if (!Files.exists(storageDir)) {
175 Files.createDirectories(storageDir);
176 }
177 String filename = TTorent.getName();
178 Path target = storageDir.resolve(seedid + "_" + filename);
179 Files.copy(TTorent.toPath(), target, StandardCopyOption.REPLACE_EXISTING);
180
181 EntityManager em = emf.createEntityManager();
182 EntityTransaction tx = em.getTransaction();
183 try {
184 tx.begin();
185 Seed seed = em.find(Seed.class, seedid);
186 seed.url = target.toString();
187 em.merge(seed);
188 tx.commit();
189 return 0;
190 } catch (Exception e) {
191 if (tx.isActive()) tx.rollback();
192 return 1;
193 } finally {
194 em.close();
195 }
196 } catch (Exception e) {
197 return 1;
198 }
199 }
root0d8b11f2025-05-15 14:10:43 +0000200
201 @Override
rootd4959a82025-05-27 07:07:37 +0000202 public File GetTTorent(String seedid, String userid, String ip) {
203 EntityManager em = emf.createEntityManager();
204 EntityTransaction tx = em.getTransaction();
205 File file = null;
206 try {
207 Seed seed = em.find(Seed.class, seedid);
208 if (seed == null || seed.url == null) {
209 return null;
210 }
211 file = new File(seed.url);
212 if (!file.exists()) {
213 return null;
214 }
215 tx.begin();
216 SeedDownload sd = new SeedDownload();
217 sd.seedId = seedid;
218 sd.userId = userid;
219 sd.clientIp = ip;
220 LocalDateTime now = LocalDateTime.now();
221 sd.downloadStart = now;
222 sd.downloadEnd = now;
223 em.persist(sd);
224 tx.commit();
225 } catch (Exception e) {
226 if (tx.isActive()) tx.rollback();
227 // ignore persistence errors and still return the file
228 } finally {
229 em.close();
230 }
231 return file;
232 }
root0d8b11f2025-05-15 14:10:43 +0000233
234 @Override
rootf35409f2025-05-19 04:41:57 +0000235 public int AddRecord(TransRecord rd){
236 EntityManager em = emf.createEntityManager();
237 EntityTransaction tx = em.getTransaction();
238 try {
239 tx.begin();
240 em.persist(rd);
241 tx.commit();
242 // 持久化成功,返回1表示插入成功
243 return 1;
244 } catch (Exception e) {
245 if (tx.isActive()) tx.rollback();
246 return -1;
247 } finally {
248 em.close();
249 }
250 }
root0d8b11f2025-05-15 14:10:43 +0000251}