001/*
002 * (C) Copyright 2006-2010 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.shell.automation.cmds;
020
021import java.io.File;
022
023import org.nuxeo.ecm.automation.client.model.DocRef;
024import org.nuxeo.ecm.automation.client.model.FileBlob;
025import org.nuxeo.shell.Argument;
026import org.nuxeo.shell.Command;
027import org.nuxeo.shell.Context;
028import org.nuxeo.shell.Parameter;
029import org.nuxeo.shell.ShellConsole;
030import org.nuxeo.shell.ShellException;
031import org.nuxeo.shell.automation.DocRefCompletor;
032import org.nuxeo.shell.automation.RemoteContext;
033import org.nuxeo.shell.fs.FileSystem;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038@Command(name = "getfile", help = "Get a document attached file")
039public class GetBlob implements Runnable {
040
041    @Context
042    protected RemoteContext ctx;
043
044    @Parameter(name = "-xpath", hasValue = true, help = "The xpath of the blob property to get. Defaults to the one used by the File document type.")
045    protected String xpath;
046
047    @Parameter(name = "-todir", hasValue = true, help = "An optional target directory to save the file. The default is the current working directory.")
048    protected File todir;
049
050    @Argument(name = "doc", index = 0, required = false, completor = DocRefCompletor.class, help = "The target document. If not specified the current document is used. To use UID references prefix them with 'doc:'.")
051    protected String path;
052
053    public void run() {
054        ShellConsole console = ctx.getShell().getConsole();
055        DocRef doc = ctx.resolveRef(path);
056        try {
057            FileBlob blob = ctx.getDocumentService().getBlob(doc, xpath);
058            String fname = blob.getFileName();
059            if (fname == null || fname.length() == 0) {
060                fname = "unnamed_blob";
061            }
062            if (todir == null) {
063                todir = ctx.getShell().getContextObject(FileSystem.class).pwd();
064            }
065            File dst = new File(todir, fname);
066            blob.getFile().renameTo(dst);
067            console.println("File saved to: " + dst.getAbsolutePath());
068        } catch (Exception e) {
069            throw new ShellException("Failed to get file from " + doc, e);
070        }
071
072    }
073}