1605 | | This implementation is WRONG, for files >= 2GB it counts WRONG hash, for example for dummy avi file it is '''61f7751ec2a72bfb''' instead '''61f7751fc2a72bfb''' - please send us correct code. Thanks! |
1606 | | {{{ |
1607 | | var b = 65536; |
1608 | | fs = file.size; |
1609 | | var e = Array(8); |
1610 | | for (var a = fs, f = 0; f < 8; f++) { |
1611 | | e[f] = a & 255; |
1612 | | a = a >> 8 |
1613 | | } |
1614 | | a = fs; |
1615 | | |
1616 | | |
| 1605 | Download [http://www.opensubtitles.org/addons/download/oshash-example.zip example]. Key is using Javascript LONG library. Thanks to [http://divhide.com/ Oscar Brito] for fixing the code. |
| 1606 | |
| 1607 | {{{ |
| 1608 | //be sure Long.min.js is already included |
| 1609 | /* |
| 1610 | * Calculate OpenSubtitles hash |
| 1611 | * (Oscar Brito - aetheon@gmail.com) |
| 1612 | * |
| 1613 | * @param {File} file - a File obj contained on a DataTransfer |
| 1614 | * @param {Function} onComplete - the result callback |
| 1615 | */ |
| 1616 | var OpenSubtitlesHash = function(file, onComplete){ |
| 1617 | |
| 1618 | var HASH_CHUNK_SIZE = 64 * 1024; |
| 1619 | if(file.size<HASH_CHUNK_SIZE) |
| 1620 | HASH_CHUNK_SIZE = file.size; |
| 1621 | |
| 1622 | |
| 1623 | // sum chunk long values |
| 1624 | var sumChunk = function(arrayBuffer){ |
| 1625 | |
| 1626 | var view = new DataView(arrayBuffer); |
| 1627 | var hNumber = new dcodeIO.Long(); |
| 1628 | |
| 1629 | for(var i=0; i<arrayBuffer.byteLength; i+=8){ |
| 1630 | |
| 1631 | var low = view.getUint32(i, true); |
| 1632 | var high = view.getUint32(i+4, true); |
| 1633 | |
| 1634 | var n = new dcodeIO.Long(low, high); |
| 1635 | hNumber = hNumber.add(n); |
| 1636 | } |
| 1637 | |
| 1638 | return hNumber; |
| 1639 | |
| 1640 | }; |
| 1641 | |
| 1642 | |
| 1643 | // read chunk |
| 1644 | var readChunk = function(start, end, callback){ |
| 1645 | |
| 1646 | var reader = new FileReader(); |
| 1647 | reader.onload = function(e){ |
| 1648 | |
| 1649 | // sum all long values on the chunk |
| 1650 | var number = sumChunk(e.currentTarget.result); |
| 1651 | |
| 1652 | if(callback) |
| 1653 | callback(number); |
| 1654 | |
| 1655 | } |
| 1656 | |
| 1657 | var blob = file.slice(start, end); |
| 1658 | reader.readAsArrayBuffer(blob); |
| 1659 | }; |
| 1660 | |
| 1661 | |
| 1662 | // read the first chunk |
| 1663 | readChunk(0, HASH_CHUNK_SIZE, function(head){ |
| 1664 | |
| 1665 | // read the tail chunk |
| 1666 | var start = file.size-HASH_CHUNK_SIZE; |
| 1667 | if(start < 0) |
| 1668 | start = 0; |
| 1669 | |
| 1670 | readChunk(start, file.size, function(tail){ |
| 1671 | |
| 1672 | // sum all values |
| 1673 | var sum = head.add(tail).add(new dcodeIO.Long(file.size)); |
| 1674 | // convert to hex |
| 1675 | var sumHex = sum.toString(16); |
| 1676 | |
| 1677 | if(onComplete) |
| 1678 | onComplete(sumHex); |
| 1679 | |
| 1680 | }); |
| 1681 | |
| 1682 | }); |
| 1683 | |
| 1684 | }; |
| 1685 | |
| 1686 | |
| 1687 | // TODO |
| 1688 | $(document).ready(function() { |
| 1689 | |
| 1690 | $('#search_field').bind('drop', function(e, ev) { |
| 1691 | e.preventDefault(); |
| 1692 | var files = e.originalEvent.dataTransfer.files; |
| 1693 | $.each(files, function(index, file) { |
1618 | | var c = file.slice(0, b); |
1619 | | var g = new FileReader; |
1620 | | |
1621 | | |
1622 | | g.onloadend = function (h) { |
1623 | | if (h.target.readyState == FileReader.DONE) { |
1624 | | |
1625 | | for (var f = h.target.result, d = 0; d < f.length; d++) e[(d + 8) % 8] += f.charCodeAt(d); |
1626 | | c = file.slice(a - b); |
1627 | | var g = new FileReader; |
1628 | | |
1629 | | g.onloadend = function (c) { |
1630 | | var b = "languages"; |
1631 | | if (c.target.readyState == FileReader.DONE) { |
1632 | | f = c.target.result; |
1633 | | for (d = 0; d < f.length; d++) e[(d + 8) % 8] += f.charCodeAt(d); |
1634 | | lang = $("#SubLanguageID").multiselect("getChecked").map(function(){return this.value}).get().join(","); |
1635 | | if (lang == "") lang = "all"; |
1636 | | var url = "http://www.opensubtitles.org/" + $(location).attr('pathname').substr(1,2) + "/search/sublanguageid-" + lang + "/moviehash-" + binl2hex(e) + "/moviebytesize-" + fs + "/dragsearch-on"; |
1637 | | document.location = url; |
1638 | | } |
1639 | | }; |
1640 | | g.readAsBinaryString(c) |
1641 | | } |
1642 | | }; |
1643 | | g.readAsBinaryString(c) |
1644 | | |
1645 | | function binl2hex(a) { |
1646 | | var b = 255; |
1647 | | a[1] += a[0] >> 8; |
1648 | | a[0] = a[0] & b; |
1649 | | a[2] += a[1] >> 8; |
1650 | | a[1] = a[1] & b; |
1651 | | a[3] += a[2] >> 8; |
1652 | | a[2] = a[2] & b; |
1653 | | a[4] += a[3] >> 8; |
1654 | | a[3] = a[3] & b; |
1655 | | a[5] += a[4] >> 8; |
1656 | | a[4] = a[4] & b; |
1657 | | a[6] += a[5] >> 8; |
1658 | | a[5] = a[5] & b; |
1659 | | a[7] += a[6] >> 8; |
1660 | | a[6] = a[6] & b; |
1661 | | a[7] = a[7] & b; |
1662 | | for (var d = "0123456789abcdef", e = "", c = 7; c > -1; c--) e += d.charAt(a[c] >> 4 & 15) + d.charAt(a[c] & 15); |
1663 | | return e |
1664 | | } |
1665 | | |
1666 | | }}} |
| 1695 | OpenSubtitlesHash(file, function(hash){ |
| 1696 | |
| 1697 | // TODO |
| 1698 | document.write(hash); |
| 1699 | |
| 1700 | }) |
| 1701 | |
| 1702 | }); |
| 1703 | }); |
| 1704 | |
| 1705 | }); |
| 1706 | |
| 1707 | }}} |