001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.file;
018
019import java.io.File;
020
021import org.apache.camel.util.FileUtil;
022import org.apache.camel.util.ObjectHelper;
023
024public final class MoveExistingFileStrategyUtils {
025
026    private MoveExistingFileStrategyUtils() {
027    }
028
029    /**
030     * This method manipulates the destinationPath in case of moveExisting parameter is expressed as file language
031     * expression subdirectory name of the directoryName adding directoryName on top and file name at the end.
032     *
033     * for example, a camel endpoint like that:
034     *
035     * file://data/file?fileExist=Move&moveExisting=archive-${date:now:yyyyMMddHHmmssSSS}/
036     *
037     * directoryName = data/file, fileOnlyName = whatever.ext, destinationPath = archive-20201110115125770
038     *
039     * the outcome of this method would be data/file/archive-20201110115125770/whatever.ext
040     *
041     * @param  destinationPath the destination path
042     * @param  fileOnlyName    the file name without the path
043     * @param  directoryName   the path of the file to be moved/renamed
044     * @return                 the full destination path
045     */
046    public static String completePartialRelativePath(String destinationPath, String fileOnlyName, String directoryName) {
047
048        if (destinationPath.length() > 1 && destinationPath.endsWith("/")) {
049            destinationPath = destinationPath + fileOnlyName;
050        }
051
052        if (ObjectHelper.isNotEmpty(directoryName) && !destinationPath.startsWith(directoryName)
053                && !FileUtil.isAbsolute(new File(destinationPath))) {
054            destinationPath = directoryName + "/" + destinationPath;
055        }
056
057        return destinationPath;
058    }
059
060}