001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.fabric.repository;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.nio.file.Path;
022    
023    import java.util.concurrent.atomic.AtomicLong;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class RepositoryHelperUtil {
029    
030            public static Path getRepositoryFilePath(
031                    Path repositoryPath, Path remoteFilePath) {
032    
033                    Path fileNamePath = remoteFilePath.getFileName();
034    
035                    String name = fileNamePath.toString();
036    
037                    int index = name.lastIndexOf(CharPool.PERIOD);
038    
039                    if (index == -1) {
040                            StringBundler sb = new StringBundler(5);
041    
042                            sb.append(name);
043                            sb.append(StringPool.DASH);
044                            sb.append(System.currentTimeMillis());
045                            sb.append(StringPool.DASH);
046                            sb.append(idGenerator.getAndIncrement());
047    
048                            return repositoryPath.resolve(sb.toString());
049                    }
050    
051                    StringBundler sb = new StringBundler(6);
052    
053                    sb.append(name.substring(0, index));
054                    sb.append(StringPool.DASH);
055                    sb.append(System.currentTimeMillis());
056                    sb.append(StringPool.DASH);
057                    sb.append(idGenerator.getAndIncrement());
058                    sb.append(name.substring(index));
059    
060                    return repositoryPath.resolve(sb.toString());
061            }
062    
063            protected static final AtomicLong idGenerator = new AtomicLong();
064    
065    }