001/*
002 * (C) Copyright 2015 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
018 */
019package org.nuxeo.ecm.core.blob;
020
021import java.io.FileNotFoundException;
022import java.io.IOException;
023import java.io.InputStream;
024import java.nio.file.Files;
025import java.nio.file.Path;
026import java.nio.file.Paths;
027import java.util.Map;
028
029import org.apache.commons.codec.digest.DigestUtils;
030import org.apache.commons.lang.StringUtils;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.blob.BlobManager.BlobInfo;
034import org.nuxeo.ecm.core.model.Document;
035
036/**
037 * Blob provider that can reference files on the filesystem.
038 * <p>
039 * This blob provider MUST be configured with a "root" property that specifies the minimum root path for all files:
040 *
041 * <pre>
042 * <code>
043 * &lt;blobprovider name="myfsblobprovider">
044 *   &lt;class>org.nuxeo.ecm.core.blob.FilesystemBlobProvider&lt;/class>
045 *   &lt;property name="root">/base/directory/for/files&lt;/property>
046 * &lt;/blobprovider>
047 * </code>
048 * </pre>
049 * <p>
050 * A root of {@code /} may be used to allow any path.
051 * <p>
052 * Blobs are constructed through {@link FilesystemBlobProvider#createBlob}. The constructed blob's key, which will be
053 * stored in the document database, contains a path relative to the root.
054 *
055 * @since 7.10
056 */
057public class FilesystemBlobProvider extends AbstractBlobProvider {
058
059    public static final String ROOT_PROP = "root";
060
061    /** The root ending with /, or an empty string. */
062    protected String root;
063
064    @Override
065    public void initialize(String blobProviderId, Map<String, String> properties) throws IOException {
066        super.initialize(blobProviderId, properties);
067        root = properties.get(ROOT_PROP);
068        if (StringUtils.isBlank(root)) {
069            throw new NuxeoException(
070                    "Missing property '" + ROOT_PROP + "' for " + getClass().getSimpleName() + ": " + blobProviderId);
071        }
072        if ("/".equals(root)) {
073            root = "";
074        } else if (!root.endsWith("/")) {
075            root = root + "/";
076        }
077    }
078
079    @Override
080    public void close() {
081    }
082
083    @Override
084    public Blob readBlob(BlobInfo blobInfo) throws IOException {
085        return new SimpleManagedBlob(blobInfo);
086    }
087
088    @Override
089    public InputStream getStream(ManagedBlob blob) throws IOException {
090        String key = blob.getKey();
091        // strip prefix
092        int colon = key.indexOf(':');
093        if (colon >= 0 && key.substring(0, colon).equals(blobProviderId)) {
094            key = key.substring(colon + 1);
095        }
096        // final sanity checks
097        if (key.contains("..")) {
098            throw new FileNotFoundException("Illegal path: " + key);
099        }
100        return Files.newInputStream(Paths.get(root + key));
101    }
102
103    @Override
104    public boolean supportsUserUpdate() {
105        return supportsUserUpdateDefaultFalse();
106    }
107
108    @Override
109    public String writeBlob(Blob blob, Document doc) throws IOException {
110        throw new UnsupportedOperationException("Writing a blob is not supported");
111    }
112
113    /**
114     * Creates a filesystem blob with the given information.
115     * <p>
116     * The passed {@link BlobInfo} contains information about the blob, and the key is a file path.
117     *
118     * @param blobInfo the blob info where the key is a file path
119     * @return the blob
120     */
121    public ManagedBlob createBlob(BlobInfo blobInfo) throws IOException {
122        String filePath = blobInfo.key;
123        if (filePath.contains("..")) {
124            throw new FileNotFoundException("Illegal path: " + filePath);
125        }
126        if (!filePath.startsWith(root)) {
127            throw new FileNotFoundException("Path is not under configured root: " + filePath);
128        }
129        Path path = Paths.get(filePath);
130        if (!Files.exists(path)) {
131            throw new FileNotFoundException(filePath);
132        }
133        // dereference links
134        while (Files.isSymbolicLink(path)) {
135            // dereference if link
136            path = Files.readSymbolicLink(path);
137            if (!Files.exists(path)) {
138                throw new FileNotFoundException(filePath);
139            }
140        }
141        String relativePath = filePath.substring(root.length());
142        long length = Files.size(path);
143        blobInfo = new BlobInfo(blobInfo); // copy
144        blobInfo.key = blobProviderId + ":" + relativePath;
145        blobInfo.length = Long.valueOf(length);
146        if (blobInfo.filename == null) {
147            blobInfo.filename = Paths.get(filePath).getFileName().toString();
148        }
149        if (blobInfo.digest == null) {
150            try (InputStream in = Files.newInputStream(path)) {
151                blobInfo.digest = DigestUtils.md5Hex(in);
152            }
153        }
154        return new SimpleManagedBlob(blobInfo);
155    }
156
157}