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

org.cassproject.general.repository.GeneralFile Maven / Gradle / Ivy

There is a newer version: 3.1.8
Show newest version
package org.cassproject.general.repository;

import com.eduworks.ec.blob.BlobHelper;
import org.cassproject.schema.general.EcRemoteLinkedData;
import org.cassproject.schema.general.FileSaver;
import org.cassproject.schema.general.General;
import org.stjs.javascript.Array;
import org.stjs.javascript.JSObjectAdapter;
import org.stjs.javascript.Map;

/**
 * A representation of a file.
 *
 * @author [email protected]
 * @module com.eduworks.ec
 * @class GeneralFile
 * @extends EcRemoteLinkedData
 * @constructor
 */
public class GeneralFile extends EcRemoteLinkedData {
	private static final String TYPE_0_1 = "http://schema.eduworks.com/general/0.1/file";
	private static final String TYPE_0_2 = "http://schema.eduworks.com/general/0.2/file";
	public static final String myType = TYPE_0_2;
	/**
	 * Optional checksum of the file, used to verify if the file has been
	 * transmitted correctly.
	 *
	 * @property checksum
	 * @type String
	 */
	public String checksum;
	/**
	 * Mime type of the file.
	 *
	 * @property mimeType
	 * @type String
	 */
	public String mimeType;
	/**
	 * Base-64 encoded version of the bytestream of a file.
	 *
	 * @property data
	 * @type String
	 */
	public String data;
	/**
	 * Name of the file, used to distinguish it
	 *
	 * @property name
	 * @type String
	 */
	public String name;

	public GeneralFile() {
		super(General.context, myType);
	}

	/**
	 * Helper method to force the browser to download the file.
	 *
	 * @memberOf GeneralFile
	 * @method download
	 */
	public void download() {
		Object blob = BlobHelper.base64ToBlob(data, mimeType);
		FileSaver.saveAs(blob, name);
	}

	@Override
	protected void upgrade() {
		super.upgrade();
		if (TYPE_0_1.equals(type)) {
			Map me = JSObjectAdapter.$properties(this);
			// Error in older versions of LD objects: We used @schema instead of
			// @context. Whoops.
			if (me.$get("@context") == null && me.$get("@schema") != null)
				me.$put("@context", me.$get("@schema"));
			setContextAndType(General.context_0_2, TYPE_0_2);
		}
	}

	@Override
	public Array getTypes() {
		Array a = new Array();
		a.push(TYPE_0_2);
		a.push(TYPE_0_1);
		return a;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy