-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathurl-hash.js
29 lines (25 loc) · 876 Bytes
/
url-hash.js
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
// import jsHash from 'jshashes'
import md5 from './_md5.js'
const server = 'https://upload.wikimedia.org/wikipedia/commons/'
const encodeTitle = function (file) {
let title = file.replace(/^(image|file?):/i, '')
title = title.trim()
//titlecase it
title = title.charAt(0).toUpperCase() + title.substring(1)
//spaces to underscores
title = title.replace(/ /g, '_')
return title
}
//the wikimedia image url is a little silly:
//https://commons.wikimedia.org/wiki/Commons:FAQ#What_are_the_strangely_named_components_in_file_paths.3F
const commonsURL = function () {
let file = this.data.file
let title = encodeTitle(file)
// let hash = new jsHash.MD5().hex(title)
let hash = md5(title)
let path = hash.substr(0, 1) + '/' + hash.substr(0, 2) + '/'
title = encodeURIComponent(title)
path += title
return server + path
}
export default commonsURL