a120df1a09c850bab29338736b587ffac9a5767b
1 package be
.nikiroo
.fanfix
;
4 import java
.io
.IOException
;
6 import java
.util
.ArrayList
;
7 import java
.util
.HashMap
;
10 import java
.util
.Map
.Entry
;
12 import be
.nikiroo
.fanfix
.bundles
.Config
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Story
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
16 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
17 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
18 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
21 * Manage a library of Stories: import, export, list.
23 * Each {@link Story} object will be associated with a (local to the library)
24 * unique ID, the LUID, which will be used to identify the {@link Story}.
28 public class Library
{
30 private Map
<MetaData
, File
> stories
;
34 * Create a new {@link Library} with the given backend directory.
37 * the directoy where to find the {@link Story} objects
39 public Library(File dir
) {
41 this.stories
= new HashMap
<MetaData
, File
>();
48 * List all the stories of the given source type in the {@link Library}, or
49 * all the stories if NULL is passed as a type.
52 * the type of story to retrieve, or NULL for all
56 public List
<MetaData
> getList(SupportType type
) {
57 String typeString
= type
== null ?
null : type
.getSourceName();
59 List
<MetaData
> list
= new ArrayList
<MetaData
>();
60 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
61 String storyType
= entry
.getValue().getParentFile().getName();
62 if (typeString
== null || typeString
.equalsIgnoreCase(storyType
)) {
63 list
.add(entry
.getKey());
71 * Retrieve a specific {@link Story}.
74 * the Library UID of the story
76 * @return the corresponding {@link Story}
78 public Story
getStory(String luid
) {
80 for (Entry
<MetaData
, File
> entry
: getStories().entrySet()) {
81 if (luid
.equals(entry
.getKey().getLuid())) {
83 SupportType type
= SupportType
.valueOfAllOkUC(entry
85 URL url
= entry
.getValue().toURI().toURL();
87 return BasicSupport
.getSupport(type
).process(url
);
89 throw new IOException("Unknown type: "
90 + entry
.getKey().getType());
92 } catch (IOException e
) {
93 // We should not have not-supported files in the
95 Instance
.syserr(new IOException(
96 "Cannot load file from library: "
97 + entry
.getValue().getPath(), e
));
107 * Import the {@link Story} at the given {@link URL} into the
111 * the {@link URL} to import
113 * @return the imported {@link Story}
115 * @throws IOException
116 * in case of I/O error
118 public Story
imprt(URL url
) throws IOException
{
119 BasicSupport support
= BasicSupport
.getSupport(url
);
120 if (support
== null) {
121 throw new IOException("URL not supported: " + url
.toString());
124 getStories(); // refresh lastId
125 Story story
= support
.process(url
);
126 story
.getMeta().setLuid(String
.format("%03d", (++lastId
)));
133 * Export the {@link Story} to the given target in the given format.
136 * the {@link Story} ID
138 * the {@link OutputType} to transform it to
140 * the target to save to
142 * @return the saved resource (the main saved {@link File})
144 * @throws IOException
145 * in case of I/O error
147 public File
export(String luid
, OutputType type
, String target
)
149 BasicOutput out
= BasicOutput
.getOutput(type
, true);
151 throw new IOException("Output type not supported: " + type
);
154 return out
.process(getStory(luid
), target
);
158 * Save a story as-is to the {@link Library} -- the LUID <b>must</b> be
162 * the {@link Story} to save
164 * @throws IOException
165 * in case of I/O error
167 private void save(Story story
) throws IOException
{
168 MetaData key
= story
.getMeta();
170 getDir(key
).mkdirs();
171 if (!getDir(key
).exists()) {
172 throw new IOException("Cannot create library dir");
177 if (key
!= null && key
.isImageDocument()) {
178 in
= SupportType
.CBZ
;
179 out
= OutputType
.CBZ
;
181 in
= SupportType
.INFO_TEXT
;
182 out
= OutputType
.INFO_TEXT
;
184 BasicOutput it
= BasicOutput
.getOutput(out
, true);
185 File file
= it
.process(story
, getFile(key
).getPath());
187 BasicSupport
.getSupport(in
).processMeta(file
.toURI().toURL())
192 * The directory (full path) where the {@link Story} related to this
193 * {@link MetaData} should be located on disk.
196 * the {@link Story} {@link MetaData}
198 * @return the target directory
200 private File
getDir(MetaData key
) {
201 String source
= key
.getSource().replaceAll("[^a-zA-Z0-9._+-]", "_");
202 return new File(baseDir
, source
);
206 * The target (full path) where the {@link Story} related to this
207 * {@link MetaData} should be located on disk.
210 * the {@link Story} {@link MetaData}
214 private File
getFile(MetaData key
) {
215 String title
= key
.getTitle().replaceAll("[^a-zA-Z0-9._+-]", "_");
216 return new File(getDir(key
), key
.getLuid() + "_" + title
);
220 * Return all the known stories in this {@link Library} object.
222 * @return the stories
224 private Map
<MetaData
, File
> getStories() {
225 if (stories
.isEmpty()) {
228 + Instance
.getConfig().getString(Config
.IMAGE_FORMAT_COVER
)
230 for (File dir
: baseDir
.listFiles()) {
231 if (dir
.isDirectory()) {
232 for (File file
: dir
.listFiles()) {
234 String path
= file
.getPath().toLowerCase();
235 if (!path
.endsWith(".info")
236 && !path
.endsWith(format
)) {
237 // TODO: export .info reading to a class and use
239 SupportType type
= SupportType
.INFO_TEXT
;
240 if (path
.toLowerCase().endsWith(".cbz")) {
241 type
= SupportType
.CBZ
;
243 BasicSupport support
= BasicSupport
245 MetaData meta
= support
.processMeta(
246 file
.toURI().toURL()).getMeta();
248 stories
.put(meta
, file
);
250 int id
= Integer
.parseInt(meta
255 } catch (Exception e
) {
257 Instance
.syserr(new IOException(
258 "Cannot understand the LUID of "
259 + file
.getPath() + ": "
260 + meta
.getLuid(), e
));
264 Instance
.syserr(new IOException(
265 "Cannot get metadata for: "
269 } catch (IOException e
) {
270 // We should not have not-supported files in the
272 Instance
.syserr(new IOException(
273 "Cannot load file from library: "
274 + file
.getPath(), e
));