001/*
002 * (C) Copyright 2019 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.IOException;
022import java.nio.file.Path;
023import java.util.Map;
024
025import org.apache.logging.log4j.LogManager;
026import org.apache.logging.log4j.Logger;
027
028/**
029 * A simple blob provider storing blobs on the local filesystem.
030 *
031 * @since 11.1
032 */
033public class LocalBlobProvider extends BlobStoreBlobProvider {
034
035    private static final Logger log = LogManager.getLogger(LocalBlobProvider.class);
036
037    protected LocalBlobStoreConfiguration config;
038
039    @Override
040    protected BlobStore getBlobStore(String blobProviderId, Map<String, String> properties) throws IOException {
041        config = new LocalBlobStoreConfiguration(properties);
042        log.info("Registering blob provider '" + blobProviderId + "' using directory: " + config.storageDir.getParent());
043        KeyStrategy keyStrategy = getKeyStrategy();
044        PathStrategy pathStrategy;
045        if (keyStrategy.useDeDuplication()) {
046            pathStrategy = new PathStrategySubDirs(config.storageDir, config.descriptor.depth);
047        } else {
048            pathStrategy = new PathStrategyFlat(config.storageDir);
049        }
050        BlobStore store = newBlobStore("File", keyStrategy, pathStrategy);
051        if (isTransactional()) {
052            PathStrategy transientPathStrategy = new PathStrategyFlat(config.tmpDir);
053            BlobStore transientStore = new LocalBlobStore("File_tmp", keyStrategy, transientPathStrategy);
054            store = new TransactionalBlobStore(store, transientStore);
055        }
056        return store;
057    }
058
059    protected BlobStore newBlobStore(String name, KeyStrategy keyStrategy, PathStrategy pathStrategy) {
060        return new LocalBlobStore(name, keyStrategy, pathStrategy);
061    }
062
063    @Override
064    public void close() {
065        // nothing to do
066    }
067
068    @Override
069    protected String getDigestAlgorithm() {
070        return config.digestConfiguration.digestAlgorithm;
071    }
072
073    // used by DownloadServiceImpl for accelerated download
074    public Path getStorageDir() {
075        return config.storageDir;
076    }
077
078}