PlatformExporter.java 4.92 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.io.IOUtils
 */
package com.day.jcr.vault.fs.io;

import com.day.jcr.vault.fs.api.AccessType;
import com.day.jcr.vault.fs.api.Artifact;
import com.day.jcr.vault.fs.api.VaultFile;
import com.day.jcr.vault.fs.io.AbstractExporter;
import com.day.jcr.vault.fs.io.ExportInfo;
import com.day.jcr.vault.util.PathUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Map;
import javax.jcr.RepositoryException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class PlatformExporter
extends AbstractExporter {
    private final File localParent;
    private boolean pruneMissing;

    public PlatformExporter(File localFile) {
        this.localParent = localFile;
    }

    public boolean pruneMissing() {
        return this.pruneMissing;
    }

    public void setPruneMissing(boolean pruneMissing) {
        this.pruneMissing = pruneMissing;
    }

    public void open() throws IOException, RepositoryException {
        this.scan(new File(this.localParent, "jcr_root"));
    }

    public void close() throws IOException {
        if (this.pruneMissing) {
            for (ExportInfo.Entry e : this.exportInfo.getEntries().values()) {
                if (e.type != ExportInfo.Type.DELETE) continue;
                File file = new File(e.path);
                FileUtils.deleteQuietly((File)file);
                this.track("D", PathUtil.getRelativePath(this.localParent.getAbsolutePath(), e.path));
            }
        }
    }

    private void scan(File dir) throws IOException {
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            String name = file.getName();
            if (name.equals(".svn") || name.equals(".vlt")) continue;
            if (file.isDirectory()) {
                this.exportInfo.update(ExportInfo.Type.RMDIR, file.getPath());
                this.scan(file);
                continue;
            }
            this.exportInfo.update(ExportInfo.Type.DELETE, file.getPath());
        }
    }

    public void createDirectory(VaultFile file, String relPath) throws RepositoryException, IOException {
        File dir = new File(this.localParent, this.getPlatformFilePath(file, relPath));
        this.mkdirs(dir);
        this.track("A", PathUtil.getRelativeFilePath(this.localParent.getAbsolutePath(), dir.getAbsolutePath()));
    }

    public void createDirectory(String relPath) throws IOException {
        File dir = new File(this.localParent, relPath);
        this.mkdirs(dir);
    }

    public void writeFile(VaultFile file, String relPath) throws RepositoryException, IOException {
        File local = new File(this.localParent, this.getPlatformFilePath(file, relPath));
        if (!local.getParentFile().exists()) {
            this.mkdirs(local.getParentFile());
        }
        if (local.exists()) {
            this.exportInfo.update(ExportInfo.Type.UPDATE, local.getPath());
        } else {
            this.exportInfo.update(ExportInfo.Type.ADD, local.getPath());
        }
        this.track("A", PathUtil.getRelativeFilePath(this.localParent.getAbsolutePath(), local.getAbsolutePath()));
        Artifact a = file.getArtifact();
        switch (a.getPreferredAccess()) {
            case NONE: {
                throw new RepositoryException("Artifact has no content.");
            }
            case SPOOL: {
                FileOutputStream out = new FileOutputStream(local);
                a.spool(out);
                out.close();
                break;
            }
            case STREAM: {
                InputStream in = a.getInputStream();
                FileOutputStream out = new FileOutputStream(local);
                IOUtils.copy((InputStream)in, (OutputStream)out);
                in.close();
                out.close();
            }
        }
        if (a.getLastModified() >= 0) {
            local.setLastModified(a.getLastModified());
        }
    }

    public void writeFile(InputStream in, String relPath) throws IOException {
        File local = new File(this.localParent, relPath);
        if (!local.getParentFile().exists()) {
            this.mkdirs(local.getParentFile());
        }
        if (local.exists()) {
            this.exportInfo.update(ExportInfo.Type.UPDATE, local.getPath());
        } else {
            this.exportInfo.update(ExportInfo.Type.ADD, local.getPath());
        }
        FileOutputStream out = new FileOutputStream(local);
        IOUtils.copy((InputStream)in, (OutputStream)out);
        in.close();
        out.close();
    }

    private void mkdirs(File dir) throws IOException {
        dir.mkdirs();
        this.exportInfo.update(ExportInfo.Type.MKDIR, dir.getPath());
    }

}