001/* 002 * (C) Copyright 2015 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-2.1.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 * thibaud 016 */ 017package org.nuxeo.diff.pictures; 018 019import org.apache.commons.lang.StringUtils; 020import org.nuxeo.ecm.core.api.Blob; 021import org.nuxeo.ecm.core.api.DocumentModel; 022import org.nuxeo.ecm.platform.picture.api.ImageInfo; 023import org.nuxeo.ecm.platform.picture.api.ImagingService; 024import org.nuxeo.runtime.api.Framework; 025 026/** 027 * @since 7.4 028 */ 029public class DiffPicturesUtils { 030 031 /* 032 * Centralize handling of the targetFileName (used in at least 3 operations => less code in the operation itself) 033 */ 034 public static String updateTargetFileName(Blob inBlob, String inTargetFileName, String inTargetFileSuffix) { 035 036 String updatedName = ""; 037 038 if (inTargetFileName == null || inTargetFileName.isEmpty()) { 039 updatedName = inBlob.getFilename(); 040 } else { 041 updatedName = inTargetFileName; 042 } 043 044 if (inTargetFileSuffix != null && !inTargetFileSuffix.isEmpty()) { 045 updatedName = DiffPicturesUtils.addSuffixToFileName(updatedName, inTargetFileSuffix); 046 } 047 048 return updatedName; 049 } 050 051 /* 052 * Adds the suffix before the file extension, if any 053 */ 054 public static String addSuffixToFileName(String inFileName, String inSuffix) { 055 if (inFileName == null || inFileName.isEmpty() || inSuffix == null || inSuffix.isEmpty()) { 056 return inFileName; 057 } 058 059 int dotIndex = inFileName.lastIndexOf('.'); 060 if (dotIndex < 0) { 061 return inFileName + inSuffix; 062 } 063 064 return inFileName.substring(0, dotIndex) + inSuffix + inFileName.substring(dotIndex); 065 } 066 067 /** 068 * Check if the 2 blobs have the same format and same size. If yes, then the quick-compare ImageMagick command can 069 * be used. 070 * <p> 071 * If blobs are null or are not pictures, we do nothing, it will fails with later (or here with a null pointer 072 * exception) 073 * 074 * @param inB1 075 * @param inB2 076 * @return true if the 2 blobs are pictures with same format and dimensions 077 * @since 7.10 078 */ 079 public static boolean sameFormatAndDimensions(Blob inB1, Blob inB2) { 080 081 boolean result = true; 082 083 String mt1 = inB1.getMimeType().toLowerCase(); 084 String mt2 = inB2.getMimeType().toLowerCase(); 085 if (!StringUtils.equals(mt1, mt2)) { 086 result = false; 087 } else { 088 // Mime types are the same, check dimensions 089 ImagingService imagingService = Framework.getService(ImagingService.class); 090 ImageInfo info1 = imagingService.getImageInfo(inB1); 091 ImageInfo info2 = imagingService.getImageInfo(inB2); 092 if (info1.getWidth() != info2.getWidth() || info1.getHeight() != info2.getHeight()) { 093 result = false; 094 } 095 } 096 097 return result; 098 } 099 100 public static Blob getDocumentBlob(DocumentModel inDoc, String inXPath) { 101 102 Blob b; 103 104 if (StringUtils.isBlank(inXPath) || "null".equals(inXPath) || "default".equals(inXPath)) { 105 b = (Blob) inDoc.getPropertyValue(DiffPictures.DEFAULT_XPATH); 106 } else { 107 b = (Blob) inDoc.getPropertyValue(inXPath); 108 } 109 110 return b; 111 } 112 113}