001/*
002 *  Copyright 2020 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.workspaces;
017
018import java.awt.image.BufferedImage;
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.Optional;
022
023import javax.imageio.ImageIO;
024
025import org.apache.avalon.framework.component.Component;
026import org.apache.avalon.framework.context.Context;
027import org.apache.avalon.framework.context.ContextException;
028import org.apache.avalon.framework.context.Contextualizable;
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031import org.apache.avalon.framework.service.Serviceable;
032import org.apache.cocoon.components.ContextHelper;
033import org.apache.cocoon.environment.Request;
034
035import org.ametys.cms.content.indexing.solr.SolrResourceGroupedMimeTypes;
036import org.ametys.core.user.UserIdentity;
037import org.ametys.plugins.explorer.resources.Resource;
038import org.ametys.runtime.plugin.component.AbstractLogEnabled;
039import org.ametys.web.URIPrefixHandler;
040import org.ametys.web.WebHelper;
041
042/**
043 * Utility classes for workspaces
044 *
045 */
046public class WorkspacesHelper extends AbstractLogEnabled implements Component, Serviceable, Contextualizable
047{
048    /** Avalon role */
049    public static final String ROLE = WorkspacesHelper.class.getName();
050    
051    /**
052     * Enumeration for file type
053     */
054    public enum FileType 
055    {
056        /** Text document */
057        TEXT,
058        /** PDF document */
059        PDF,
060        /** Spreadsheet */
061        SPREADSHEET,
062        /** Presentation */
063        PRES,
064        /** Image */
065        IMAGE,
066        /** Video */
067        VIDEO,
068        /** Audio */
069        AUDIO,
070        /** Archive */
071        ARCHIVE,
072        /** Unknown type */
073        UNKNOWN
074    }
075
076    private URIPrefixHandler _prefixHandler;
077
078    private Context _context;
079    
080    public void contextualize(Context context) throws ContextException
081    {
082        _context = context;
083    }
084    
085    public void service(ServiceManager smanager) throws ServiceException
086    {
087        _prefixHandler = (URIPrefixHandler) smanager.lookup(URIPrefixHandler.ROLE);
088    }
089    
090    /**
091     * Determines if the resource file is an image
092     * @param file the resource
093     * @return true if the resource file is an image
094     */
095    public boolean isImage(Resource file)
096    {
097        return SolrResourceGroupedMimeTypes.getGroup(file.getMimeType())
098            .map(groupMimeType -> FileType.IMAGE == FileType.valueOf(groupMimeType.toUpperCase()))
099            .orElse(false);
100    }
101    
102    /**
103     * Get the buffered image from the resource
104     * @param file the resource
105     * @return the buffered image or null if the resource is not a image or if failed to read image
106     */
107    public BufferedImage getImage(Resource file)
108    {
109        if (isImage(file))
110        {
111            try (InputStream is = file.getInputStream())
112            {
113                return ImageIO.read(is);
114            }
115            catch (IOException e)
116            {
117                getLogger().error("Unable to read image for file {}", file.getName());
118            }
119        }
120        
121        return null;
122    }
123    
124    /**
125     * Get the file type of a resource file
126     * @param file the resource
127     * @return the {@link FileType}
128     */
129    public FileType getFileType(Resource file)
130    {
131        Optional<String> group = SolrResourceGroupedMimeTypes.getGroup(file.getMimeType());
132        
133        return group
134                .map(groupMimeType -> FileType.valueOf(groupMimeType.toUpperCase()))
135                .orElse(FileType.UNKNOWN);
136    }
137    
138    /**
139     * Get user avatar
140     * @param userIdentity the user identity
141     * @param lang the lang for user's content
142     * @param size the size in pixel of image
143     * @return the avatar url
144     */
145    public String getAvatar(UserIdentity userIdentity, String lang, int size)
146    {
147        StringBuilder sb = new StringBuilder();
148        
149        String siteName = WebHelper.getSiteName(_getRequest());
150        sb.append(_prefixHandler.getUriPrefix(siteName))
151            .append("/_plugins/workspaces/user/")
152            .append(userIdentity.getPopulationId())
153            .append("/")
154            .append(userIdentity.getLogin())
155            .append("/image_")
156            .append(size)
157            .append("?lang=")
158            .append(lang);
159        
160        return sb.toString();
161    }
162    
163    private Request _getRequest()
164    {
165        return ContextHelper.getRequest(_context);
166    }
167}