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.cms.repository.Content;
025import org.ametys.core.util.FilenameUtils;
026import org.ametys.plugins.explorer.resources.Resource;
027
028/**
029 * Implementation of a {@link AbstractConvertDocument2ImagesComponent} for a content attachment
030 */
031public class ConvertContentAttachment2ImagesComponent extends AbstractConvertDocument2ImagesComponent
032{
033    /** Avalon ROLE. */
034    public static final String ROLE = ConvertContentAttachment2ImagesComponent.class.getName();
035    
036    // match a resource stored as a content attachment
037    private static final Pattern __ATTACHMENTS_PATTERN = Pattern.compile("^.*/ametys-internal:sites/([^/]+)/ametys-internal:contents/([^/]+)/ametys-internal:attachments/(.*)$");
038    
039    /**
040     * Put the file in cache
041     * @param resource the resource
042     * @param contentName the name of the content
043     * @param siteName the name of the site
044     * @return The absolute cache path
045     * @throws Exception if an error occurs while caching the file
046     */
047    public String doCache(Resource resource, String contentName, String siteName) throws Exception
048    {
049        // Ensure the base folder exists.
050        String cachePath = _getCacheDirectory (resource.getResourcePath(), contentName, siteName);
051        
052        try (InputStream is = resource.getInputStream(); InputStream is2 = resource.getInputStream())
053        {
054            String md5sum = DigestUtils.md5Hex(is);
055            return cache(cachePath, md5sum, is2, resource.getName(), resource.getId(), resource.getMimeType());
056        }
057    }
058    
059    private String _getCacheDirectory(String resourcePath, String contentName, String siteName)
060    {
061        // Content attachments.
062        StringBuilder buff = _getContentAttachmentsCacheDirectory(contentName, siteName);
063        buff.append(FilenameUtils.encodePath(resourcePath));
064        
065        return buff.toString();
066    }
067    
068    private StringBuilder _getContentAttachmentsCacheDirectory(String contentName, String siteName)
069    {
070        StringBuilder buff = new StringBuilder();
071        
072        buff.append("/");
073        buff.append(siteName);
074        buff.append("/contents/");
075        buff.append(contentName);
076        buff.append("/attachments");
077        
078        return buff;
079    }
080
081    /**
082     * Remove content attachments image stored in the flipbook cache for the given content and sitename
083     * @param content the linked content
084     * @param siteName the sitename
085     */
086    public void doCleanCache(Content content, String siteName)
087    {
088        cleanCache(_getContentAttachmentsCacheDirectory(content.getName(), siteName).toString());
089    }
090
091    /**
092     * Remove the image stored in cache that are associated with the resource path.
093     * If the provided resource path doesn't point to a content attachment, calling this method will have no effect.
094     * @param path the JCR path of the resource to clean
095     */
096    public void doCleanCache(String path)
097    {
098        Matcher attachmentMatcher = __ATTACHMENTS_PATTERN.matcher(path);
099
100        if (attachmentMatcher.matches())
101        {
102            cleanCache(_getCacheDirectory("/" + attachmentMatcher.group(3), attachmentMatcher.group(2), attachmentMatcher.group(1)));
103        }
104    }
105}