All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.moviejukebox.scanner.artwork.PhotoScanner Maven / Gradle / Ivy

There is a newer version: 2.9
Show newest version
/*
 *      Copyright (c) 2004-2012 YAMJ Members
 *      http://code.google.com/p/moviejukebox/people/list
 *
 *      Web: http://code.google.com/p/moviejukebox/
 *
 *      This software is licensed under a Creative Commons License
 *      See this page: http://code.google.com/p/moviejukebox/wiki/License
 *
 *      For any reuse or distribution, you must make clear to others the
 *      license terms of this work.
 */
/**
 * PhotoScanner
 *
 * Routines for locating and downloading Photo for people
 *
 */
package com.moviejukebox.scanner.artwork;

import com.moviejukebox.model.Jukebox;
import com.moviejukebox.model.Movie;
import com.moviejukebox.model.Person;
import com.moviejukebox.plugin.MovieImagePlugin;
import com.moviejukebox.tools.FileTools;
import com.moviejukebox.tools.GraphicTools;
import com.moviejukebox.tools.PropertiesUtil;
import static com.moviejukebox.tools.PropertiesUtil.FALSE;
import com.moviejukebox.tools.StringTools;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;

/**
 * Scanner for photo files in local directory
 *
 * @author ilgizar Initial code copied from BannerScanner.java
 *
 */
public class PhotoScanner {

    private static final Logger logger = Logger.getLogger(PhotoScanner.class);
    private static final String logMessage = "PhotoScanner: ";
    private static Collection photoExtensions = new ArrayList();
    private static boolean photoOverwrite = PropertiesUtil.getBooleanProperty("mjb.forcePhotoOverwrite", FALSE);
    private static Collection photoImageName;
    private static String skinHome = PropertiesUtil.getProperty("mjb.skin.dir", "./skins/default");
    private static String peopleFolder;

    static {
        // We get valid extensions
        StringTokenizer st = new StringTokenizer(PropertiesUtil.getProperty("photo.scanner.photoExtensions", "jpg,jpeg,gif,bmp,png"), ",;| ");
        while (st.hasMoreTokens()) {
            photoExtensions.add(st.nextToken());
        }

        // Issue 1947: Cast enhancement - option to save all related files to a specific folder
        peopleFolder = PropertiesUtil.getProperty("mjb.people.folder", "");
        if (StringTools.isNotValidString(peopleFolder)) {
            peopleFolder = "";
        } else if (!peopleFolder.endsWith(File.separator)) {
            peopleFolder += File.separator;
        }
    }

    /**
     * Scan for local photo and download if necessary
     *
     * @param imagePlugin
     * @param jukeboxDetailsRoot
     * @param tempJukeboxDetailsRoot
     * @param person
     */
    public static boolean scan(MovieImagePlugin imagePlugin, Jukebox jukebox, Person person) {
        String localPhotoBaseFilename = person.getFilename();
        File localPhotoFile = null;
        boolean foundLocalPhoto = false;

        // Try searching the fileCache for the filename.
        localPhotoFile = FileTools.findFilenameInCache(localPhotoBaseFilename, photoExtensions, jukebox, logMessage, Boolean.TRUE);
        if (localPhotoFile != null) {
            foundLocalPhoto = true;
        }

        if (!foundLocalPhoto) {
            downloadPhoto(imagePlugin, jukebox, person);
        }
        return foundLocalPhoto;
    }

    /**
     * Download the photo from the URL. Initially this is populated from TheTVDB
     * plugin
     *
     * @param imagePlugin
     * @param jukeboxDetailsRoot
     * @param tempJukeboxDetailsRoot
     * @param person
     */
    private static void downloadPhoto(MovieImagePlugin imagePlugin, Jukebox jukebox, Person person) {
        String prevPhotoFilename = person.getPhotoFilename();
        person.setPhotoFilename();
        String safePhotoFilename = person.getPhotoFilename();
        String photoFilename = jukebox.getJukeboxRootLocationDetails() + File.separator + peopleFolder + safePhotoFilename;
        File photoFile = FileTools.fileCache.getFile(photoFilename);
        String tmpDestFileName = jukebox.getJukeboxTempLocationDetails() + File.separator + peopleFolder + safePhotoFilename;
        File tmpDestFile = new File(tmpDestFileName);
        String dummyFileName = skinHome + File.separator + "resources" + File.separator + "dummy_photo.jpg";
        File dummyFile = new File(dummyFileName);
        photoFile.getParentFile().mkdirs();
        tmpDestFile.getParentFile().mkdirs();

        if (StringTools.isValidString(person.getPhotoURL())) {

            // Do not overwrite existing photo unless ForcePhotoOverwrite = true
            if (photoOverwrite || person.isDirtyPhoto() || (!photoFile.exists() && !tmpDestFile.exists())) {
                try {
                    logger.debug(logMessage + "Downloading photo for " + person.getName() + " to " + tmpDestFileName + " [calling plugin]");

                    // Download the photo using the proxy save downloadImage
                    FileTools.downloadImage(tmpDestFile, person.getPhotoURL());
                    BufferedImage photoImage = GraphicTools.loadJPEGImage(tmpDestFile);

                    if (photoImage != null) {
//                        photoImage = imagePlugin.generate(person, photoImage, "photos", null);
                        GraphicTools.saveImageToDisk(photoImage, tmpDestFileName);
                        logger.debug(logMessage + "Downloaded photo for " + person.getPhotoURL());
                    } else {
                        person.setPhotoFilename(Movie.UNKNOWN);
                        person.setPhotoURL(Movie.UNKNOWN);
                    }
                } catch (Exception error) {
                    logger.debug(logMessage + "Failed to download photo: " + person.getPhotoURL());
                    person.setPhotoURL(Movie.UNKNOWN);
                }
            } else {
                logger.debug(logMessage + "Photo exists for " + person.getName());
            }
        } else if ((photoOverwrite || (!photoFile.exists() && !tmpDestFile.exists()))) {
            if (dummyFile.exists()) {
                logger.debug("Dummy image used for " + person.getName());
                FileTools.copyFile(dummyFile, tmpDestFile);
            } else {
                person.clearPhotoFilename();
                if (prevPhotoFilename.equals(Movie.UNKNOWN)) {
                    person.setDirty(false);
                }
            }
        }

        return;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy