/*
 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Antoine Taillefer <ataillefer@nuxeo.com>
 */
package org.nuxeo.drive.operations;

import java.io.IOException;

import javax.mail.internet.ParseException;

import org.nuxeo.drive.adapter.FileItem;
import org.nuxeo.drive.adapter.FileSystemItem;
import org.nuxeo.drive.service.FileSystemItemManager;
import org.nuxeo.ecm.automation.OperationContext;
import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Context;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.runtime.api.Framework;

/**
 * Creates a file with the given blob in the {@link FileSystemItem} with the
 * given id for the currently authenticated user.
 *
 * @author Antoine Taillefer
 */
@Operation(id = NuxeoDriveCreateFile.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Create file")
public class NuxeoDriveCreateFile {

    public static final String ID = "NuxeoDrive.CreateFile";

    @Context
    protected OperationContext ctx;

    @Param(name = "parentId")
    protected String parentId;

    @OperationMethod
    public Blob run(Blob blob) throws ClientException, ParseException,
            IOException {

        FileSystemItemManager fileSystemItemManager = Framework.getLocalService(FileSystemItemManager.class);
        NuxeoDriveOperationHelper.normalizeMimeTypeAndEncoding(blob);
        FileItem fileItem = fileSystemItemManager.createFile(parentId, blob,
                ctx.getPrincipal());

        // Commit transaction explicitly to ensure client-side consistency
        // TODO: remove when https://jira.nuxeo.com/browse/NXP-10964 is fixed
        NuxeoDriveOperationHelper.commitAndReopenTransaction();
        return NuxeoDriveOperationHelper.asJSONBlob(fileItem);
    }

}

@@ -768,24 +768,68 @@
 Exception;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A
 import org.n
@@ -1910,16 +1910,191 @@
 entId;%0A%0A
+    /**%0A     * @deprecated%0A     * @see https://jira.nuxeo.com/browse/NXP-12173%0A     */%0A    @Deprecated%0A    @Param(name = %22name%22, required = false)%0A    protected String name;%0A%0A
     @Ope
@@ -2314,16 +2314,245 @@
 class);%0A
+        // The filename transfered by the multipart encoding is not preserved%0A        // correctly if there is non ascii characters in it.%0A        if (StringUtils.isNotBlank(name)) %7B%0A            blob.setFilename(name);%0A        %7D%0A
         

Put back the name parameter in NuxeoDrive.CreateFile (optional) as asked by a client for backward compat of old versions of the client