001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape;
019
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.io.File;
023import java.io.InputStream;
024import java.net.URI;
025import java.util.Collection;
026
027import javax.jcr.Node;
028import javax.jcr.RepositoryException;
029import org.fcrepo.kernel.api.exception.InvalidChecksumException;
030import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
031import org.fcrepo.kernel.api.services.policy.StoragePolicyDecisionPoint;
032import org.slf4j.Logger;
033
034/**
035 * External binary from a local file
036 *
037 * @author bbpennel
038 * @since 11/29/2017
039 */
040public class LocalFileBinary extends UrlBinary {
041    private static final Logger LOGGER = getLogger(LocalFileBinary.class);
042
043    /**
044     * Constructs a LocalFileBinaryImpl
045     *
046     * @param node node
047     */
048    public LocalFileBinary(final Node node) {
049        super(node);
050    }
051
052    /*
053     * (non-Javadoc)
054     * @see
055     * org.fcrepo.kernel.modeshape.FedoraBinaryImpl#setContent(java.io.InputStream, java.lang.String,
056     * java.util.Collection, java.lang.String, org.fcrepo.kernel.api.services.policy.StoragePolicyDecisionPoint)
057     */
058    @Override
059    public void setContent(final InputStream content, final String contentType,
060                           final Collection<URI> checksums, final String originalFileName,
061                           final StoragePolicyDecisionPoint storagePolicyDecisionPoint)
062            throws UnsupportedOperationException {
063        throw new UnsupportedOperationException(
064                "Cannot call setContent() on local file, call setExternalContent() instead");
065    }
066
067    @Override
068    public void setExternalContent(final String contentType,
069                                   final Collection<URI> checksums, final String originalFileName,
070                                   final String externalHandling, final String externalUrl)
071            throws InvalidChecksumException {
072        try {
073            super.setExternalContent(contentType, checksums, originalFileName, externalHandling, externalUrl);
074
075            final Node descNode = getDescriptionNodeOrNull();
076            // Store the size of the file
077            final long size = getContentSize();
078            if (descNode != null) {
079                descNode.setProperty(CONTENT_SIZE, size);
080            }
081
082            LOGGER.debug("Decorated local file data property at path: {}", descNode.getPath());
083
084        } catch (final RepositoryException e) {
085            throw new RepositoryRuntimeException(e);
086        }
087    }
088
089    @Override
090    public String getMimeType() {
091        return getMimeTypeValue();
092    }
093
094    /*
095     * (non-Javadoc)
096     * @see org.fcrepo.kernel.modeshape.FedoraBinaryImpl#getContentSize()
097     */
098    @Override
099    public long getContentSize() {
100        final long sizeValue = super.getContentSize();
101        if (sizeValue > -1L) {
102            return sizeValue;
103        }
104        return getRemoteContentSize();
105    }
106
107    @Override
108    protected long getRemoteContentSize() {
109        final File file = new File(getResourceUri().getPath());
110        return file.length();
111    }
112}