001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.cacheadmin;
029
030import com.alkacon.simapi.Simapi;
031
032import org.opencms.file.CmsObject;
033import org.opencms.loader.CmsImageLoader;
034import org.opencms.main.CmsException;
035import org.opencms.main.OpenCms;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.util.CmsFileUtil;
038
039import java.awt.image.BufferedImage;
040import java.io.FileInputStream;
041import java.io.IOException;
042
043import org.apache.commons.io.IOUtils;
044
045/**
046 * Bean for Variations im image resources.<p>
047 */
048public class CmsVariationBean {
049
050    /**Variation path.*/
051    private String m_variationPath;
052
053    /**Root CmsObject. */
054    private CmsObject m_rootCms;
055
056    /**
057     * public constructor.<p>
058     *
059     * @param variation path to variation file to hold information for
060     */
061    public CmsVariationBean(String variation) {
062
063        m_variationPath = variation;
064        try {
065            m_rootCms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
066            m_rootCms.getRequestContext().setSiteRoot("");
067        } catch (CmsException e) {
068            //
069        }
070
071    }
072
073    /**
074     * Gets the dimensions of the current variation.<p>
075     *
076     * @return String representation of the dimensions
077     */
078    @SuppressWarnings("resource")
079    public String getDimensions() {
080
081        try {
082            BufferedImage img = Simapi.read(IOUtils.toByteArray(new FileInputStream(m_variationPath)));
083            return "" + img.getWidth() + " x " + img.getHeight() + "px";
084        } catch (IOException e) {
085            return "";
086        }
087    }
088
089    /**
090     * Get length of variation.<p>
091     *
092     * @return string representation of length
093     */
094    @SuppressWarnings("resource")
095    public String getLength() {
096
097        try {
098            return CmsFileUtil.formatFilesize(
099                IOUtils.toByteArray(new FileInputStream(m_variationPath)).length,
100                A_CmsUI.get().getLocale());
101        } catch (IOException e) {
102            return "";
103        }
104    }
105
106    /**
107     * Gets path of variation.<p>
108     *
109     * @return path
110     */
111    public String getName() {
112
113        return m_variationPath.substring(CmsImageLoader.getImageRepositoryPath().length());
114    }
115}