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.Blob;
024import org.nuxeo.ecm.automation.client.model.Blobs;
025import org.nuxeo.ecm.automation.client.model.DocRef;
026import org.nuxeo.ecm.automation.client.model.FileBlob;
027import org.nuxeo.shell.Argument;
028import org.nuxeo.shell.Command;
029import org.nuxeo.shell.Context;
030import org.nuxeo.shell.Parameter;
031import org.nuxeo.shell.ShellConsole;
032import org.nuxeo.shell.ShellException;
033import org.nuxeo.shell.automation.DocRefCompletor;
034import org.nuxeo.shell.automation.RemoteContext;
035import org.nuxeo.shell.fs.FileSystem;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040@Command(name = "getfiles", help = "Get all the files attahced to a document")
041public class GetBlobs implements Runnable {
042
043    @Context
044    protected RemoteContext ctx;
045
046    @Parameter(name = "-xpath", hasValue = true, help = "The xpath of the blob property to get. Defaults to the one used by the File document type.")
047    protected String xpath;
048
049    @Parameter(name = "-todir", hasValue = true, help = "An optional target directory to save the file. The default is the current working directory.")
050    protected File todir;
051
052    @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:'.")
053    protected String path;
054
055    public void run() {
056        ShellConsole console = ctx.getShell().getConsole();
057        DocRef doc = ctx.resolveRef(path);
058        try {
059            Blobs blobs = ctx.getDocumentService().getBlobs(doc, xpath);
060            int i = 0;
061            for (Blob b : blobs) {
062                FileBlob blob = (FileBlob) b;
063                String fname = blob.getFileName();
064                if (fname == null || fname.length() == 0) {
065                    fname = "unnamed_blob_" + (++i);
066                }
067                if (todir == null) {
068                    todir = ctx.getShell().getContextObject(FileSystem.class).pwd();
069                }
070                File dst = new File(todir, fname);
071                blob.getFile().renameTo(dst);
072                console.println("File saved to: " + dst.getAbsolutePath());
073            }
074        } catch (Exception e) {
075            throw new ShellException("Failed to get files from " + doc, e);
076        }
077
078    }
079}