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

com.github.automately.java.data.VirtualFile Maven / Gradle / Ivy

The newest version!
package com.github.automately.java.data;

import io.jsync.json.JsonObject;
import io.jsync.json.impl.Json;
import io.jsync.utils.Token;

import java.util.Date;

/**
 * The VirtualFile class is used by JCluster to store information about user files stored in the cluster.
 * It is used heavily by the VirtualFileService and VirtualFileSystem.
 */
public class VirtualFile {

    private String name = "";
    private String userToken = ""; //Empty by default
    private Date created = new Date();
    private Date updated = new Date();
    private long size = 0;
    private String type = "";
    private String pathAlias = "/";
    private boolean isPublic = false;
    private String token = Token.generateToken().toHex();
    private boolean isDirectory = false;

    public VirtualFile(JsonObject json){
        if(json == null){
            throw new NullPointerException();
        }
        this.token = json.getString("token", this.token);
        this.name = json.getString("name", this.name);
        this.userToken = json.getString("userToken", this.userToken);
        this.size = json.getLong("size", size);
        this.type = json.getString("type", this.type);
        this.pathAlias = json.getString("pathAlias", this.pathAlias);
        this.isPublic = json.getBoolean("isPublic", this.isPublic);

        // For some reason data didn't work out too well
        if(json.getValue("created") instanceof JsonObject &&
                json.getObject("created", new JsonObject()).containsField("$numberLong")){
            json.putValue("created", Long.valueOf(json.getObject("created", new JsonObject()).getValue("$numberLong")));
        }
        this.created = new Date(json.getLong("created", created.getTime()));

        // For some reason data didn't work out too well
        if(json.getValue("updated") instanceof JsonObject &&
                json.getObject("updated", new JsonObject()).containsField("$numberLong")){
            json.putValue("updated", Long.valueOf(json.getObject("updated", new JsonObject()).getValue("$numberLong")));
        }
        this.updated = new Date(json.getLong("updated", updated.getTime()));
        this.isDirectory = json.getBoolean("isDirectory", isDirectory);
    }

    public String getName(){
        return name;
    }

    public String getUserToken(){
        return userToken;
    }

    public Date getCreated(){
        return created;
    }

    public Date getUpdated(){
        return updated;
    }

    public long getSize(){
        return size;
    }

    public String getType(){
        return type;
    }

    public String getPathAlias(){
        return pathAlias;
    }

    public boolean isPublic(){
        return isPublic;
    }

    public String getToken(){
        return token;
    }

    public boolean isDirectory(){
        return isDirectory;
    }

    public JsonObject toJson() {
        JsonObject ret = new JsonObject(Json.encode(this));
        ret.putString("token", token);
        ret.putNumber("created", created.getTime());
        ret.putNumber("updated", updated.getTime());
        return ret;
    }

    @Override
    public String toString(){
        return toJson().encode();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy