webgme-engine
Version:
WebGME server and Client API without a GUI
191 lines (147 loc) • 8.14 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: server/middleware/blob/BlobRunPluginClient.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: server/middleware/blob/BlobRunPluginClient.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*globals requireJS*/
/*eslint-env node*/
/**
* @author lattmann / https://github.com/lattmann
*
* Should be used only by developers in developer mode. Application server shall not run at the same time.
*/
'use strict';
var BlobClientClass = requireJS('blob/BlobClient'),
BlobMetadata = requireJS('blob/BlobMetadata'),
Q = require('q'),
fs = require('fs'),
path = require('path'),
BufferStreamReader = require('../../util/BufferStreamReader'),
StringStreamWriter = require('../../util/StringStreamWriter'),
ensureDir = require('../../util/ensureDir');
/**
* Initializes a new instance of a server side file system object.
*
* Note: This code strictly runs in node.js (server side).
*
* @param {{}} parameters
* @constructor
*/
function BlobRunPluginClient(blobBackend, logger, opts) {
BlobClientClass.call(this, {logger: logger});
this.opts = opts;
this.blobBackend = blobBackend;
this.writeBlobFilesDir = opts && opts.writeBlobFilesDir;
if (this.writeBlobFilesDir) {
this.logger.warn('writeBlobFilesDir given, will also write blobs to',
path.join(process.cwd(), this.writeBlobFilesDir));
}
}
// Inherits from BlobClient
BlobRunPluginClient.prototype = Object.create(BlobClientClass.prototype);
// Override the constructor with this object's constructor
BlobRunPluginClient.prototype.constructor = BlobRunPluginClient;
BlobRunPluginClient.prototype.getNewInstance = function () {
return new BlobRunPluginClient(this.blobBackend, this.logger, this.opts);
};
BlobRunPluginClient.prototype.getMetadata = function (metadataHash, callback) {
var self = this,
deferred = Q.defer();
self.blobBackend.getMetadata(metadataHash, function (err, hash, metadata) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(metadata);
}
});
return deferred.promise.nodeify(callback);
};
BlobRunPluginClient.prototype.getObject = function (metadataHash, callback, subpath) {
var self = this,
writeStream = new StringStreamWriter(),
deferred = Q.defer();
// TODO: we need to get the content and save as a local file.
// if we just proxy the stream we cannot set errors correctly.
self.blobBackend.getFile(metadataHash, subpath || '', writeStream, function (err /*, hash*/) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(writeStream.getBuffer());
}
});
return deferred.promise.nodeify(callback);
};
BlobRunPluginClient.prototype.putMetadata = function (metadataDescriptor, callback) {
var self = this,
metadata = new BlobMetadata(metadataDescriptor),
deferred = Q.defer();
self.blobBackend.putMetadata(metadata, function (err, hash) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(hash);
}
});
return deferred.promise.nodeify(callback);
};
BlobRunPluginClient.prototype.putFile = function (name, data, callback) {
var deferred = Q.defer(),
self = this,
filePath;
if (Buffer.isBuffer(data)) {
data = new BufferStreamReader(data);
}
this.blobBackend.putFile(name, data, function (err, hash) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(hash);
}
});
if (this.writeBlobFilesDir) {
filePath = path.join(process.cwd(), this.writeBlobFilesDir, name);
return ensureDir(path.dirname(filePath))
.then(function () {
return Q.all([
deferred.promise,
Q.ninvoke(fs, 'writeFile', filePath, data)
]);
})
.then(function (res) {
self.logger.info('Wrote file to', filePath);
return res[0]; // The hash
})
.nodeify(callback);
} else {
return deferred.promise.nodeify(callback);
}
};
module.exports = BlobRunPluginClient;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="Server_GMEAuth.html">Server:GMEAuth</a></li><li><a href="Server_SafeStorage.html">Server:SafeStorage</a></li><li><a href="Server_UserProject.html">Server:UserProject</a></li><li><a href="module-Core.html">Core</a></li><li><a href="module-Storage.html">Storage</a></li><li><a href="module-crosscuts.html">crosscuts</a></li><li><a href="module-serialization.html">serialization</a></li></ul><h3>Externals</h3><ul><li><a href="external-Promise.html">Promise</a></li></ul><h3>Classes</h3><ul><li><a href="AddOnBase.html">AddOnBase</a></li><li><a href="AddOnUpdateResult.html">AddOnUpdateResult</a></li><li><a href="Artifact.html">Artifact</a></li><li><a href="BlobClient.html">BlobClient</a></li><li><a href="BlobMetadata.html">BlobMetadata</a></li><li><a href="BlobRunPluginClient.html">BlobRunPluginClient</a></li><li><a href="Client.html">Client</a></li><li><a href="Core.html">Core</a></li><li><a href="ExecutorClient.html">ExecutorClient</a></li><li><a href="GMENode.html">GMENode</a></li><li><a href="GmeLogger.html">GmeLogger</a></li><li><a href="InterPluginResult.html">InterPluginResult</a></li><li><a href="JobInfo.html">JobInfo</a></li><li><a href="OutputInfo.html">OutputInfo</a></li><li><a href="PluginBase.html">PluginBase</a></li><li><a href="PluginConfig.html">PluginConfig</a></li><li><a href="PluginMessage.html">PluginMessage</a></li><li><a href="PluginNodeDescription.html">PluginNodeDescription</a></li><li><a href="PluginResult.html">PluginResult</a></li><li><a href="Project.html">Project</a></li><li><a href="ProjectInterface.html">ProjectInterface</a></li><li><a href="Server_GMEAuth-GMEAuth.html">GMEAuth</a></li><li><a href="Server_SafeStorage-SafeStorage.html">SafeStorage</a></li><li><a href="Server_UserProject-UserProject.html">UserProject</a></li><li><a href="WebsocketRouter.html">WebsocketRouter</a></li><li><a href="WebsocketRouterUser.html">WebsocketRouterUser</a></li></ul><h3>Events</h3><ul><li><a href="Client.html#event:BRANCH_CHANGED">BRANCH_CHANGED</a></li><li><a href="Client.html#event:BRANCH_CLOSED">BRANCH_CLOSED</a></li><li><a href="Client.html#event:BRANCH_OPENED">BRANCH_OPENED</a></li><li><a href="Client.html#event:BRANCH_STATUS_CHANGED">BRANCH_STATUS_CHANGED</a></li><li><a href="Client.html#event:CONNECTED_USERS_CHANGED">CONNECTED_USERS_CHANGED</a></li><li><a href="Client.html#event:NETWORK_STATUS_CHANGED">NETWORK_STATUS_CHANGED</a></li><li><a href="Client.html#event:NOTIFICATION">NOTIFICATION</a></li><li><a href="Client.html#event:PLUGIN_FINISHED">PLUGIN_FINISHED</a></li><li><a href="Client.html#event:PLUGIN_INITIATED">PLUGIN_INITIATED</a></li><li><a href="Client.html#event:PLUGIN_NOTIFICATION">PLUGIN_NOTIFICATION</a></li><li><a href="Client.html#event:PROJECT_CLOSED">PROJECT_CLOSED</a></li><li><a href="Client.html#event:PROJECT_OPENED">PROJECT_OPENED</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Fri Jun 21 2024 09:43:40 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>