001/*
002 * (C) Copyright 2006-2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume, jcarsique
018 */
019
020package org.nuxeo.ecm.core.blob.binary;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025
026import org.apache.commons.io.output.NullOutputStream;
027import org.apache.commons.lang3.StringUtils;
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
030
031/**
032 * A simple filesystem-based binary manager. It stores the binaries according to their digest (hash), which means that
033 * no transactional behavior needs to be implemented.
034 * <p>
035 * A garbage collection is needed to purge unused binaries.
036 * <p>
037 * The format of the <em>binaries</em> directory is:
038 * <ul>
039 * <li><em>data/</em> hierarchy with the actual binaries in subdirectories,</li>
040 * <li><em>tmp/</em> temporary storage during creation,</li>
041 * <li><em>config.xml</em> a file containing the configuration used.</li>
042 * </ul>
043 *
044 * @author Florent Guillaume
045 */
046public class DefaultBinaryManager extends LocalBinaryManager {
047
048    @Override
049    public Binary getBinary(Blob blob) throws IOException {
050        if (!(blob instanceof FileBlob) || !((FileBlob) blob).isTemporary()) {
051            return super.getBinary(blob); // just open the stream
052        }
053        String digest = storeAndDigest((FileBlob) blob);
054        File file = getFileForDigest(digest, false);
055        /*
056         * Now we can build the Binary.
057         */
058        return new Binary(file, digest, blobProviderId);
059    }
060
061    /**
062     * Stores and digests a temporary FileBlob.
063     */
064    protected String storeAndDigest(FileBlob blob) throws IOException {
065        String digest;
066        if (StringUtils.isEmpty(blob.getDigest())) {
067            try (InputStream in = blob.getStream()) {
068                digest = storeAndDigest(in, NullOutputStream.NULL_OUTPUT_STREAM);
069            }
070        } else {
071            digest = blob.getDigest();
072        }
073        File digestFile = getFileForDigest(digest, true);
074        if (digestFile.exists()) {
075            // The file with the proper digest is already there so don't do anything. This is to avoid
076            // "Stale NFS File Handle" problems which would occur if we tried to overwrite it anyway.
077            // Note that this doesn't try to protect from the case where two identical files are uploaded
078            // at the same time.
079            // Update date for the GC.
080            digestFile.setLastModified(blob.getFile().lastModified());
081        } else {
082            blob.moveTo(digestFile);
083        }
084        return digest;
085    }
086
087}