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.util.Optional;
019
020import org.apache.avalon.framework.component.Component;
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.cocoon.Constants;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030
031import org.ametys.cms.content.indexing.solr.SolrResourceGroupedMimeTypes;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.plugins.explorer.resources.Resource;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035import org.ametys.web.URIPrefixHandler;
036import org.ametys.web.WebHelper;
037
038/**
039 * Utility classes for workspaces
040 *
041 */
042public class WorkspacesHelper extends AbstractLogEnabled implements Component, Serviceable, Contextualizable
043{
044    /** Avalon role */
045    public static final String ROLE = WorkspacesHelper.class.getName();
046    
047    /**
048     * Enumeration for file type
049     */
050    public enum FileType 
051    {
052        /** Text document */
053        TEXT,
054        /** PDF document */
055        PDF,
056        /** Spreadsheet */
057        SPREADSHEET,
058        /** Presentation */
059        PRES,
060        /** Image */
061        IMAGE,
062        /** Video */
063        VIDEO,
064        /** Audio */
065        AUDIO,
066        /** Archive */
067        ARCHIVE,
068        /** Unknown type */
069        UNKNOWN
070    }
071
072    private URIPrefixHandler _prefixHandler;
073
074    private Context _context;
075
076    private org.apache.cocoon.environment.Context _cocoonContext;
077    
078    public void contextualize(Context context) throws ContextException
079    {
080        _context = context;
081        _cocoonContext = (org.apache.cocoon.environment.Context) _context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
082    }
083    
084    public void service(ServiceManager smanager) throws ServiceException
085    {
086        _prefixHandler = (URIPrefixHandler) smanager.lookup(URIPrefixHandler.ROLE);
087    }
088    
089    /**
090     * Determines if the resource file is an image
091     * @param file the resource
092     * @return true if the resource file is an image
093     */
094    public boolean isImage(Resource file)
095    {
096        return SolrResourceGroupedMimeTypes.getGroup(file.getMimeType())
097            .map(groupMimeType -> FileType.IMAGE == FileType.valueOf(groupMimeType.toUpperCase()))
098            .orElse(false);
099    }
100    
101    /**
102     * Get the file type of a resource file
103     * @param file the resource
104     * @return the {@link FileType}
105     */
106    public FileType getFileType(Resource file)
107    {
108        Optional<String> group = SolrResourceGroupedMimeTypes.getGroup(file.getMimeType());
109        
110        return group
111                .map(groupMimeType -> FileType.valueOf(groupMimeType.toUpperCase()))
112                .orElse(FileType.UNKNOWN);
113    }
114    
115    /**
116     * Get the file type of a resource from its name
117     * @param filename the resource's name
118     * @return the {@link FileType}
119     */
120    public FileType getFileType(String filename)
121    {
122        String mimeType = _cocoonContext.getMimeType(filename);
123        Optional<String> group = SolrResourceGroupedMimeTypes.getGroup(mimeType);
124        
125        return group
126                .map(groupMimeType -> FileType.valueOf(groupMimeType.toUpperCase()))
127                .orElse(FileType.UNKNOWN);
128    }
129    
130    /**
131     * Get user avatar
132     * @param userIdentity the user identity
133     * @param lang the lang for user's content
134     * @param size the size in pixel of image
135     * @return the avatar url
136     */
137    public String getAvatar(UserIdentity userIdentity, String lang, int size)
138    {
139        StringBuilder sb = new StringBuilder();
140        
141        String siteName = WebHelper.getSiteName(_getRequest());
142        sb.append(_prefixHandler.getUriPrefix(siteName))
143            .append("/_plugins/user-directory/user/")
144            .append(userIdentity.getPopulationId())
145            .append("/")
146            .append(userIdentity.getLogin())
147            .append("/image_")
148            .append(size)
149            .append("?lang=")
150            .append(lang);
151        
152        return sb.toString();
153    }
154    
155    private Request _getRequest()
156    {
157        return ContextHelper.getRequest(_context);
158    }
159}