001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.shell.automation.cmds;
018
019import java.io.File;
020
021import org.nuxeo.ecm.automation.client.model.Blob;
022import org.nuxeo.ecm.automation.client.model.Blobs;
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 = "getfiles", help = "Get all the files attahced to a document")
039public class GetBlobs 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 content 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            Blobs blobs = ctx.getDocumentService().getBlobs(doc, xpath);
058            int i = 0;
059            for (Blob b : blobs) {
060                FileBlob blob = (FileBlob) b;
061                String fname = blob.getFileName();
062                if (fname == null || fname.length() == 0) {
063                    fname = "unnamed_blob_" + (++i);
064                }
065                if (todir == null) {
066                    todir = ctx.getShell().getContextObject(FileSystem.class).pwd();
067                }
068                File dst = new File(todir, fname);
069                blob.getFile().renameTo(dst);
070                console.println("File saved to: " + dst.getAbsolutePath());
071            }
072        } catch (Exception e) {
073            throw new ShellException("Failed to get files from " + doc, e);
074        }
075
076    }
077}