|
1 | 1 | package com.robothaver.torrentfileparser.parser; |
2 | 2 |
|
3 | | -import com.robothaver.torrentfileparser.domain.Torrent; |
| 3 | +import com.robothaver.torrentfileparser.domain.TorrentMetadata; |
4 | 4 | import com.robothaver.torrentfileparser.domain.TorrentFile; |
5 | 5 |
|
6 | 6 | import java.util.List; |
7 | 7 | import java.util.Map; |
8 | 8 |
|
9 | 9 | public class TorrentBuilder { |
10 | | - private final Torrent torrent = new Torrent(); |
| 10 | + private final TorrentMetadata torrentMetadata = new TorrentMetadata(); |
11 | 11 | private Long lastLength; |
12 | 12 |
|
| 13 | + @SuppressWarnings("unchecked") |
13 | 14 | public void processKeyValue(String key, Object value) { |
14 | 15 | switch (key) { |
15 | | - case "announce" -> torrent.setAnnounce(String.valueOf(value)); |
16 | | - case "name" -> torrent.setName(String.valueOf(value)); |
17 | | - case "announce-list" -> torrent.setAnnounceList((List<List<String>>) value); |
18 | | - case "azureus_properties" -> torrent.setAzureusProperties((Map<String, Object>) value); |
19 | | - case "created by" -> torrent.setCreator(String.valueOf(value)); |
20 | | - case "creation date" -> torrent.setCreationDate((long) value); |
21 | | - case "encoding" -> torrent.setEncoding(String.valueOf(value)); |
22 | | - case "files" -> torrent.setSingleFile(false); |
| 16 | + case "announce" -> torrentMetadata.setAnnounce(String.valueOf(value)); |
| 17 | + case "name" -> torrentMetadata.setName(String.valueOf(value)); |
| 18 | + case "announce-list" -> torrentMetadata.setAnnounceList((List<List<String>>) value); |
| 19 | + case "azureus_properties" -> torrentMetadata.setAzureusProperties((Map<String, Object>) value); |
| 20 | + case "created by" -> torrentMetadata.setCreator(String.valueOf(value)); |
| 21 | + case "creation date" -> torrentMetadata.setCreationDate((long) value); |
| 22 | + case "encoding" -> torrentMetadata.setEncoding(String.valueOf(value)); |
| 23 | + case "files" -> torrentMetadata.setSingleFile(false); |
23 | 24 | case "length" -> { |
24 | 25 | lastLength = (long) value; |
25 | | - torrent.setTotalLength(torrent.getTotalLength() + lastLength); |
| 26 | + torrentMetadata.setTotalLength(torrentMetadata.getTotalLength() + lastLength); |
| 27 | + } |
| 28 | + case "path" -> parseFile(value); |
| 29 | + case "piece length" -> torrentMetadata.setPieceLength((long) value); |
| 30 | + case "source" -> torrentMetadata.setSource(String.valueOf(value)); |
| 31 | + case "pieces" -> torrentMetadata.setPieces(String.valueOf(value)); |
| 32 | + case "comment" -> torrentMetadata.setComment(String.valueOf(value)); |
| 33 | + case "private" -> torrentMetadata.setPrivate((long) value == 1); |
| 34 | + default -> { |
| 35 | + if (key.equals("info")) return; |
| 36 | + torrentMetadata.getOtherValues().put(key, value); |
26 | 37 | } |
27 | | - case "path" -> torrent.getFiles().add(new TorrentFile(lastLength, formatFilePath(String.valueOf(value)))); |
28 | | - case "piece length" -> torrent.setPieceLength((long) value); |
29 | | - case "source" -> torrent.setSource(String.valueOf(value)); |
30 | | - case "pieces" -> torrent.setPieces(String.valueOf(value)); |
31 | | - case "comment" -> torrent.setComment(String.valueOf(value)); |
32 | | - case "private" -> torrent.setPrivate((long) value == 1); |
33 | 38 | } |
34 | 39 | } |
35 | 40 |
|
36 | 41 | public void setInfoHash(byte[] infoBytes) { |
37 | | - torrent.setInfoHash(InfoHasCompute.getInfoHash(infoBytes)); |
| 42 | + torrentMetadata.setInfoHash(InfoHasCompute.getInfoHash(infoBytes)); |
38 | 43 | } |
39 | 44 |
|
40 | | - private String formatFilePath(String path) { |
41 | | - return path.replace("[", "").replace("]", "").replace(", ", "/"); |
| 45 | + private void parseFile(Object value) { |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + List<String> pathElements = (List<String>) value; |
| 48 | + String fullPath = String.join("/", pathElements); |
| 49 | + torrentMetadata.getFiles().add(new TorrentFile(lastLength, fullPath)); |
42 | 50 | } |
43 | 51 |
|
44 | | - public Torrent getTorrent() { |
45 | | - return torrent; |
| 52 | + public TorrentMetadata getTorrent() { |
| 53 | + return torrentMetadata; |
46 | 54 | } |
47 | 55 | } |
0 commit comments