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

com.metaeffekt.mirror.download.advisor.MsrcManualCsvDownload Maven / Gradle / Ivy

There is a newer version: 0.132.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 com.metaeffekt.mirror.download.advisor;

import com.metaeffekt.mirror.download.documentation.MirrorMetadata;
import com.metaeffekt.mirror.download.Download;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Arrays;

@Deprecated
@MirrorMetadata(directoryName = "msrc-csv", mavenPropertyName = "msrcCsvDownload")
public class MsrcManualCsvDownload extends Download {

    private final static Logger LOG = LoggerFactory.getLogger(MsrcManualCsvDownload.class);

    public MsrcManualCsvDownload(File baseMirrorDirectory) {
        super(baseMirrorDirectory, MsrcManualCsvDownload.class);
    }

    @Override
    protected void performDownload() {
        final File[] files = super.downloadIntoDirectory.listFiles();
        final int fileCount = files == null ? 0 : (int) Arrays.stream(files).filter(file -> file.getName().endsWith(".csv")).count();

        if (fileCount == 0) {
            LOG.error("No files found in {}", super.downloadIntoDirectory);
            LOG.error("No files found for [msrc-csv]. Visit https://msrc.microsoft.com/update-guide/vulnerability to manually download the CSV files into {}", super.downloadIntoDirectory);
            setCsvCount(0);
        } else {
            LOG.info("Found {} files in {}. Assuming download was successful.", fileCount, super.downloadIntoDirectory);
            setCsvCount(fileCount);
        }
    }

    private void setCsvCount(int count) {
        super.propertyFiles.set(this.downloadIntoDirectory, "info", InfoFileAttributes.MSRC_PREFIX.getKey() + "csv-count", String.valueOf(count));
    }

    private int getCsvCount() {
        return Integer.parseInt(super.propertyFiles.getString(this.downloadIntoDirectory, "info", InfoFileAttributes.MSRC_PREFIX.getKey() + "csv-count").orElse("0"));
    }

    @Override
    protected boolean isResetRequired() {
        return false;
    }

    @Override
    protected boolean additionalIsDownloadRequired() {
        final long lastModified = super.getDownloadDirectoryLastModified();
        final File[] files = super.downloadIntoDirectory.listFiles();

        int count = 0;
        if (files != null) {
            for (File file : files) {
                if (file.getName().endsWith(".csv")) {
                    count++;
                    if (file.lastModified() > lastModified) {
                        LOG.info("Download is required: File {} has been updated {} > {}", file.getName(), file.lastModified(), lastModified);
                        return true;
                    }
                }
            }
        }

        if (count != getCsvCount()) {
            LOG.info("Download is required: CSV count has changed {} != {}", count, getCsvCount());
            return true;
        }

        LOG.info("Download is not required: CSV count has not changed {} == {}", count, getCsvCount());

        return false;
    }

    @Override
    public void setRemoteResourceLocation(String location, String url) {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy