001/* 002 * Copyright 2014 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.flipbook; 017 018import java.io.InputStream; 019import java.util.regex.Matcher; 020import java.util.regex.Pattern; 021 022import org.apache.commons.codec.digest.DigestUtils; 023 024import org.ametys.core.util.FilenameUtils; 025import org.ametys.plugins.explorer.resources.Resource; 026import org.ametys.web.explorer.ResourceHelper; 027 028/** 029 * Implementation of a {@link AbstractConvertDocument2ImagesComponent} for a {@link Resource} 030 */ 031public class ConvertResource2ImagesComponent extends AbstractConvertDocument2ImagesComponent 032{ 033 /** Avalon ROLE. */ 034 public static final String ROLE = ConvertResource2ImagesComponent.class.getName(); 035 036 // match a resource in site 037 private static final Pattern __RESOURCE_PATTERN = Pattern.compile("^.*/ametys-internal:sites/([^/]+)/ametys-internal:resources/(.*)$"); 038 // match a resource in site but with hashed site 039 private static final Pattern __ROOT_SITE_RESOURCE_PATTERN = Pattern.compile("^.*/ametys-internal:sites/[^/]+/[^/]+/([^/]+)/ametys-internal:resources/(.*)$"); 040 // match a resource shared 041 private static final Pattern __SHARED_RESOURCE_PATTERN = Pattern.compile("^.*/ametys:plugins/web-explorer/shared-resources/(.*)$"); 042 043 /** 044 * Put the file in cache 045 * @param resource the resource to cache 046 * @param siteName the name of the site 047 * @return The absolute cache path 048 * @throws Exception if an error occurs while caching the file 049 */ 050 public String doCache(Resource resource, String siteName) throws Exception 051 { 052 // Ensure the base folder exists. 053 String cachePath = _getCacheDirectory(resource, siteName); 054 055 try (InputStream is = resource.getInputStream(); InputStream is2 = resource.getInputStream()) 056 { 057 String md5sum = DigestUtils.md5Hex(is); 058 return cache(cachePath, md5sum, is2, resource.getName(), resource.getId(), resource.getMimeType()); 059 } 060 } 061 062 private String _getCacheDirectory(Resource resource, String siteName) 063 { 064 065 String fullPath = resource.getPath(); 066 if (fullPath.startsWith(ResourceHelper.SHARED_RESOURCE_PATH)) 067 { 068 // Shared resources. 069 return "/shared-resources" + FilenameUtils.encodePath(fullPath.substring(ResourceHelper.SHARED_RESOURCE_PATH.length())); 070 } 071 072 StringBuilder buff = new StringBuilder(); 073 074 // Non-shared resources. 075 buff.append("/"); 076 buff.append(siteName); 077 buff.append("/resources"); 078 079 buff.append(FilenameUtils.encodePath(resource.getResourcePath())); 080 081 return buff.toString(); 082 } 083 084 /** 085 * Remove a resource from the flipbook cache. 086 * This method will first try to determine if the provided path either point to shared resource or a site resource. 087 * If it's neither, the method won't do anything as this component only handle resource 088 * @param path the JCR path of the resource 089 * @param resourcePath the resource path (as return by {@link Resource}:getResourcePath) 090 */ 091 public void doCleanCache(String path, String resourcePath) 092 { 093 Matcher resourceMatcher = __RESOURCE_PATTERN.matcher(path); 094 Matcher rootSiteResourceMatcher = __ROOT_SITE_RESOURCE_PATTERN.matcher(path); 095 Matcher sharedMatcher = __SHARED_RESOURCE_PATTERN.matcher(path); 096 097 String cachePath = null; 098 if (resourceMatcher.matches()) 099 { 100 // call ResourceImageComponent 101 cachePath = "/" + resourceMatcher.group(1) + "/resources" + FilenameUtils.encodePath(resourcePath); 102 } 103 else if (rootSiteResourceMatcher.matches()) 104 { 105 // call ResourceImageComponent 106 cachePath = "/" + rootSiteResourceMatcher.group(1) + "/resources" + FilenameUtils.encodePath(resourcePath); 107 } 108 else if (sharedMatcher.matches()) 109 { 110 // call ResourceImageComponent 111 cachePath = "/shared-resources" + FilenameUtils.encodePath(path.substring(ResourceHelper.SHARED_RESOURCE_PATH.length())); 112 } 113 if (cachePath != null) 114 { 115 cleanCache(cachePath); 116 } 117 } 118}