All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metaeffekt.artifact.resolver.model.DownloadLocation Maven / Gradle / Ivy

There is a newer version: 0.134.0
Show newest version
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.metaeffekt.artifact.resolver.model;

import java.io.File;
import java.util.Locale;

/**
 * Represents and manages a download area.
 */
public class DownloadLocation {

    private final File downloadBaseDir;

    public DownloadLocation(File downloadBaseDir) {
        this.downloadBaseDir = downloadBaseDir;
    }

    public File deriveDownloadFolder(String ecosystem, String dirName) {
        final File ecosystemDir = new File(downloadBaseDir, ecosystem);
        return deriveDownloadDir(ecosystemDir, dirName);
    }

    private static File deriveDownloadDir(File ecosystemDir, String dirName) {
        // convert everything to lowercase
        String idLowerCase = dirName.toLowerCase(Locale.US).trim();

        // do not use slashes and colons (shiled from filesystem / platform issues)
        idLowerCase = idLowerCase.replace("/", "_");
        idLowerCase = idLowerCase.replace("\\", "_");
        idLowerCase = idLowerCase.replace(":", "_");

        final int length = idLowerCase.length();
        final String level1 = "[" + idLowerCase.substring(0, Math.min(length, 1)) + "]";
        final String level2 = "[" + idLowerCase.substring(0, Math.min(length, 2)) + "]";
        final String level3 = "[" + idLowerCase + "]";

        return new File(new File(new File(ecosystemDir, level1), level2), level3);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy