8 |
8 |
*/
|
9 |
9 |
package eu.etaxonomy.cdm.api.service.media;
|
10 |
10 |
|
11 |
|
|
12 |
11 |
import java.awt.Point;
|
13 |
12 |
import java.net.URI;
|
14 |
13 |
import java.net.URISyntaxException;
|
... | ... | |
22 |
21 |
import eu.etaxonomy.cdm.model.media.ImageFile;
|
23 |
22 |
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
24 |
23 |
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
|
24 |
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
|
25 |
25 |
|
|
26 |
/**
|
|
27 |
* Creates new purely volatile {@link MediaRepresentation MediaRepresentations} objects based
|
|
28 |
* a list of {@link MediaUriTransformation} rules. These rules are usually stored in the
|
|
29 |
* per data base {@link PreferencePredicate.MediaRepresentationTransformations MediaRepresentationTransformations} property
|
|
30 |
* (See also {@link MediaToolbox#readTransformations()}).
|
|
31 |
* <p>
|
|
32 |
* These volatile {@link MediaRepresentation MediaRepresentations} objects must not be persisted!
|
|
33 |
*
|
|
34 |
* @author a.kohlbecker
|
|
35 |
* @since Jul 8, 2020
|
|
36 |
*/
|
26 |
37 |
public class MediaUriTransformationProcessor {
|
27 |
38 |
|
28 |
39 |
private static final Logger logger = Logger.getLogger(MediaUriTransformationProcessor.class);
|
... | ... | |
56 |
67 |
return newUris;
|
57 |
68 |
}
|
58 |
69 |
|
59 |
|
protected URI uriTransformation(URI uri, String pathQueryFragment, MediaUriTransformation replacement) throws URISyntaxException {
|
|
70 |
protected URI uriTransformation(URI uri, String pathQueryFragment, MediaUriTransformation replacement)
|
|
71 |
throws URISyntaxException {
|
60 |
72 |
|
61 |
73 |
String newScheme = uri.getScheme();
|
62 |
74 |
String newHost = uri.getHost();
|
... | ... | |
73 |
85 |
if (replacement.getHost() != null) {
|
74 |
86 |
Matcher m = replacement.getHost().searchPattern().matcher(newHost);
|
75 |
87 |
isMatch &= m.find();
|
76 |
|
newHost= m.replaceAll(replacement.getHost().getReplace());
|
|
88 |
newHost = m.replaceAll(replacement.getHost().getReplace());
|
77 |
89 |
}
|
78 |
90 |
// TODO port
|
79 |
91 |
|
... | ... | |
82 |
94 |
isMatch &= m.find();
|
83 |
95 |
newPathQueryFragment = m.replaceAll(replacement.getPathQueryFragment().getReplace());
|
84 |
96 |
}
|
85 |
|
if(isMatch) {
|
|
97 |
if (isMatch) {
|
86 |
98 |
// recombine
|
87 |
99 |
String newURIString = newScheme + "://" + newHost + (newPort > 0 ? ":" + String.valueOf(newPort) : "")
|
88 |
100 |
+ newPathQueryFragment;
|
... | ... | |
94 |
106 |
return uri;
|
95 |
107 |
}
|
96 |
108 |
|
97 |
|
|
98 |
109 |
}
|
99 |
110 |
|
100 |
111 |
protected String buildPathQueryFragment(URI uri) {
|
... | ... | |
109 |
120 |
}
|
110 |
121 |
|
111 |
122 |
@Deprecated
|
112 |
|
public List<MediaRepresentation> makeNewMediaRepresentationsFor(URI uri){
|
|
123 |
public List<MediaRepresentation> makeNewMediaRepresentationsFor(URI uri) {
|
113 |
124 |
|
114 |
125 |
List<MediaRepresentation> repr = new ArrayList<>();
|
115 |
126 |
|
... | ... | |
119 |
130 |
|
120 |
131 |
try {
|
121 |
132 |
URI newUri = uriTransformation(uri, pathQueryFragment, transformation);
|
122 |
|
MediaRepresentation mRepresentation = MediaRepresentation.NewInstance(transformation.getMimeType(), null);
|
|
133 |
MediaRepresentation mRepresentation = MediaRepresentation.NewInstance(transformation.getMimeType(),
|
|
134 |
null);
|
123 |
135 |
MediaRepresentationPart part;
|
124 |
|
if(transformation.getMimeType() != null && transformation.getMimeType().startsWith("image/")) {
|
|
136 |
if (transformation.getMimeType() != null && transformation.getMimeType().startsWith("image/")) {
|
125 |
137 |
part = ImageFile.NewInstance(newUri, null, transformation.getHeight(), transformation.getWidth());
|
126 |
138 |
} else {
|
127 |
139 |
part = MediaRepresentationPart.NewInstance(newUri, null);
|
... | ... | |
137 |
149 |
return repr;
|
138 |
150 |
}
|
139 |
151 |
|
140 |
|
public List<MediaRepresentation> makeNewMediaRepresentationsFor(MediaRepresentationPart part){
|
|
152 |
public List<MediaRepresentation> makeNewMediaRepresentationsFor(MediaRepresentationPart part) {
|
141 |
153 |
|
142 |
154 |
List<MediaRepresentation> repr = new ArrayList<>();
|
143 |
155 |
|
... | ... | |
147 |
159 |
|
148 |
160 |
try {
|
149 |
161 |
URI newUri = uriTransformation(part.getUri(), pathQueryFragment, transformation);
|
150 |
|
if(newUri.equals(part.getUri())){
|
|
162 |
if (newUri.equals(part.getUri())) {
|
151 |
163 |
// transformation did not apply
|
152 |
164 |
continue;
|
153 |
165 |
}
|
154 |
|
MediaRepresentation mRepresentation = MediaRepresentation.NewInstance(transformation.getMimeType(), null);
|
|
166 |
MediaRepresentation mRepresentation = MediaRepresentation.NewInstance(transformation.getMimeType(),
|
|
167 |
null);
|
155 |
168 |
MediaRepresentationPart newPart;
|
156 |
|
if(transformation.getMimeType() != null && transformation.getMimeType().startsWith("image/")) {
|
157 |
|
if(part instanceof ImageFile) {
|
158 |
|
ImageFile originalImageFile = (ImageFile)part;
|
159 |
|
Point newSize = calculateTargetSize(transformation, originalImageFile.getWidth(), originalImageFile.getHeight());
|
|
169 |
if (transformation.getMimeType() != null && transformation.getMimeType().startsWith("image/")) {
|
|
170 |
if (part instanceof ImageFile) {
|
|
171 |
ImageFile originalImageFile = (ImageFile) part;
|
|
172 |
Point newSize = calculateTargetSize(transformation, originalImageFile.getWidth(),
|
|
173 |
originalImageFile.getHeight());
|
160 |
174 |
newPart = ImageFile.NewInstance(newUri, null, newSize.y, newSize.x);
|
161 |
175 |
} else {
|
162 |
|
newPart = ImageFile.NewInstance(newUri, null, transformation.getHeight(), transformation.getWidth());
|
|
176 |
newPart = ImageFile.NewInstance(newUri, null, transformation.getHeight(),
|
|
177 |
transformation.getWidth());
|
163 |
178 |
}
|
164 |
179 |
} else {
|
165 |
180 |
newPart = MediaRepresentationPart.NewInstance(newUri, null);
|
... | ... | |
177 |
192 |
|
178 |
193 |
protected Point calculateTargetSize(MediaUriTransformation trans, Integer originalWidth, Integer originalHeight) {
|
179 |
194 |
|
180 |
|
if(trans.getWidth() == null && trans.getHeight() == null) {
|
|
195 |
if (trans.getWidth() == null && trans.getHeight() == null) {
|
181 |
196 |
return null;
|
182 |
|
} else if(originalWidth == null || originalHeight == null) {
|
|
197 |
} else if (originalWidth == null || originalHeight == null) {
|
183 |
198 |
return new Point(trans.getWidth(), trans.getHeight());
|
184 |
199 |
} else {
|
185 |
200 |
// calculate
|
186 |
|
float originalAspectRatio = ((float)originalWidth/(float)originalHeight);
|
187 |
|
if(trans.getWidth() == null) {
|
188 |
|
return new Point(Math.round((float)trans.getHeight() * originalAspectRatio), trans.getHeight());
|
189 |
|
} else if(trans.getHeight() == null) {
|
190 |
|
return new Point(trans.getWidth(), Math.round((float)trans.getWidth() / originalAspectRatio));
|
191 |
|
} else if(trans.getWidth().equals(trans.getHeight())) {
|
|
201 |
float originalAspectRatio = ((float) originalWidth / (float) originalHeight);
|
|
202 |
if (trans.getWidth() == null) {
|
|
203 |
return new Point(Math.round((float) trans.getHeight() * originalAspectRatio), trans.getHeight());
|
|
204 |
} else if (trans.getHeight() == null) {
|
|
205 |
return new Point(trans.getWidth(), Math.round((float) trans.getWidth() / originalAspectRatio));
|
|
206 |
} else if (trans.getWidth().equals(trans.getHeight())) {
|
192 |
207 |
int extend = trans.getWidth();
|
193 |
|
if(originalWidth.equals(originalHeight)) {
|
|
208 |
if (originalWidth.equals(originalHeight)) {
|
194 |
209 |
return new Point(extend, extend);
|
195 |
|
} else if(originalWidth > originalHeight) {
|
|
210 |
} else if (originalWidth > originalHeight) {
|
196 |
211 |
return new Point(extend, Math.round(extend / originalAspectRatio));
|
197 |
212 |
} else {
|
198 |
213 |
return new Point(Math.round(extend * originalAspectRatio), extend);
|
ref #9134 adding ducumentation for MediaUriTransformationProcessor and Mediatoolbox