forked from comixed/comixed
-
Notifications
You must be signed in to change notification settings - Fork 2
/
OPDSEntry.java
173 lines (148 loc) · 5.5 KB
/
OPDSEntry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2018, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/
package org.comixed.model.opds;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import lombok.extern.log4j.Log4j2;
import org.codehaus.plexus.util.StringUtils;
import org.comixed.model.comic.Comic;
import org.comixed.model.comic.Credit;
import org.comixed.model.library.ReadingList;
/**
* <code>OPDSEntry</code> represents a single entry within an OPDS feed.
*
* @author João França
* @author Giao Phan
* @author Darryl L. Pierce
*/
@Log4j2
public class OPDSEntry {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "author")
private List<OPDSAuthor> authors;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
private Date updated;
private OPDSContent content;
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "link")
private List<OPDSLink> links;
private String title;
private String id;
public OPDSEntry(Comic comic) {
this.id = comic.getId().toString();
this.updated = comic.getDateAdded();
this.title = comic.getBaseFilename();
StringBuilder comicContent = new StringBuilder("");
if (StringUtils.isNotEmpty(comic.getTitle())) {
comicContent.append(comic.getTitle());
comicContent.append(" - ");
}
if (StringUtils.isNotEmpty(comic.getSummary())) {
comicContent.append(comic.getSummary());
}
this.content = new OPDSContent(comicContent.toString());
this.authors = new ArrayList<>();
for (Credit credit : comic.getCredits()) {
if (StringUtils.equals(credit.getRole().toUpperCase(), "WRITER")) {
log.debug("Adding author: {}", credit.getName());
this.authors.add(new OPDSAuthor(credit.getName(), ""));
}
}
// FIXME: Is there some sort of router interface we can
// use to build urls
String urlPrefix = "/opds/feed/comics/" + comic.getId();
if (!comic.isMissing()) {
log.debug("Added comic to feed: {}", comic.getFilename());
String coverUrl = urlPrefix + "/0/0";
String thumbnailUrl = urlPrefix + "/0/160";
String comicUrl = "";
try {
comicUrl =
urlPrefix
+ "/download/"
+ URLEncoder.encode(comic.getBaseFilename(), StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException error) {
log.error("error encoding comic filename", error);
comicUrl = urlPrefix + "/download/" + comic.getBaseFilename();
}
log.debug("coverUrl: {}", coverUrl);
log.debug("thumbnailUrl: {}", thumbnailUrl);
log.debug("comicUrl: {}", comicUrl);
// TODO need to get the correct mime types for the cover
this.links =
Arrays.asList(
new OPDSLink("image/jpeg", "http://opds-spec.org/image", coverUrl + "?cover=true"),
new OPDSLink(
"image/jpeg",
"http://opds-spec.org/image/thumbnail",
thumbnailUrl + "?cover=true"),
new OPDSLink(
comic.getArchiveType().getMimeType(),
"http://opds-spec.org/acquisition",
comicUrl),
new OPDSVLink(
"image/jpeg",
"http://vaemendis.net/opds-pse/stream",
"/opds/feed/comics/" + comic.getId() + "/{pageNumber}/{maxWidth}",
comic.getPageCount()));
} else {
log.debug("Comic file missing: {}", comic.getFilename());
}
}
public OPDSEntry(String title, String content, List<OPDSAuthor> authors, List<OPDSLink> links) {
this.id = "urn:uuid:" + UUID.randomUUID();
this.updated = new Date(System.currentTimeMillis());
this.title = title;
this.content = new OPDSContent(content);
this.authors = authors;
this.links = links;
}
public OPDSEntry(ReadingList readingList, Long id) {
this.id = readingList.getId().toString();
this.title = readingList.getName();
this.links =
Arrays.asList(
new OPDSLink(
"application/atom+xml; profile=opds-catalog; kind=acquisition",
"subsection",
"/opds-comics/" + id + "/?displayFiles=true"));
}
public List<OPDSAuthor> getAuthors() {
return this.authors;
}
public OPDSContent getContent() {
return this.content;
}
public String getId() {
return this.id;
}
public List<OPDSLink> getLinks() {
return this.links;
}
public String getTitle() {
return this.title;
}
public Date getUpdated() {
return this.updated;
}
}