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.apache.commons.lang.StringUtils; 024import org.nuxeo.drive.adapter.FileItem; 025import org.nuxeo.drive.adapter.FileSystemItem; 026import org.nuxeo.drive.service.FileSystemItemManager; 027import org.nuxeo.ecm.automation.OperationContext; 028import org.nuxeo.ecm.automation.core.Constants; 029import org.nuxeo.ecm.automation.core.annotations.Context; 030import org.nuxeo.ecm.automation.core.annotations.Operation; 031import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 032import org.nuxeo.ecm.automation.core.annotations.Param; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * Creates a file with the given blob in the {@link FileSystemItem} with the given id for the currently authenticated 038 * user. 039 * 040 * @author Antoine Taillefer 041 */ 042@Operation(id = NuxeoDriveCreateFile.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Create file") 043public class NuxeoDriveCreateFile { 044 045 public static final String ID = "NuxeoDrive.CreateFile"; 046 047 @Context 048 protected OperationContext ctx; 049 050 @Param(name = "parentId") 051 protected String parentId; 052 053 /** 054 * @deprecated 055 * @see https://jira.nuxeo.com/browse/NXP-12173 056 */ 057 @Deprecated 058 @Param(name = "name", required = false) 059 protected String name; 060 061 @OperationMethod 062 public Blob run(Blob blob) throws ParseException, IOException { 063 064 FileSystemItemManager fileSystemItemManager = Framework.getLocalService(FileSystemItemManager.class); 065 // The filename transfered by the multipart encoding is not preserved 066 // correctly if there is non ascii characters in it. 067 if (StringUtils.isNotBlank(name)) { 068 blob.setFilename(name); 069 } 070 NuxeoDriveOperationHelper.normalizeMimeTypeAndEncoding(blob); 071 FileItem fileItem = fileSystemItemManager.createFile(parentId, blob, ctx.getPrincipal()); 072 073 return NuxeoDriveOperationHelper.asJSONBlob(fileItem); 074 } 075 076}