Skip to content

Commit

Permalink
First go downloading video for WPT #1705
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore committed Sep 27, 2017
1 parent 6b42700 commit b6d78b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/plugins/webpagetest/analyzer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const fs = require('fs');
const http = require('https');
const Promise = require('bluebird');
const log = require('intel').getLogger('sitespeedio.plugin.webpagetest');
const clone = require('lodash.clonedeep');
Expand Down Expand Up @@ -99,6 +100,42 @@ module.exports = {
)
);

if (options.video) {
let videoId;
promises.push(
wptClient
.createVideoAsync(id, { run, repeatView })
.then(result => {
videoId = result.data.videoId;
return storageManager.getWriteStreamForUrl(
videoId + '-' + run + '.mp4',
url,
'video'
);
})
.then(file => {
return new Promise((resolve, reject) => {
http.get(
options.host + '/video/download.php?id=' + videoId,
function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(function() {
resolve();
});
});
}
);
});
})
.catch(WPTAPIError, error =>
log.warn(
`Couldn't get video for id ${id}, run ${run}: ${error.message} (url = ${url})`
)
)
);
}

promises.push(
wptClient
.getWaterfallImageAsync(id, {
Expand Down
9 changes: 9 additions & 0 deletions lib/support/resultsStorage/storageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ module.exports = function storageManager(baseDir, storagePathPrefix) {
mkdirp(dirPath)
);
},
getWriteStreamForUrl(filename, url, subDir) {
const dirPath = ['data', subDir].filter(Boolean).join(path.sep);
return Promise.join(
this.createDirForUrl(url, dirPath),
filename,
(dirPath, filename) =>
fs.createWriteStream(path.join(dirPath, filename))
);
},
writeData(filename, data) {
return write(this.createDataDir('data'), filename, data);
},
Expand Down

0 comments on commit b6d78b3

Please sign in to comment.