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;
023import java.nio.file.Paths;
024
025import org.nuxeo.common.Environment;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.OperationException;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.automation.core.annotations.Context;
030import org.nuxeo.ecm.automation.core.annotations.Operation;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.automation.core.annotations.Param;
033import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.NuxeoPrincipal;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040@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 = {
041        "Blob.ToFile" })
042public class BlobToFile {
043
044    public static final String ID = "Blob.ExportToFS";
045
046    @Context
047    protected OperationContext ctx;
048
049    @Param(name = "directory", required = true)
050    protected String directory;
051
052    @Param(name = "prefix", required = false)
053    protected String prefix;
054
055    protected File root;
056
057    protected void init() {
058        root = new File(directory);
059        root.mkdirs();
060    }
061
062    protected boolean isTargetDirectoryForbidden() {
063        File nuxeoHome = Environment.getDefault().getServerHome().getAbsoluteFile();
064        return Paths.get(directory)
065                    .toAbsolutePath()
066                    .normalize()
067                    .startsWith(nuxeoHome.toPath().toAbsolutePath().normalize());
068    }
069
070    protected File getFile(String name) {
071        return new File(root, prefix != null ? prefix + name : name);
072    }
073
074    protected void writeFile(Blob blob) throws IOException {
075        String name = blob.getFilename();
076        if (name.length() == 0) {
077            name = "blob#" + Integer.toHexString(System.identityHashCode(blob));
078        }
079        // get the output file
080        File file = getFile(name);
081        // use a .tmp extension while writing the blob and rename it when write
082        // is done this is allowing external tools to track when the file becomes
083        // available.
084        File tmp = new File(file.getParentFile(), file.getName() + ".tmp");
085        blob.transferTo(tmp);
086        tmp.renameTo(file);
087    }
088
089    @OperationMethod(collector = BlobCollector.class)
090    public Blob run(Blob blob) throws IOException, OperationException {
091        if (!(ctx.getPrincipal() instanceof NuxeoPrincipal)
092                || !((NuxeoPrincipal) ctx.getPrincipal()).isAdministrator()) {
093            throw new OperationException("Not allowed. You must be administrator to use this operation");
094        }
095        if (isTargetDirectoryForbidden()) {
096            throw new OperationException(
097                    "Not allowed. The target directory is forbidden for this operation (" + directory + ").");
098        }
099        init();
100        writeFile(blob);
101        return blob;
102    }
103
104}