return null;
}
+ /**
+ * Return the cover image associated to this author.
+ * <p>
+ * By default, return the custom cover if any, and if not, return the cover
+ * of the first story with this author.
+ *
+ * @param author
+ * the author
+ *
+ * @return the cover image or NULL
+ */
+ public Image getAuthorCover(String author) {
+ Image custom = getCustomAuthorCover(author);
+ if (custom != null) {
+ return custom;
+ }
+
+ List<MetaData> metas = getListByAuthor(author);
+ if (metas.size() > 0) {
+ return getCover(metas.get(0).getLuid());
+ }
+
+ return null;
+ }
+
/**
* Return the custom cover image associated to this source.
* <p>
}
/**
- * Fix the source cover to the given story cover.
+ * Return the custom cover image associated to this author.
+ * <p>
+ * By default, return NULL.
+ *
+ * @param author
+ * the author to look for
+ *
+ * @return the custom cover or NULL if none
+ */
+ public Image getCustomAuthorCover(@SuppressWarnings("unused") String author) {
+ return null;
+ }
+
+ /**
+ * Set the source cover to the given story cover.
*
* @param source
* the source to change
*/
public abstract void setSourceCover(String source, String luid);
+ /**
+ * Set the author cover to the given story cover.
+ *
+ * @param source
+ * the author to change
+ * @param luid
+ * the story LUID
+ */
+ public abstract void setAuthorCover(String author, String luid);
+
/**
* Return the list of stories (represented by their {@link MetaData}, which
* <b>MAY</b> not have the cover included).
return lib.getSourceCover(source);
}
+ @Override
+ public Image getAuthorCover(String author) {
+ Image custom = getCustomSourceCover(author);
+ if (custom != null) {
+ return custom;
+ }
+
+ Image cached = cacheLib.getSourceCover(author);
+ if (cached != null) {
+ return cached;
+ }
+
+ return lib.getSourceCover(author);
+
+ }
+
@Override
public Image getCustomSourceCover(String source) {
Image custom = cacheLib.getCustomSourceCover(source);
return custom;
}
+
+ @Override
+ public Image getCustomAuthorCover(String author) {
+ Image custom = cacheLib.getCustomAuthorCover(author);
+ if (custom == null) {
+ custom = lib.getCustomAuthorCover(author);
+ if (custom != null) {
+ cacheLib.setAuthorCover(author, custom);
+ }
+ }
+
+ return custom;
+ }
@Override
public void setSourceCover(String source, String luid) {
cacheLib.setSourceCover(source, getCover(luid));
}
+ @Override
+ public void setAuthorCover(String author, String luid) {
+ lib.setAuthorCover(author, luid);
+ cacheLib.setAuthorCover(author, getCover(luid));
+ }
+
@Override
protected void updateInfo(MetaData meta) {
if (meta != null && metas != null) {
import be.nikiroo.utils.IOUtils;
import be.nikiroo.utils.Image;
import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.StringUtils;
/**
* This {@link BasicLibrary} will store the stories locally on disk.
private int lastId;
private Map<MetaData, File[]> stories; // Files: [ infoFile, TargetFile ]
private Map<String, Image> sourceCovers;
+ private Map<String, Image> authorCovers;
private File baseDir;
private OutputType text;
return sourceCovers.get(source);
}
+ @Override
+ public synchronized Image getCustomAuthorCover(String author) {
+ File cover = getAuthorCoverFile(author);
+ if (cover.exists()) {
+ InputStream in;
+ try {
+ in = new FileInputStream(cover);
+ try {
+ authorCovers.put(author, new Image(in));
+ } finally {
+ in.close();
+ }
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ Instance.getTraceHandler().error(
+ new IOException(
+ "Cannot load the existing custom author cover: "
+ + cover, e));
+ }
+ }
+
+ return authorCovers.get(author);
+ }
+
@Override
public void setSourceCover(String source, String luid) {
setSourceCover(source, getCover(luid));
}
+ @Override
+ public void setAuthorCover(String author, String luid) {
+ setAuthorCover(author, getCover(luid));
+ }
+
/**
- * Fix the source cover to the given story cover.
+ * Set the source cover to the given story cover.
*
* @param source
* the source to change
}
}
+ /**
+ * Set the author cover to the given story cover.
+ *
+ * @param author
+ * the author to change
+ * @param coverImage
+ * the cover image
+ */
+ synchronized void setAuthorCover(String author, Image coverImage) {
+ File cover = getAuthorCoverFile(author);
+ cover.getParentFile().mkdirs();
+ try {
+ Instance.getCache().saveAsImage(coverImage, cover, true);
+ if (authorCovers != null) {
+ authorCovers.put(author, coverImage);
+ }
+ } catch (IOException e) {
+ Instance.getTraceHandler().error(e);
+ }
+ }
+
@Override
public void imprt(BasicLibrary other, String luid, Progress pg)
throws IOException {
title = "";
}
title = title.replaceAll("[^a-zA-Z0-9._+-]", "_");
+ if (title.length() > 40) {
+ title = title.substring(0, 40);
+ }
return new File(getExpectedDir(key.getSource()), key.getLuid() + "_"
+ title);
}
private File getExpectedDir(String source) {
String sanitizedSource = source.replaceAll("[^a-zA-Z0-9._+/-]", "_");
- while (sanitizedSource.startsWith("/")) {
+ while (sanitizedSource.startsWith("/")
+ || sanitizedSource.startsWith("_")) {
if (sanitizedSource.length() > 1) {
sanitizedSource = sanitizedSource.substring(1);
} else {
sanitizedSource = sanitizedSource.replace("/", File.separator);
if (sanitizedSource.isEmpty()) {
- sanitizedSource = "EMPTY";
+ sanitizedSource = "_EMPTY";
}
return new File(baseDir, sanitizedSource);
}
+ /**
+ * Return the full path to the file to use for the custom cover of this
+ * author.
+ * <p>
+ * One or more of the parent directories <b>MAY</b> not exist.
+ *
+ * @param author
+ * the author
+ *
+ * @return the custom cover file
+ */
+ private File getAuthorCoverFile(String author) {
+ File aDir = new File(baseDir, "_AUTHORS");
+ String hash = StringUtils.getMd5Hash(author);
+ String ext = Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER);
+ return new File(aDir, hash + "." + ext.toLowerCase());
+ }
+
/**
* Return the list of files/directories on disk for this {@link Story}.
* <p>
@Override
public Image getCustomSourceCover(final String source) {
+ return getCustomCover(source, "SOURCE");
+ }
+
+ @Override
+ public Image getCustomAuthorCover(final String author) {
+ return getCustomCover(author, "AUTHOR");
+ }
+
+ // type: "SOURCE" or "AUTHOR"
+ private Image getCustomCover(final String source, final String type) {
final Image[] result = new Image[1];
try {
new ConnectActionClientObject(host, port, true) {
@Override
public void action(Version serverVersion) throws Exception {
- Object rep = send(new Object[] { md5,
- "GET_CUSTOM_SOURCE_COVER", source });
+ Object rep = send(new Object[] { md5, "GET_CUSTOM_COVER",
+ type, source });
result[0] = (Image) rep;
}
@Override
public void setSourceCover(final String source, final String luid) {
+ setCover(source, luid, "SOURCE");
+ }
+
+ @Override
+ public void setAuthorCover(final String author, final String luid) {
+ setCover(author, luid, "AUTHOR");
+ }
+
+ // type = "SOURCE" | "AUTHOR"
+ private void setCover(final String value, final String luid,
+ final String type) {
try {
new ConnectActionClientObject(host, port, true) {
@Override
public void action(Version serverVersion) throws Exception {
- send(new Object[] { md5, "SET_SOURCE_COVER", source, luid });
+ send(new Object[] { md5, "SET_COVER", type, value, luid });
}
@Override
* the LUID</li>
* <li>[md5] DELETE_STORY [luid]: delete the story of LUID luid</li>
* <li>[md5] GET_COVER [luid]: return the cover of the story</li>
- * <li>[md5] GET_CUSTOM_SOURCE_COVER [source]: return the cover for this source</li>
- * <li>[md5] SET_SOURCE_COVER [source], [luid]: set the default cover for the
- * given source to the cover of the story denoted by luid</li>
+ * <li>[md5] GET_CUSTOM_COVER ["SOURCE"|"AUTHOR"] [source]: return the cover for
+ * this source/author</li>
+ * <li>[md5] SET_COVER ["SOURCE"|"AUTHOR"] [value] [luid]: set the default cover
+ * for the given source/author to the cover of the story denoted by luid</li>
* <li>[md5] CHANGE_SOURCE [luid] [new source]: change the source of the story
* of LUID luid</li>
* <li>[md5] EXIT: stop the server</li>
Instance.getLibrary().delete((String) args[0]);
} else if ("GET_COVER".equals(command)) {
return Instance.getLibrary().getCover((String) args[0]);
- } else if ("GET_CUSTOM_SOURCE_COVER".equals(command)) {
- return Instance.getLibrary().getCustomSourceCover((String) args[0]);
- } else if ("SET_SOURCE_COVER".equals(command)) {
- Instance.getLibrary().setSourceCover((String) args[0],
- (String) args[1]);
+ } else if ("GET_CUSTOM_COVER".equals(command)) {
+ if ("SOURCE".equals(args[0])) {
+ return Instance.getLibrary().getCustomSourceCover(
+ (String) args[1]);
+ } else if ("AUTHOR".equals(args[0])) {
+ return Instance.getLibrary().getCustomAuthorCover(
+ (String) args[1]);
+ } else {
+ return null;
+ }
+ } else if ("SET_COVER".equals(command)) {
+ if ("SOURCE".equals(args[0])) {
+ Instance.getLibrary().setSourceCover((String) args[1],
+ (String) args[2]);
+ } else if ("AUTHOR".equals(args[0])) {
+ Instance.getLibrary().setAuthorCover((String) args[1],
+ (String) args[2]);
+ }
} else if ("CHANGE_STA".equals(command)) {
Progress pg = createPgForwarder(action);
Instance.getLibrary().changeSTA((String) args[0], (String) args[1],