001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (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.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
029
030/**
031 * A simple filesystem-based binary manager. It stores the binaries according to their digest (hash), which means that
032 * no transactional behavior needs to be implemented.
033 * <p>
034 * A garbage collection is needed to purge unused binaries.
035 * <p>
036 * The format of the <em>binaries</em> directory is:
037 * <ul>
038 * <li><em>data/</em> hierarchy with the actual binaries in subdirectories,</li>
039 * <li><em>tmp/</em> temporary storage during creation,</li>
040 * <li><em>config.xml</em> a file containing the configuration used.</li>
041 * </ul>
042 *
043 * @author Florent Guillaume
044 */
045public class DefaultBinaryManager extends LocalBinaryManager {
046
047    @Override
048    public Binary getBinary(Blob blob) throws IOException {
049        if (!(blob instanceof FileBlob) || !((FileBlob) blob).isTemporary()) {
050            return super.getBinary(blob); // just open the stream
051        }
052        String digest = storeAndDigest((FileBlob) blob);
053        File file = getFileForDigest(digest, false);
054        /*
055         * Now we can build the Binary.
056         */
057        return new Binary(file, digest, blobProviderId);
058    }
059
060    /**
061     * Stores and digests a temporary FileBlob.
062     */
063    protected String storeAndDigest(FileBlob blob) throws IOException {
064        String digest;
065        try (InputStream in = blob.getStream()) {
066            digest = storeAndDigest(in, NullOutputStream.NULL_OUTPUT_STREAM);
067        }
068        File digestFile = getFileForDigest(digest, true);
069        if (digestFile.exists()) {
070            // The file with the proper digest is already there so don't do anything. This is to avoid
071            // "Stale NFS File Handle" problems which would occur if we tried to overwrite it anyway.
072            // Note that this doesn't try to protect from the case where two identical files are uploaded
073            // at the same time.
074            // Update date for the GC.
075            digestFile.setLastModified(blob.getFile().lastModified());
076        } else {
077            blob.moveTo(digestFile);
078        }
079        return digest;
080    }
081
082}