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

com.qiniu.datasource.DefaultFileContainer Maven / Gradle / Ivy

package com.qiniu.datasource;

import com.qiniu.convert.*;
import com.qiniu.interfaces.*;
import com.qiniu.model.local.FileInfo;
import com.qiniu.persistence.FileSaveMapper;
import com.qiniu.util.ConvertingUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class DefaultFileContainer extends FileContainer> {

    public DefaultFileContainer(String path, Map> directoriesMap,
                                List antiDirectories, boolean keepDir, Map indexMap,
                                List fields, int unitLen, int threads) throws IOException {
        super(path, directoriesMap, antiDirectories, keepDir, indexMap, fields, unitLen, threads);
    }

    @Override
    public String getSourceName() {
        return "file";
    }

    @Override
    protected ITypeConvert> getNewConverter() {
        return new Converter>() {
            @Override
            public Map convertToV(FileInfo line) throws IOException {
                return ConvertingUtils.toPair(line, indexMap, new StringMapPair());
            }
        };
    }

    @Override
    protected ITypeConvert getNewStringConverter() {
        IStringFormat stringFormatter;
        if ("json".equals(saveFormat)) {
            stringFormatter = line -> ConvertingUtils.toPair(line, fields, new JsonObjectPair()).toString();
        } else if ("yaml".equals(saveFormat)) {
            stringFormatter = line -> ConvertingUtils.toStringWithIndent(line, fields, initPathSize);
        } else {
            stringFormatter = line -> ConvertingUtils.toPair(line, fields, new StringBuilderPair(saveSeparator));
        }
        return new Converter() {
            @Override
            public String convertToV(FileInfo line) throws IOException {
                return stringFormatter.toFormatString(line);
            }
        };
    }

    @Override
    protected IResultOutput getNewResultSaver(String order) throws IOException {
        return order != null ? new FileSaveMapper(savePath, getSourceName(), order) : new FileSaveMapper(savePath);
    }

    @Override
    protected IFileLister getLister(File directory, String start, String end, int unitLen) throws IOException {
        return new FileInfoLister(directory, keepDir, withEtag, withDatetime, withMime, withParent, transferPath, leftTrimSize, start, end, unitLen);
    }

    @Override
    protected IFileLister getLister(String name, List fileInfoList, String start, String end,
                                                    int unitLen) throws IOException {
        return new FileInfoLister(name, fileInfoList, start, end, unitLen);
    }

    @Override
    protected IFileLister getLister(String singleFilePath) throws IOException {
        File file = new File(singleFilePath);
        if (!file.exists()) throw new IOException(singleFilePath + " path not found.");
        FileInfo fileInfo = new FileInfo(file, transferPath, leftTrimSize);
        if (indexMap.containsKey("etag")) {
            try { fileInfo = fileInfo.withEtag(); } catch (IOException e) {
                fileInfo.etag = e.getMessage().replace("\n", ","); }
        }
        if (indexMap.containsKey("mime")) {
            try { fileInfo = fileInfo.withMime(); } catch (IOException e) {
                fileInfo.mime = e.getMessage().replace("\n", ","); }
        }
        if (indexMap.containsKey("parent")) fileInfo = fileInfo.withParent();
        List list = new ArrayList<>();
        list.add(fileInfo);
        return getLister(singleFilePath, list, null, null, unitLen);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy