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.nuxeo.ecm.core.api.Blob;
020
021/**
022 * @since 7.4
023 */
024public class DiffPicturesUtils {
025
026    /*
027     * Centralize handling of the targetFileName (used in at least 3 operations => less code in the operation itself)
028     */
029    public static String updateTargetFileName(Blob inBlob, String inTargetFileName, String inTargetFileSuffix) {
030
031        String updatedName = "";
032
033        if (inTargetFileName == null || inTargetFileName.isEmpty()) {
034            updatedName = inBlob.getFilename();
035        } else {
036            updatedName = inTargetFileName;
037        }
038
039        if (inTargetFileSuffix != null && !inTargetFileSuffix.isEmpty()) {
040            updatedName = DiffPicturesUtils.addSuffixToFileName(updatedName, inTargetFileSuffix);
041        }
042
043        return updatedName;
044    }
045
046    /*
047     * Adds the suffix before the file extension, if any
048     */
049    public static String addSuffixToFileName(String inFileName, String inSuffix) {
050        if (inFileName == null || inFileName.isEmpty() || inSuffix == null || inSuffix.isEmpty()) {
051            return inFileName;
052        }
053
054        int dotIndex = inFileName.lastIndexOf('.');
055        if (dotIndex < 0) {
056            return inFileName + inSuffix;
057        }
058
059        return inFileName.substring(0, dotIndex) + inSuffix + inFileName.substring(dotIndex);
060    }
061
062}