001/* 002 * (C) Copyright 2016 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 * Ricardo Dias 018 */ 019package org.nuxeo.ecm.platform.video.tools; 020 021import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; 022 023import java.io.IOException; 024import java.nio.file.Files; 025import java.nio.file.Paths; 026import java.util.HashMap; 027import java.util.Map; 028 029import org.apache.commons.io.FilenameUtils; 030import org.nuxeo.ecm.core.api.Blob; 031import org.nuxeo.ecm.core.api.NuxeoException; 032import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 033import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 034import org.nuxeo.ecm.core.api.impl.blob.FileBlob; 035 036/** 037 * Common interface to setup the video tools. 038 * 039 * @since 8.4 040 */ 041public abstract class VideoTool { 042 043 protected String name; 044 045 protected String commandLineName; 046 047 protected final static String SOURCE_FILE_PATH_PARAM = "sourceFilePath"; 048 049 protected final static String OUTPUT_FILE_PATH_PARAM = "outFilePath"; 050 051 protected final static String OUTPUT_MIMETYPE_PARAM = "outputMimetype"; 052 053 protected final static String VIDEO_TOOLS_DIRECTORY = "NuxeoVideoTools"; 054 055 public VideoTool(String name, String commandLineName) { 056 this.name = name; 057 this.commandLineName = commandLineName; 058 } 059 060 public String getName() { 061 return name; 062 } 063 064 public String getCommandLineName() { 065 return commandLineName; 066 } 067 068 public Map<String, String> setupParameters(BlobHolder input, Map<String, Object> parameters) { 069 Map<String, String> cmdParameters = new HashMap<>(); 070 cmdParameters.put(SOURCE_FILE_PATH_PARAM, input.getBlob().getFile().getAbsolutePath()); 071 return cmdParameters; 072 } 073 074 /** 075 * Removes any temporary input files that were used for command execution. 076 */ 077 public void cleanupInputs(Map<String, String> parameters) { 078 } 079 080 /** 081 * Returns a {@link BlobHolder} containing the result of the command. 082 * 083 * @param mimeType the MIME type 084 * @param parameters the parameters 085 * @return the blob holder 086 */ 087 public BlobHolder buildResult(String mimeType, Map<String, String> parameters) { 088 String path = parameters.get(OUTPUT_FILE_PATH_PARAM); 089 mimeType = parameters.getOrDefault(OUTPUT_MIMETYPE_PARAM, mimeType); 090 Blob blob = getTemporaryBlob(path, mimeType); 091 return new SimpleBlobHolder(blob); 092 } 093 094 /** 095 * Gets a temporary blob for the given temporary path. 096 * <p> 097 * The temporary blob is backed by a temporary file in a new location. The old file is removed. 098 * 099 * @param path the path to a temporary file 100 * @param mimeType the blob MIME type 101 * @return a temporary {@link Blob} 102 */ 103 public static Blob getTemporaryBlob(String path, String mimeType) { 104 String ext = "." + FilenameUtils.getExtension(path); 105 Blob blob; 106 try { 107 blob = new FileBlob(ext); // automatically tracked for removal 108 Files.move(Paths.get(path), blob.getFile().toPath(), REPLACE_EXISTING); 109 } catch (IOException e) { 110 throw new NuxeoException(e); 111 } 112 blob.setMimeType(mimeType); 113 blob.setFilename(FilenameUtils.getName(path)); 114 return blob; 115 } 116 117}