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

org.zodiac.boot.file.ReactiveLocalFile Maven / Gradle / Ivy

The newest version!
package org.zodiac.boot.file;

import java.io.File;
import java.util.Date;

import org.springframework.http.codec.multipart.FilePart;
import org.zodiac.boot.config.PlatformUploadFileInfo;
import org.zodiac.sdk.toolkit.constants.StringPool;
import org.zodiac.sdk.toolkit.util.DateTimeUtil;
import org.zodiac.sdk.toolkit.util.ExceptionUtil;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class ReactiveLocalFile extends LocalFile {

    /**
     * 上传文件
     */
    @JsonIgnore
    private FilePart file;

    /**
     * 上传物理路径
     */
    private String uploadPath;

    /**
     * 上传虚拟路径
     */
    private String uploadVirtualPath;

    /**
     * 文件名
     */
    private String fileName;

    /**
     * 真实文件名
     */
    private String originalFileName;

    public ReactiveLocalFile(PlatformUploadFileInfo platformFileInfo, String dir, FilePart file) {
        super(platformFileInfo, dir);
        this.file = file;
        this.fileName = file.name();
        this.originalFileName = file.filename();
        this.uploadPath = PlatformFileUtil.formatUrl(
            File.separator + getPlatformFileInfo().getUploadRealPath() + File.separator + dir + File.separator
                + DateTimeUtil.format(new Date(), DateTimeUtil.FORMAT_8_a) + File.separator + this.originalFileName);
        this.uploadVirtualPath = PlatformFileUtil.formatUrl(
            getPlatformFileInfo().getUploadCtxPath().replace(getPlatformFileInfo().getContextPath(), StringPool.EMPTY)
                + File.separator + dir + File.separator + DateTimeUtil.format(new Date(), DateTimeUtil.FORMAT_8_a) + File.separator
                + this.originalFileName);
    }

    public ReactiveLocalFile(PlatformUploadFileInfo platformFileInfo, String dir, FilePart file, String uploadPath, String uploadVirtualPath) {
        this(platformFileInfo, dir, file);
        if (null != uploadPath) {
            this.uploadPath = PlatformFileUtil.formatUrl(uploadPath);
            this.uploadVirtualPath = PlatformFileUtil.formatUrl(uploadVirtualPath);
        }
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public String getFileName() {
        return fileName;
    }

    public void setOriginalFileName(String originalFileName) {
        this.originalFileName = originalFileName;
    }

    @Override
    public String getOriginalFileName() {
        return originalFileName;
    }

    public FilePart getFile() {
        return file;
    }

    public void setFile(FilePart file) {
        this.file = file;
    }

    @Override
    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public String getUploadVirtualPath() {
        return uploadVirtualPath;
    }

    public void setUploadVirtualPath(String uploadVirtualPath) {
        this.uploadVirtualPath = uploadVirtualPath;
    }

    @Override
    public void transfer(FileProxy fileFactory, boolean compress) {
        try {
            File file = new File(getUploadPath());

            if (null != fileFactory) {
                String[] path = fileFactory.path(file, getDir());
                this.uploadPath = path[0];
                this.uploadVirtualPath = path[1];
                file = fileFactory.rename(file, path[0]);
            }

            File pfile = file.getParentFile();
            if (!pfile.exists()) {
                pfile.mkdirs();
            }

            this.file.transferTo(file);

            if (compress) {
                fileFactory.compress(this.uploadPath);
            }

        } catch (IllegalStateException e) {
            log.error("{}", ExceptionUtil.stackTrace(e));
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy