Skip to content

Commit

Permalink
electron: fix hash issue when replacing ffmpeg
Browse files Browse the repository at this point in the history
Hash computation is bogus, this commit fixes it.

Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
  • Loading branch information
paul-marechal committed Aug 1, 2019
1 parent 2a65e63 commit fe938ac
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
17 changes: 17 additions & 0 deletions dev-packages/electron/electron-ffmpeg-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,26 @@

// @ts-check

const crypto = require('crypto');
const path = require('path');
const fs = require('fs');

const ffmpeg = require('./native/build/Release/ffmpeg.node');

/**
* @param {String} path
* @return {Buffer} Hash of the file.
*/
exports.hashFile = async function (path) {
return new Promise((resolve, reject) => {
const sha256 = crypto.createHash('sha256');
fs.createReadStream(path)
.on('close', () => resolve(sha256.digest()))
.on('data', data => sha256.update(data))
.on('error', reject);
});
}

/**
* @type {NodeJS.Platform[]}
*/
Expand Down
17 changes: 2 additions & 15 deletions dev-packages/electron/electron-replace-ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,17 @@

const downloadElectron = require('electron-download');
const unzipper = require('unzipper');
const crypto = require('crypto');
const yargs = require('yargs');
const path = require('path');
const fs = require('fs');

const { platforms, libffmpegLocation } = require('./electron-ffmpeg-lib')
const { hashFile, platforms, libffmpegLocation } = require('./electron-ffmpeg-lib')

const downloadCache = path.resolve(__dirname, 'download');
if (!fs.existsSync(downloadCache)) {
fs.mkdirSync(downloadCache);
}

/**
* @param {String} path
* @return {Buffer} Hash of the file.
*/
async function hashFile(path) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash('md5');
const stream = fs.createReadStream(path);
stream.pipe(hash).on('finish', () => resolve(hash.digest()));
});
}

async function main() {
const options = yargs
.option('electronVersion', {
Expand Down Expand Up @@ -125,7 +112,7 @@ async function main() {
// Extract file to cache.
await new Promise((resolve, reject) => {
file.stream()
.pipe(fs.createWriteStream(libffmpegCachedPath, { }))
.pipe(fs.createWriteStream(libffmpegCachedPath))
.on('finish', resolve)
.on('error', reject);
});
Expand Down
3 changes: 2 additions & 1 deletion dev-packages/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"yargs": "^11.1.0"
},
"scripts": {
"postinstall": "node scripts/post-install.js"
"postinstall": "node scripts/post-install.js",
"test": "mocha \"tests/**/*.spec.js\""
}
}
40 changes: 40 additions & 0 deletions dev-packages/electron/tests/electron-ffmpeg-lib.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/********************************************************************************
* Copyright (C) 2019 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

const path = require('path');
const { expect } = require('chai');

const { hashFile } = require('../electron-ffmpeg-lib');

function resource(...parts) {
return path.resolve(__dirname, 'test-resources', ...parts);
}

describe('ffmpeg utility functions', () => {

it('hashFile', async () => {
const [
hashA, hashB, hashC
] = await Promise.all([
hashFile(resource('fileA.txt')),
hashFile(resource('fileB.txt')),
hashFile(resource('fileC.txt')),
]);
expect(hashA.equals(hashC)).true;
expect(hashA.equals(hashB)).false;
});

});
1 change: 1 addition & 0 deletions dev-packages/electron/tests/test-resources/fileA.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ABC
1 change: 1 addition & 0 deletions dev-packages/electron/tests/test-resources/fileB.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
XYZ
1 change: 1 addition & 0 deletions dev-packages/electron/tests/test-resources/fileC.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ABC

0 comments on commit fe938ac

Please sign in to comment.