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