Skip to content

Package: DefaultMediaScanner

DefaultMediaScanner

nameinstructionbranchcomplexitylinemethod
DefaultMediaScanner()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
afterPropertiesSet()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
destroy()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$onInternalMediaFolderScanRequest$0(PathAwareEntity)
M: 0 C: 31
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 7
100%
M: 0 C: 1
100%
onInternalMediaFolderScanRequest(InternalMediaFolderScanRequest)
M: 5 C: 15
75%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
onMediaItemImportRequest(MediaItemImportRequest)
M: 0 C: 24
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
process(MediaFolder)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
sha1Of(Path)
M: 0 C: 39
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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.mediascanner.impl;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import java.util.Optional;
32: import java.util.concurrent.Semaphore;
33: import java.io.File;
34: import java.io.IOException;
35: import java.io.RandomAccessFile;
36: import java.nio.MappedByteBuffer;
37: import java.nio.file.Path;
38: import java.security.MessageDigest;
39: import java.security.NoSuchAlgorithmException;
40: import it.tidalwave.util.annotation.VisibleForTesting;
41: import it.tidalwave.messagebus.MessageBus;
42: import it.tidalwave.messagebus.annotation.ListensTo;
43: import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
44: import it.tidalwave.bluemarine2.model.MediaFolder;
45: import it.tidalwave.bluemarine2.model.MediaItem;
46: import lombok.extern.slf4j.Slf4j;
47: import static java.nio.channels.FileChannel.MapMode.READ_ONLY;
48: import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
49: import static it.tidalwave.bluemarine2.util.PathNormalization.fixedPath;
50:
51: /***********************************************************************************************************************
52: *
53: * @author Fabrizio Giudici
54: *
55: **********************************************************************************************************************/
56: @SimpleMessageSubscriber @Slf4j
57: public class DefaultMediaScanner
58: {
59: private static final String ALGORITHM = "SHA1";
60:
61: @Inject
62: private ProgressHandler progress;
63:
64: @Inject
65: private MessageBus messageBus;
66:
67: // With magnetic disks it's better to access files one at a time
68: private final Optional<Semaphore> diskSemaphore = Optional.empty(); // new Semaphore(1);
69:
70: /*******************************************************************************************************************
71: *
72: * Processes a folder of {@link MediaItem}s.
73: *
74: * @param folder the folder
75: *
76: ******************************************************************************************************************/
77: public void process (@Nonnull final MediaFolder folder)
78: {
79: log.info("process({})", folder);
80: // shared.reset();
81: progress.reset();
82: progress.incrementTotalFolders();
83: messageBus.publish(new InternalMediaFolderScanRequest(folder));
84: }
85:
86: /*******************************************************************************************************************
87: *
88: * Scans a folder of {@link MediaItem}s.
89: *
90: ******************************************************************************************************************/
91: @VisibleForTesting
92: void onInternalMediaFolderScanRequest
93: (@ListensTo @Nonnull final InternalMediaFolderScanRequest request)
94: {
95: try
96: {
97: log.info("onInternalMediaFolderScanRequest({})", request);
98:
99: request.getFolder().findChildren().stream().forEach(item ->
100: {
101:• if (item instanceof MediaItem)
102: {
103: progress.incrementTotalMediaItems();
104: messageBus.publish(new MediaItemImportRequest((MediaItem)item, Optional.empty()));
105: }
106:
107:• else if (item instanceof MediaFolder)
108: {
109: progress.incrementTotalFolders();
110: messageBus.publish(new InternalMediaFolderScanRequest((MediaFolder)item));
111: }
112: });
113: }
114: catch (Exception e)
115: {
116: log.error("", e);
117: }
118: finally
119: {
120: progress.incrementScannedFolders();
121: }
122: }
123:
124: /*******************************************************************************************************************
125: *
126: *
127: ******************************************************************************************************************/
128: /* VisibleForTesting */ void onMediaItemImportRequest (@ListensTo @Nonnull final MediaItemImportRequest request)
129: throws InterruptedException, NoSuchAlgorithmException, IOException
130: {
131:• if (request.getSha1().isEmpty())
132: {
133: final byte[] sha1 = sha1Of(request.getMediaItem().getPath());
134: messageBus.publish(new MediaItemImportRequest(request.getMediaItem(), Optional.of(sha1)));
135: progress.incrementDoneFingerprints();
136: }
137: }
138:
139: /*******************************************************************************************************************
140: *
141: *
142: ******************************************************************************************************************/
143: @Nonnull
144: public byte[] sha1Of (@Nonnull final Path path)
145: throws InterruptedException, NoSuchAlgorithmException, IOException
146: {
147: try
148: {
149: diskSemaphore.ifPresent(_c(Semaphore::acquire));
150: final File file = fixedPath(path).toFile();
151:
152: try (final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"))
153: {
154: final MappedByteBuffer byteBuffer = randomAccessFile.getChannel().map(READ_ONLY, 0, file.length());
155: final MessageDigest digestComputer = MessageDigest.getInstance(ALGORITHM);
156: digestComputer.update(byteBuffer);
157: return digestComputer.digest();
158: }
159: }
160: finally
161: {
162: diskSemaphore.ifPresent(_c(Semaphore::release));
163: }
164: }
165: }