Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
*
* The Apereo Foundation licenses this file to you under the Educational
* Community License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License
* at:
*
* http://opensource.org/licenses/ecl2.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package org.opencastproject.adminui.impl;
import static org.opencastproject.mediapackage.MediaPackageElementFlavor.flavor;
import static org.opencastproject.mediapackage.MediaPackageElementFlavor.parseFlavor;
import org.opencastproject.assetmanager.api.AssetManager;
import org.opencastproject.assetmanager.util.WorkflowPropertiesUtil;
import org.opencastproject.composer.api.ComposerService;
import org.opencastproject.composer.api.EncoderException;
import org.opencastproject.distribution.api.DistributionException;
import org.opencastproject.mediapackage.Attachment;
import org.opencastproject.mediapackage.MediaPackage;
import org.opencastproject.mediapackage.MediaPackageElement;
import org.opencastproject.mediapackage.MediaPackageElementFlavor;
import org.opencastproject.mediapackage.MediaPackageElementParser;
import org.opencastproject.mediapackage.MediaPackageException;
import org.opencastproject.mediapackage.Publication;
import org.opencastproject.mediapackage.Track;
import org.opencastproject.mediapackage.attachment.AttachmentImpl;
import org.opencastproject.publication.api.ConfigurablePublicationService;
import org.opencastproject.publication.api.OaiPmhPublicationService;
import org.opencastproject.publication.api.PublicationException;
import org.opencastproject.security.urlsigning.exception.UrlSigningException;
import org.opencastproject.security.urlsigning.service.UrlSigningService;
import org.opencastproject.util.MimeType;
import org.opencastproject.util.MimeTypes;
import org.opencastproject.util.NotFoundException;
import org.opencastproject.util.UnknownFileTypeException;
import org.opencastproject.util.data.Tuple;
import org.opencastproject.workflow.handler.distribution.InternalPublicationChannel;
import org.opencastproject.workspace.api.Workspace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public final class ThumbnailImpl {
/** Name of the thumbnail type workflow property */
private static final String THUMBNAIL_PROPERTY_TYPE = "thumbnailType";
/** Name of the thumbnail position workflow property */
private static final String THUMBNAIL_PROPERTY_POSITION = "thumbnailPosition";
/** Name of the thumbnail track workflow property */
private static final String THUMBNAIL_PROPERTY_TRACK = "thumbnailTrack";
public enum ThumbnailSource {
DEFAULT(0),
UPLOAD(1),
SNAPSHOT(2);
private final long number;
ThumbnailSource(final long number) {
this.number = number;
}
public long getNumber() {
return number;
}
public static ThumbnailSource byNumber(final long number) {
return Arrays.stream(ThumbnailSource.values()).filter(v -> v.number == number).findFirst().orElse(DEFAULT);
}
}
public static class Thumbnail {
private final ThumbnailSource type;
private final Double position;
private final String track;
private final URI url;
public Thumbnail(final ThumbnailSource type, final Double position, final String track, final URI url) {
this.type = type;
this.position = position;
this.track = track;
this.url = url;
}
public ThumbnailSource getType() {
return type;
}
public OptionalDouble getPosition() {
if (position != null) {
return OptionalDouble.of(position);
} else {
return OptionalDouble.empty();
}
}
public Optional getTrack() {
if (track != null) {
return Optional.of(track);
} else {
return Optional.empty();
}
}
public URI getUrl() {
return url;
}
}
/** The logging facility */
private static final Logger logger = LoggerFactory.getLogger(ThumbnailImpl.class);
private final MediaPackageElementFlavor previewFlavor;
private final String masterProfile;
private final String previewProfile;
private final String previewProfileDownscale;
private final MediaPackageElementFlavor uploadedFlavor;
private final List uploadedTags;
private final Workspace workspace;
private final OaiPmhPublicationService oaiPmhPublicationService;
private final AssetManager assetManager;
private final ConfigurablePublicationService configurablePublicationService;
private final ComposerService composerService;
private final double defaultPosition;
private final String sourceFlavorSubtype;
private final MediaPackageElementFlavor sourceFlavorPrimary;
private final MediaPackageElementFlavor sourceFlavorSecondary;
private String tempThumbnailFileName;
private final String tempThumbnailId;
private URI tempThumbnail;
private MimeType tempThumbnailMimeType;
private AdminUIConfiguration.ThumbnailDistributionSettings distributionOaiPmh;
private AdminUIConfiguration.ThumbnailDistributionSettings distributionConfigurable;
public ThumbnailImpl(final AdminUIConfiguration config, final Workspace workspace,
final OaiPmhPublicationService oaiPmhPublicationService,
final ConfigurablePublicationService configurablePublicationService, final AssetManager assetManager,
final ComposerService composerService) {
this.masterProfile = config.getThumbnailMasterProfile();
this.previewFlavor = parseFlavor(config.getThumbnailPreviewFlavor());
this.previewProfile = config.getThumbnailPreviewProfile();
this.previewProfileDownscale = config.getThumbnailPreviewProfileDownscale();
this.uploadedFlavor = parseFlavor(config.getThumbnailUploadedFlavor());
this.uploadedTags = Arrays.asList(config.getThumbnailUploadedTags().split(","));
this.defaultPosition = config.getThumbnailDefaultPosition();
this.sourceFlavorSubtype = config.getThumbnailSourceFlavorSubtype();
this.sourceFlavorPrimary = flavor(config.getThumbnailSourceFlavorTypePrimary(),
config.getThumbnailSourceFlavorSubtype());
this.sourceFlavorSecondary = flavor(config.getThumbnailSourceFlavorTypeSecondary(),
config.getThumbnailSourceFlavorSubtype());
this.workspace = workspace;
this.oaiPmhPublicationService = oaiPmhPublicationService;
this.assetManager = assetManager;
this.composerService = composerService;
this.configurablePublicationService = configurablePublicationService;
this.tempThumbnail = null;
this.tempThumbnailId = null;
this.tempThumbnailMimeType = null;
this.tempThumbnailFileName = null;
this.distributionOaiPmh = config.getThumbnailDistributionOaiPmh();
this.distributionConfigurable = config.getThumbnailDistributionConfigurable();
}
private Optional getThumbnailPreviewForMediaPackage(final MediaPackage mp) {
final Optional internalPublication = getPublication(mp, InternalPublicationChannel.CHANNEL_ID);
if (internalPublication.isPresent()) {
return Arrays
.stream(internalPublication.get().getAttachments())
.filter(attachment -> previewFlavor.matches(attachment.getFlavor()))
.findFirst();
} else {
throw new IllegalStateException("Expected internal publication, but found none for mp " + mp.getIdentifier());
}
}
public double getDefaultPosition() {
return defaultPosition;
}
public Optional getThumbnail(final MediaPackage mp, final UrlSigningService urlSigningService,
final Long expireSeconds) throws UrlSigningException, URISyntaxException {
final Optional optThumbnail = getThumbnailPreviewForMediaPackage(mp);
if (!optThumbnail.isPresent()) {
return Optional.empty();
}
final Attachment thumbnail = optThumbnail.get();
final URI url;
if (urlSigningService.accepts(thumbnail.getURI().toString())) {
url = new URI(urlSigningService.sign(optThumbnail.get().getURI().toString(), expireSeconds, null, null));
} else {
url = thumbnail.getURI();
}
final Map ps = WorkflowPropertiesUtil
.getLatestWorkflowProperties(assetManager, mp.getIdentifier().toString());
final ThumbnailSource source = ps.entrySet().stream()
.filter(p -> ThumbnailImpl.THUMBNAIL_PROPERTY_TYPE.equals(p.getKey()))
.map(Map.Entry::getValue)
.map(Long::parseLong)
.map(ThumbnailSource::byNumber)
.findAny()
.orElse(ThumbnailSource.DEFAULT);
final Double position = ps.entrySet().stream()
.filter(p -> ThumbnailImpl.THUMBNAIL_PROPERTY_POSITION.equals(p.getKey()))
.map(Map.Entry::getValue)
.map(Double::parseDouble)
.findAny().orElse(defaultPosition);
final String track = ps.entrySet().stream()
.filter(p -> ThumbnailImpl.THUMBNAIL_PROPERTY_TRACK.equals(p.getKey()))
.map(Map.Entry::getValue)
.findAny().orElse(null);
return Optional.of(new Thumbnail(source, position, track, url));
}
public MediaPackageElement upload(final MediaPackage mp, final InputStream inputStream, final String contentType)
throws IOException, NotFoundException, MediaPackageException, PublicationException,
EncoderException, DistributionException {
createTempThumbnail(mp, inputStream, contentType);
final Collection deletionUris = new ArrayList<>(0);
try {
// Archive uploaded thumbnail (and remove old one)
archive(mp);
final MediaPackageElementFlavor trackFlavor = getPrimaryOrSecondaryTrack(mp).getFlavor();
final Tuple> internalPublicationResult = updateInternalPublication(mp, false);
deletionUris.add(internalPublicationResult.getA());
if (distributionConfigurable.getEnabled()) {
deletionUris.add(updateConfigurablePublication(mp, trackFlavor));
}
if (distributionOaiPmh.getEnabled()) {
deletionUris.add(updateOaiPmh(mp, trackFlavor));
}
assetManager.takeSnapshot(mp);
// Set workflow settings: type = UPLOAD
WorkflowPropertiesUtil
.storeProperty(assetManager, mp, THUMBNAIL_PROPERTY_TYPE, Long.toString(ThumbnailSource.UPLOAD.getNumber()));
return internalPublicationResult.getB().get(0);
} finally {
inputStream.close();
workspace.cleanup(mp.getIdentifier());
for (final URI uri : deletionUris) {
if (uri != null) {
workspace.delete(uri);
}
}
}
}
private Track getPrimaryOrSecondaryTrack(final MediaPackage mp) throws MediaPackageException {
final Optional