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