Package: DefaultMetadataLoader
DefaultMetadataLoader
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultMetadataLoader() |
|
|
|
|
|
||||||||||||||||||||
findMedia(Id, ResourceProperties) |
|
|
|
|
|
||||||||||||||||||||
findMediaResourceFile(ResourceProperties, Id) |
|
|
|
|
|
||||||||||||||||||||
lambda$findMedia$0(Id, String) |
|
|
|
|
|
||||||||||||||||||||
lambda$findMedia$1(Site, String) |
|
|
|
|
|
||||||||||||||||||||
loadMetadata(ResourceFile) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.media.impl;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import javax.inject.Provider;
32: import java.util.Optional;
33: import java.io.IOException;
34: import it.tidalwave.util.Id;
35: import it.tidalwave.util.NotFoundException;
36: import it.tidalwave.image.EditableImage;
37: import it.tidalwave.image.op.ReadOp;
38: import it.tidalwave.northernwind.core.model.Media;
39: import it.tidalwave.northernwind.core.model.Resource;
40: import it.tidalwave.northernwind.core.model.ResourceFile;
41: import it.tidalwave.northernwind.core.model.ResourceProperties;
42: import it.tidalwave.northernwind.core.model.SiteProvider;
43: import lombok.extern.slf4j.Slf4j;
44: import static it.tidalwave.image.op.ReadOp.Type.METADATA;
45: import static java.util.Collections.emptyList;
46: import static it.tidalwave.northernwind.core.model.Media._Media_;
47: import static it.tidalwave.northernwind.frontend.media.impl.EmbeddedMediaMetadataProvider.*;
48:
49: /***********************************************************************************************************************
50: *
51: * The default implementation of {@link MetadataLoader}.
52: *
53: * @author Fabrizio Giudici
54: *
55: **********************************************************************************************************************/
56: @Slf4j
57: public class DefaultMetadataLoader implements MetadataLoader
58: {
59: @Inject
60: private Provider<SiteProvider> siteProvider;
61:
62: /*******************************************************************************************************************
63: *
64: * {@inheritDoc}
65: *
66: ******************************************************************************************************************/
67: @Override @Nonnull
68: public ResourceFile findMediaResourceFile (@Nonnull final ResourceProperties siteNodeProperties,
69: @Nonnull final Id mediaId)
70: throws NotFoundException
71: {
72: final var properties = siteNodeProperties.getGroup(P_GROUP_ID);
73: return findMedia(mediaId, properties).map(Resource::getFile).orElseThrow(NotFoundException::new); // FIXME
74: }
75:
76: /*******************************************************************************************************************
77: *
78: * {@inheritDoc}
79: *
80: ******************************************************************************************************************/
81: @Override @Nonnull
82: public Metadata loadMetadata (@Nonnull final ResourceFile file)
83: throws IOException
84: {
85: log.debug("loadMetadata({})", file.getPath());
86: final var image = EditableImage.create(new ReadOp(file.toFile(), METADATA));
87: return new DefaultMetadata(file.getName(), image);
88: }
89:
90: /*******************************************************************************************************************
91: *
92: * Finds a {@link Media} item for the given id.
93: *
94: * @param mediaId the media id
95: * @param properties the configuration properties
96: * @return the {@code Media}
97: *
98: ******************************************************************************************************************/
99: @Nonnull
100: private Optional<Media> findMedia (@Nonnull final Id mediaId, @Nonnull final ResourceProperties properties)
101: {
102: final var site = siteProvider.get().getSite();
103:
104: return properties.getProperty(P_MEDIA_PATHS).orElse(emptyList())
105: .stream()
106: .map(pathTemplate -> String.format(pathTemplate, mediaId.stringValue()))
107: .flatMap(path -> site.find(_Media_).withRelativePath(path).stream())
108: .findFirst();
109: }
110: }