001/*
002 *  Copyright 2021 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.documents.onlyoffice;
017
018import java.io.File;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.thread.ThreadSafe;
025import org.apache.cocoon.acting.ServiceableAction;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.SourceResolver;
028
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.core.util.URIUtils;
031
032/**
033 * Get the file thumbnail
034 */
035public class GetThumbnailAction extends ServiceableAction implements ThreadSafe
036{
037    /** The only office manager */
038    protected OnlyOfficeManager _onlyOfficeManager;
039    
040    /** The current user provider */
041    protected CurrentUserProvider _currentUserProvider;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _onlyOfficeManager = (OnlyOfficeManager) smanager.lookup(OnlyOfficeManager.ROLE);
048        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
049    }
050    
051    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
052    {
053        String projectName = parameters.getParameter("projectName");
054        String resourceId = parameters.getParameter("resourceId");
055
056        try
057        {
058            Map<String, String> info = _getThumbnailInfo(projectName, resourceId);
059            if (info != null)
060            {
061                return info;
062            }
063            
064            _onlyOfficeManager.generateThumbnailInCache(projectName, resourceId, _currentUserProvider.getUser());
065            
066            return _getThumbnailInfo(projectName, resourceId);
067        }
068        catch (Exception e) 
069        {
070            getLogger().error("An error occurred getting thumbnail for project '" + projectName + "' and resourceId '" + resourceId + "'", e);
071        }
072        
073        return null;
074    }
075    
076    private Map<String, String> _getThumbnailInfo(String projectName, String resourceId)
077    {
078        File thumbnailFile = _onlyOfficeManager.getThumbnailFile(projectName, resourceId);
079        if (thumbnailFile != null && thumbnailFile.exists())
080        {
081            return Map.of("thumbnail-path", URIUtils.encodePath(thumbnailFile.getAbsolutePath()));
082        }
083        
084        return null;
085    }
086}