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; 019 020import org.apache.commons.codec.digest.DigestUtils; 021 022import org.ametys.cms.data.Binary; 023import org.ametys.cms.repository.Content; 024import org.ametys.core.util.FilenameUtils; 025 026/** 027 * Implementation of a {@link AbstractConvertDocument2ImagesComponent} for a binary metadata 028 */ 029public class ConvertMetadata2ImagesComponent extends AbstractConvertDocument2ImagesComponent 030{ 031 /** Avalon ROLE. */ 032 public static final String ROLE = ConvertMetadata2ImagesComponent.class.getName(); 033 034 /** 035 * Put the file in cache 036 * @param content the content 037 * @param dataPath the path of the data 038 * @param siteName the name of the site 039 * @return The absolute cache path 040 * @throws Exception if an error occurs while caching the file 041 */ 042 public String doCache(Content content, String dataPath, String siteName) throws Exception 043 { 044 Binary binary = content.getValue(dataPath); 045 046 String cachePath = _getCacheDirectory(dataPath, binary.getFilename(), content.getName(), siteName); 047 try (InputStream is = binary.getInputStream(); InputStream is2 = binary.getInputStream()) 048 { 049 String md5sum = DigestUtils.md5Hex(is); 050 return cache(cachePath, md5sum, is2, content.getName(), content.getId(), binary.getMimeType()); 051 } 052 } 053 054 private String _getCacheDirectory(String metadataPath, String fileName, String contentName, String siteName) 055 { 056 StringBuilder buff = _getContentCacheDirectory(contentName, siteName); 057 buff.append("/metadatas/"); 058 buff.append(metadataPath); 059 buff.append("/"); 060 buff.append(FilenameUtils.encodeName(fileName)); 061 062 return buff.toString(); 063 } 064 065 private StringBuilder _getContentCacheDirectory(String contentName, String siteName) 066 { 067 StringBuilder buff = new StringBuilder(); 068 buff.append("/"); 069 buff.append(siteName); 070 buff.append("/contents/"); 071 buff.append(contentName); 072 return buff; 073 } 074 075 /** 076 * Remove all cached attribute image for the given content and sitename in the flipbook cache 077 * @param content the content linked to the info to remove 078 * @param siteName the sitename linked to the info to remove 079 */ 080 public void doCleanCache(Content content, String siteName) 081 { 082 // target the metadata cache only to avoid removing the attachment cache if its unnecessary 083 cleanCache(_getContentCacheDirectory(content.getName(), siteName).toString() + "/metadatas"); 084 } 085}