001/* 002 * (C) Copyright 2015-2018 Nuxeo (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.lang3.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 if (inTargetFileName == null || inTargetFileName.isEmpty()) { 040 updatedName = inBlob.getFilename(); 041 } else { 042 updatedName = inTargetFileName; 043 } 044 045 if (inTargetFileSuffix != null && !inTargetFileSuffix.isEmpty()) { 046 updatedName = DiffPicturesUtils.addSuffixToFileName(updatedName, inTargetFileSuffix); 047 } 048 049 return updatedName; 050 } 051 052 /* 053 * Adds the suffix before the file extension, if any 054 */ 055 public static String addSuffixToFileName(String inFileName, String inSuffix) { 056 if (inFileName == null || inFileName.isEmpty() || inSuffix == null || inSuffix.isEmpty()) { 057 return inFileName; 058 } 059 060 int dotIndex = inFileName.lastIndexOf('.'); 061 if (dotIndex < 0) { 062 return inFileName + inSuffix; 063 } 064 065 return inFileName.substring(0, dotIndex) + inSuffix + inFileName.substring(dotIndex); 066 } 067 068 /** 069 * Check if the 2 blobs have the same format and same size. If yes, then the quick-compare ImageMagick command can 070 * be used. 071 * <p> 072 * If blobs are null or are not pictures, we do nothing, it will fails with later (or here with a null pointer 073 * exception) 074 * 075 * @return true if the 2 blobs are pictures with same format and dimensions 076 * @since 7.10 077 */ 078 public static boolean sameFormatAndDimensions(Blob inB1, Blob inB2) { 079 080 boolean result = true; 081 082 String mt1 = inB1.getMimeType().toLowerCase(); 083 String mt2 = inB2.getMimeType().toLowerCase(); 084 if (!StringUtils.equals(mt1, mt2)) { 085 result = false; 086 } else { 087 // Mime types are the same, check dimensions 088 ImagingService imagingService = Framework.getService(ImagingService.class); 089 ImageInfo info1 = imagingService.getImageInfo(inB1); 090 ImageInfo info2 = imagingService.getImageInfo(inB2); 091 if (info1.getWidth() != info2.getWidth() || info1.getHeight() != info2.getHeight()) { 092 result = false; 093 } 094 } 095 096 return result; 097 } 098 099 public static Blob getDocumentBlob(DocumentModel inDoc, String inXPath) { 100 101 Blob b; 102 103 if (StringUtils.isBlank(inXPath) || "null".equals(inXPath) || "default".equals(inXPath)) { 104 b = (Blob) inDoc.getPropertyValue(DiffPictures.DEFAULT_XPATH); 105 } else { 106 b = (Blob) inDoc.getPropertyValue(inXPath); 107 } 108 109 return b; 110 } 111 112}