refactor getFingerprint

This commit is contained in:
Saebekassebil 2012-03-27 00:14:59 +02:00
parent 94a1558843
commit 4703a6cfcd

View File

@ -587,16 +587,6 @@ var PDFDocModel = (function PDFDocModelClosure() {
this.mainXRefEntriesOffset); this.mainXRefEntriesOffset);
this.xref = xref; this.xref = xref;
this.catalog = new Catalog(xref); this.catalog = new Catalog(xref);
if (xref.trailer) {
if (xref.trailer.has('ID')) {
var fileID = '';
var id = xref.fetchIfRef(xref.trailer.get('ID'))[0];
id.split('').forEach(function(el) {
fileID += Number(el.charCodeAt(0)).toString(16);
});
this.fileID = fileID;
}
}
}, },
get numPages() { get numPages() {
var linearization = this.linearization; var linearization = this.linearization;
@ -610,20 +600,25 @@ var PDFDocModel = (function PDFDocModelClosure() {
} }
}, },
getFingerprint: function pdfDocGetFingerprint() { getFingerprint: function pdfDocGetFingerprint() {
if (this.fileID) { var xref = this.xref, fileID;
return this.fileID; if (xref.trailer.has('ID')) {
fileID = '';
var id = xref.fetchIfRef(xref.trailer.get('ID'))[0];
id.split('').forEach(function(el) {
fileID += Number(el.charCodeAt(0)).toString(16);
});
} else { } else {
// If we got no fileID, then we generate one, // If we got no fileID, then we generate one,
// from the first 100 bytes of PDF // from the first 100 bytes of PDF
var data = this.stream.bytes.subarray(0, 100); var data = this.stream.bytes.subarray(0, 100);
var hash = calculateMD5(data, 0, data.length); var hash = calculateMD5(data, 0, data.length);
var strHash = ''; fileID = '';
for (var i = 0, length = hash.length; i < length; i++) { for (var i = 0, length = hash.length; i < length; i++) {
strHash += Number(hash[i]).toString(16); fileID += Number(hash[i]).toString(16);
}
} }
return strHash; return shadow(this, 'getFingerprint', fileID);
}
}, },
getPage: function pdfDocGetPage(n) { getPage: function pdfDocGetPage(n) {
return this.catalog.getPage(n); return this.catalog.getPage(n);