001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Anahide Tchertchian 011 */ 012package org.nuxeo.ecm.core.api.externalblob; 013 014import java.io.File; 015import java.io.IOException; 016 017import org.nuxeo.ecm.core.api.Blob; 018import org.nuxeo.ecm.core.api.Blobs; 019import org.nuxeo.ecm.core.api.PropertyException; 020 021/** 022 * File system external adapter that takes the "container" property to set the absolute path of the container folder on 023 * the file system. 024 * 025 * @author Anahide Tchertchian 026 */ 027public class FileSystemExternalBlobAdapter extends AbstractExternalBlobAdapter { 028 029 private static final long serialVersionUID = 1L; 030 031 public static final String CONTAINER_PROPERTY_NAME = "container"; 032 033 public String getFileAbsolutePath(String localPath) throws PropertyException { 034 String container = getProperty(CONTAINER_PROPERTY_NAME); 035 if (container == null) { 036 throw new PropertyException(String.format("External blob adapter with prefix '%s' " 037 + "and class '%s' is missing the '%s' property", getPrefix(), getClass().getName(), 038 CONTAINER_PROPERTY_NAME)); 039 } 040 container = container.trim(); 041 if (!container.endsWith(File.separator)) { 042 return String.format("%s%s%s", container, File.separator, localPath); 043 } else { 044 return String.format("%s%s", container, localPath); 045 } 046 } 047 048 @Override 049 public Blob getBlob(String uri) throws PropertyException, IOException { 050 String localPath = getLocalName(uri); 051 String path = getFileAbsolutePath(localPath); 052 File file = new File(path); 053 if (!file.exists()) { 054 throw new PropertyException(String.format("Cannot find file at '%s'", path)); 055 } 056 Blob blob = Blobs.createBlob(file); 057 blob.setFilename(file.getName()); 058 return blob; 059 } 060}