001/* 002 * (C) Copyright 2006-2011 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 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.core.api.externalblob; 020 021import java.io.File; 022import java.io.IOException; 023 024import org.nuxeo.ecm.core.api.Blob; 025import org.nuxeo.ecm.core.api.Blobs; 026import org.nuxeo.ecm.core.api.PropertyException; 027 028/** 029 * File system external adapter that takes the "container" property to set the absolute path of the container folder on 030 * the file system. 031 * 032 * @author Anahide Tchertchian 033 */ 034public class FileSystemExternalBlobAdapter extends AbstractExternalBlobAdapter { 035 036 private static final long serialVersionUID = 1L; 037 038 public static final String CONTAINER_PROPERTY_NAME = "container"; 039 040 public String getFileAbsolutePath(String localPath) throws PropertyException { 041 String container = getProperty(CONTAINER_PROPERTY_NAME); 042 if (container == null) { 043 throw new PropertyException(String.format("External blob adapter with prefix '%s' " 044 + "and class '%s' is missing the '%s' property", getPrefix(), getClass().getName(), 045 CONTAINER_PROPERTY_NAME)); 046 } 047 container = container.trim(); 048 if (!container.endsWith(File.separator)) { 049 return String.format("%s%s%s", container, File.separator, localPath); 050 } else { 051 return String.format("%s%s", container, localPath); 052 } 053 } 054 055 @Override 056 public Blob getBlob(String uri) throws PropertyException, IOException { 057 String localPath = getLocalName(uri); 058 String path = getFileAbsolutePath(localPath); 059 File file = new File(path); 060 if (!file.exists()) { 061 throw new PropertyException(String.format("Cannot find file at '%s'", path)); 062 } 063 Blob blob = Blobs.createBlob(file); 064 blob.setFilename(file.getName()); 065 return blob; 066 } 067}