Package: PhotoItemDIDLAdapter
PhotoItemDIDLAdapter
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PhotoItemDIDLAdapter(PhotoItem) |
|
|
|
|
|
||||||||||||||||||||
computeUrl(int) |
|
|
|
|
|
||||||||||||||||||||
createResource(ProtocolInfo, int) |
|
|
|
|
|
||||||||||||||||||||
dateFor(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$toObject$0(ProtocolInfo, Integer) |
|
|
|
|
|
||||||||||||||||||||
lambda$toObject$1(int) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toObject() |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.service.stoppingdown.impl;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.concurrent.Immutable;
31: import java.util.List;
32: import java.nio.file.Path;
33: import org.fourthline.cling.support.model.DIDLObject;
34: import org.fourthline.cling.support.model.Protocol;
35: import org.fourthline.cling.support.model.ProtocolInfo;
36: import org.fourthline.cling.support.model.Res;
37: import org.fourthline.cling.support.model.dlna.DLNAProtocolInfo;
38: import org.fourthline.cling.support.model.item.Photo;
39: import it.tidalwave.dci.annotation.DciRole;
40: import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
41: import it.tidalwave.bluemarine2.upnp.mediaserver.impl.didl.DIDLAdapter;
42: import lombok.RequiredArgsConstructor;
43: import static java.util.Collections.reverseOrder;
44:
45: /***********************************************************************************************************************
46: *
47: * A role that converts a {@link PhotoItem} into DIDL content.
48: *
49: * @stereotype Role
50: *
51: * @author Fabrizio Giudici
52: *
53: **********************************************************************************************************************/
54: // FIXME: this introduces a dependency on UPnP. It's needed because it contains stuff related to StoppingDown (URLs).
55: // FIXME: move PhotoItem to Model, this class to UPnP and try to make the URLs contained in metadata of PhotoItem.
56: @RequiredArgsConstructor
57: @Immutable @DciRole(datumType = PhotoItem.class)
58: public class PhotoItemDIDLAdapter implements DIDLAdapter
59: {
60: private static final String MEDIA_URL_TEMPLATE =
61: PhotoCollectionProviderSupport.URL_STOPPINGDOWN + "/media/stillimages/%s/%d/image.jpg";
62:
63: private static final List<Integer> SIZES = List.of(200, 400, 800, 1280, 1920, 2560);
64:
65: @Nonnull
66: private final PhotoItem datum;
67:
68: private final String creator = "Fabrizio Giudici";
69:
70: /*******************************************************************************************************************
71: *
72: * {@inheritDoc}
73: *
74: ******************************************************************************************************************/
75: @Override @Nonnull
76: public DIDLObject toObject()
77: {
78: final ProtocolInfo protocolInfo = new DLNAProtocolInfo(Protocol.HTTP_GET, "*", "image/jpeg", "*");
79: final Res[] resources = SIZES.stream()
80: .sorted(reverseOrder())
81: .map(size -> createResource(protocolInfo, size))
82: .toArray(Res[]::new);
83: final Path parentPath = datum.getParent().map(PathAwareEntity::getPath).orElseThrow(RuntimeException::new);
84: final String parentId = parentPath.toString();
85: final String photoId = parentPath.resolve(datum.getId()).toString();
86: final String title = datum.getId();
87: final Photo item = new Photo(photoId, parentId, title, creator, parentId, resources);
88: item.setDescription(datum.getTitle());
89: item.setDate(dateFor(datum.getId()));
90: return item;
91: }
92:
93: /*******************************************************************************************************************
94: *
95: ******************************************************************************************************************/
96: @Nonnull
97: private Res createResource (@Nonnull final ProtocolInfo protocolInfo, final int size)
98: {
99: final Res resource = new Res(protocolInfo, null, computeUrl(size));
100: resource.setResolution(size, size);
101: return resource;
102: }
103:
104: /*******************************************************************************************************************
105: *
106: ******************************************************************************************************************/
107: @Nonnull
108: private String computeUrl (final int size)
109: {
110: return String.format(MEDIA_URL_TEMPLATE, datum.getId(), size);
111: }
112:
113: /*******************************************************************************************************************
114: *
115: ******************************************************************************************************************/
116: @Nonnull
117: private static String dateFor (final String id)
118: {
119: return String.format("%s-%s-%s", id.substring(0, 4), id.substring(4, 6), id.substring(6, 8));
120: }
121: }