001/*
002 * (C) Copyright 2012 Nuxeo SA (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.operations;
018
019import java.io.IOException;
020
021import javax.mail.internet.ParseException;
022
023import org.nuxeo.drive.adapter.FileItem;
024import org.nuxeo.drive.adapter.FileSystemItem;
025import org.nuxeo.drive.service.FileSystemItemManager;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.core.Constants;
028import org.nuxeo.ecm.automation.core.annotations.Context;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.automation.core.annotations.Param;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Updates the {@link FileSystemItem} with the given id with the given blob for the currently authenticated user.
037 *
038 * @author Antoine Taillefer
039 */
040@Operation(id = NuxeoDriveUpdateFile.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Update file")
041public class NuxeoDriveUpdateFile {
042
043    public static final String ID = "NuxeoDrive.UpdateFile";
044
045    @Context
046    protected OperationContext ctx;
047
048    @Param(name = "id")
049    protected String id;
050
051    /**
052     * @since 6.0
053     */
054    @Param(name = "parentId", required = false)
055    protected String parentId;
056
057    @OperationMethod
058    public Blob run(Blob blob) throws ParseException, IOException {
059
060        FileSystemItemManager fileSystemItemManager = Framework.getLocalService(FileSystemItemManager.class);
061        NuxeoDriveOperationHelper.normalizeMimeTypeAndEncoding(blob);
062        FileItem fileItem;
063        if (parentId == null) {
064            fileItem = fileSystemItemManager.updateFile(id, blob, ctx.getPrincipal());
065        } else {
066            fileItem = fileSystemItemManager.updateFile(id, parentId, blob, ctx.getPrincipal());
067        }
068
069        return NuxeoDriveOperationHelper.asJSONBlob(fileItem);
070    }
071
072}