Skip to content

Commit

Permalink
Add support for specifying generator ID
Browse files Browse the repository at this point in the history
Closes #18
  • Loading branch information
tjenkinson committed Nov 12, 2016
1 parent fd8364b commit bbcc531
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The following parameters are allowed:
- **interval**: Override `interval` option. (Optional)
- **initialThumbnailCount**: Override `initialThumbnailCount` option. (Optional)
- **targetThumbnailCount**: Override `targetThumbnailCount` option. (Optional)
- **id**: Provide an alpha-numeric ID for this generator. (Optional. Will be generated automatically if not provided.)

The response is `{id: <id which represents this generator>}`

Expand Down
16 changes: 12 additions & 4 deletions src/thumbnail-generator-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ ThumbnailGeneratorService.prototype._createServer = function() {
if (!url) {
throw new Error("URL required.");
}
var id = req.body.id;
if (typeof(id) === "undefined") {
id = this._generateId();
}
else if (/[^A-Za-z0-9\-]/.test(id) || id.length === 0) {
throw new Error("Invalid ID. Must be alpha numberic.");
}
else if (this._generators[id]) {
throw new Error("A generator with the provided ID already exists.");
}
var options = {
playlistUrl: url
};
Expand All @@ -128,7 +138,7 @@ ThumbnailGeneratorService.prototype._createServer = function() {
req.body.initialThumbnailCount && (options.initialThumbnailCount = parseInt(req.body.initialThumbnailCount));
req.body.targetThumbnailCount && (options.targetThumbnailCount = parseInt(req.body.targetThumbnailCount));

var id = this._createGenerator(options);
this._createGenerator(id, options);
res.send({
id: id
});
Expand Down Expand Up @@ -167,8 +177,7 @@ ThumbnailGeneratorService.prototype._createServer = function() {
this._app = app;
};

ThumbnailGeneratorService.prototype._createGenerator = function(options) {
var id = this._generateId();
ThumbnailGeneratorService.prototype._createGenerator = function(id, options) {
var thumbnailGeneratorOptions = Object.assign({}, this._thumbnailGeneratorOptions, options, {
tempDir: this._tempDir,
outputNamePrefix: id
Expand All @@ -180,7 +189,6 @@ ThumbnailGeneratorService.prototype._createGenerator = function(options) {
this._addListeners(id, generator);
this._generators[id] = generator;
this._schedulePingTimeout(id, generator);
return id;
};

ThumbnailGeneratorService.prototype._schedulePingTimeout = function(id, generator) {
Expand Down

0 comments on commit bbcc531

Please sign in to comment.