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.actions; 017 018import java.util.Arrays; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Optional; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.cocoon.acting.ServiceableAction; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Redirector; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.environment.SourceResolver; 029 030import org.ametys.cms.content.indexing.solr.SolrResourceGroupedMimeTypes; 031import org.ametys.plugins.workspaces.WorkspacesHelper.FileType; 032 033/** 034 * Get file icon action from mime type 035 */ 036public class GetFileIconAction extends ServiceableAction 037{ 038 /** MIME types for archive */ 039 protected static final String[] ARCHIVE_MIMETYPE = new String [] 040 { 041 "application/x-rar-compressed", 042 "application/octet-stream", 043 "application/zip", 044 "application/x-zip", 045 "application/octet-stream", 046 "application/x-zip-compressed", 047 "multipart/x-zip", 048 "application/tar", 049 "application/x-tar", 050 "application/x-gtar", 051 "multipart/x-tar", 052 "application/x-compress", 053 "application/x-compressed", 054 "application/gzip", 055 "application/x-gzip", 056 "application/x-gtar", 057 "application/x-tgz" 058 }; 059 060 061 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 062 { 063 Map<String, Object> result = new HashMap<>(); 064 065 Request request = ObjectModelHelper.getRequest(objectModel); 066 067 String mimeType = request.getParameter("mimeType"); 068 if (Arrays.asList(ARCHIVE_MIMETYPE).contains(mimeType)) 069 { 070 result.put("type", "archive"); 071 } 072 else 073 { 074 Optional<String> group = SolrResourceGroupedMimeTypes.getGroup(mimeType); 075 076 FileType type = group 077 .map(groupMimeType -> FileType.valueOf(groupMimeType.toUpperCase())) 078 .orElse(FileType.UNKNOWN); 079 080 result.put("type", type.name().toLowerCase()); 081 } 082 return result; 083 } 084}