001/* 002 * Copyright 2020 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.cms.content; 017 018import java.io.IOException; 019import java.io.InputStream; 020 021import org.apache.excalibur.source.Source; 022import org.apache.excalibur.source.SourceNotFoundException; 023import org.apache.excalibur.source.SourceValidity; 024import org.apache.excalibur.source.impl.validity.TimeStampValidity; 025 026import org.ametys.cms.data.Resource; 027import org.ametys.cms.data.RichText; 028import org.ametys.cms.repository.Content; 029import org.ametys.core.cocoon.source.NamedSource; 030 031/** 032 * {@link Source} representing a {@link RichText}'s local file. 033 */ 034public class RichTextFileSource implements NamedSource 035{ 036 private Content _content; 037 private RichText _richText; 038 private String _filename; 039 private Resource _resource; 040 041 private String _uri; 042 043 /** 044 * Constructor. 045 * @param content the {@link Content}. 046 * @param richText the {@link RichText}. 047 * @param filename the local file name. 048 * @param uri this source's uri. 049 */ 050 public RichTextFileSource(Content content, RichText richText, String filename, String uri) 051 { 052 _content = content; 053 _richText = richText; 054 _filename = filename; 055 _uri = uri; 056 } 057 058 private Resource _getResource() throws SourceNotFoundException 059 { 060 if (_resource == null) 061 { 062 if (!exists()) 063 { 064 throw new SourceNotFoundException("No rich text resource found at " + _uri); 065 } 066 067 _resource = _richText.getAttachment(_filename); 068 } 069 070 return _resource; 071 } 072 073 public boolean exists() 074 { 075 return _content != null && _richText != null && _richText.hasAttachment(_filename); 076 } 077 078 public InputStream getInputStream() throws IOException, SourceNotFoundException 079 { 080 return _getResource().getInputStream(); 081 } 082 083 public String getURI() 084 { 085 return _uri; 086 } 087 088 public String getScheme() 089 { 090 return RichTextFileSourceFactory.RICHTEXT_FILE_SCHEME; 091 } 092 093 public SourceValidity getValidity() 094 { 095 return new TimeStampValidity(getLastModified()); 096 } 097 098 public String getName() 099 { 100 return _filename; 101 } 102 103 public void refresh() 104 { 105 // does nothing 106 } 107 108 public String getMimeType() 109 { 110 try 111 { 112 return _getResource().getMimeType(); 113 } 114 catch (SourceNotFoundException e) 115 { 116 return null; 117 } 118 } 119 120 public long getContentLength() 121 { 122 try 123 { 124 return _getResource().getLength(); 125 } 126 catch (SourceNotFoundException e) 127 { 128 return -1; 129 } 130 } 131 132 public long getLastModified() 133 { 134 return _content != null ? _content.getLastModified().toInstant().toEpochMilli() : 0; 135 } 136}