File size: 652 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const crypto = require( 'crypto' );
const fs = require( 'fs' );
const qs = require( 'qs' );

const HASH_LENGTH = 10;
const URL_BASE_PATH = '/calypso';

function hashFile( path ) {
	const md5 = crypto.createHash( 'md5' );
	let data;
	let hash;

	try {
		data = fs.readFileSync( path );
		md5.update( data );
		hash = md5.digest( 'hex' );
		hash = hash.slice( 0, HASH_LENGTH );
	} catch ( e ) {
		hash = new Date().getTime().toString();
	}

	return hash;
}

function getUrl( filename, hash ) {
	return (
		URL_BASE_PATH +
		'/' +
		filename +
		'?' +
		qs.stringify( {
			v: hash,
		} )
	);
}

module.exports = {
	hashFile: hashFile,
	getUrl: getUrl,
};