BinaryMap

class
 new BinaryMap() 

Option name Type Description

This is a map used to retrieve binary

class BinaryMap {
	constructor(obj) {
		for (var i in obj) {
			this[i] = obj[i];
		}
	}
}

Binary

class
 new Binary()  extends Executor

Option name Type Description

This is an abstract service to represent a storage of files
The binary allow you to expose this service as HTTP ( therefore is an executor )
It needs an object to attach the binary too

The Binary storage should store only once a binary and reference every object that are used by this binary, so it can be cleaned.

class Binary extends Executor {

store

method
 Binary.prototype.store() 

Option name Type Description
targetStore Store

The store that handles the object to attach binary to

object String

The object uuid to get from the store

property String

The object property to add the file to

file Object

The file by itself

metadatas Object

to add to the binary object

When you store a binary to be able to retrieve it you need to store the information into another object

If you have a User object define like this : User = {'name': 'Remi', 'uuid': 'Loopingz'}
You will call the store(userStore, 'Loopingz', 'images', filedata, {'type':'profile'})
After a successful call the object will look like User = {'name': 'Remi', 'uuid': 'Loopingz', 'images': [{'type':'profile','hash':'a12545...','size':1245,'mime':'application/octet'}]}

store(targetStore, object, property, file, metadatas) {
	throw Error("AbstractBinary has no store method");
}

getUsageCount

method
 Binary.prototype.getUsageCount() 

The store can retrieve how many time a binary has been used

getUsageCount(hash) {
	throw Error("AbstractBinary has no store method");
}

update

method
 Binary.prototype.update() 

Option name Type Description
targetStore Store

The store that handles the object to attach binary to

object String

The object uuid to get from the store

property String

The object property to add the file to

index Number

The index of the file to change in the property

file Object

The file by itself

metadatas Object

to add to the binary object

Update a binary

update(targetStore, object, property, index, file, metadatas) {
	throw Error("AbstractBinary has no update method");
}

delete

method
 Binary.prototype.delete() 

Option name Type Description
targetStore Store

The store that handles the object to attach binary to

object String

The object uuid to get from the store

property String

The object property to add the file to

index Number

The index of the file to change in the property

Update a binary

delete(targetStore, object, property, index) {
	throw Error("AbstractBinary has no update method");
}

get

method
 Binary.prototype.get() 

Option name Type Description
info Object

The reference stored in your target object

Get a binary

get(info) {
	return this.emit('Binary.Get', {'object': info, 'service': this}).then( () => {
		return this._get(info);	
	});
}

downloadTo

method
 Binary.prototype.downloadTo() 

Option name Type Description
info Object

The reference stored in your target object

filepath String

to save the binary to

Download a binary to a file

downloadTo(info, filename) {
	var readStream = this.get(info);
	var writeStream = fs.createWriteStream(filename);
	return new Promise((resolve, reject) => {
		writeStream.on('finish', (src) => {
			return resolve();
		});
		writeStream.on('error', (src) => {
			try {
				fs.unlinkSync(filename);
			} catch(err) {
				console.log(err);
			}
			return reject();
		});
		readStream.pipe(writeStream);
	});
}