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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.operations.blob;
020
021import java.io.File;
022import java.io.IOException;
023
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
029import org.nuxeo.ecm.core.api.Blob;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034@Operation(id = BlobToFile.ID, category = Constants.CAT_BLOB, label = "Export to File", description = "Save the input blob(s) as a file(s) into the given target directory. The blob(s) filename is used as the file name. You can specify an optional <b>prefix</b> string to prepend to the file name. Return back the blob(s).", aliases = { "Blob.ToFile" })
035public class BlobToFile {
036
037    public static final String ID = "Blob.ExportToFS";
038
039    @Param(name = "directory", required = true)
040    protected String directory;
041
042    @Param(name = "prefix", required = false)
043    protected String prefix;
044
045    protected File root;
046
047    protected void init() {
048        root = new File(directory);
049        root.mkdirs();
050    }
051
052    protected File getFile(String name) {
053        return new File(root, prefix != null ? prefix + name : name);
054    }
055
056    protected void writeFile(Blob blob) throws IOException {
057        String name = blob.getFilename();
058        if (name.length() == 0) {
059            name = "blob#" + Integer.toHexString(System.identityHashCode(blob));
060        }
061        // get the output file
062        File file = getFile(name);
063        // use a .tmp extension while writing the blob and rename it when write
064        // is done this is allowing external tools to track when the file becomes
065        // available.
066        File tmp = new File(file.getParentFile(), file.getName() + ".tmp");
067        blob.transferTo(tmp);
068        tmp.renameTo(file);
069    }
070
071    @OperationMethod(collector = BlobCollector.class)
072    public Blob run(Blob blob) throws IOException {
073        init();
074        writeFile(blob);
075        return blob;
076    }
077
078}