Commit 8ed1f971 authored by chdorner's avatar chdorner

Use PDF URL instead of URN when not a file:// URI

Which will then be used as the `target.source` when creating an
annotation.
parent 2998e577
......@@ -27,7 +27,11 @@ function PDFMetadata(app) {
*/
PDFMetadata.prototype.getUri = function () {
return this._loaded.then(function (app) {
return fingerprintToURN(app.documentFingerprint);
var uri = getPDFURL(app);
if (!uri) {
uri = fingerprintToURN(app.documentFingerprint);
}
return uri;
});
};
......@@ -52,11 +56,9 @@ PDFMetadata.prototype.getMetadata = function () {
{href: fingerprintToURN(app.documentFingerprint)}
];
// Local file:// URLs should not be saved in document metadata.
// Entries in document.link should be URIs. In the case of
// local files, omit the URL.
if (app.url.indexOf('file://') !== 0) {
link.push({href: app.url});
var url = getPDFURL(app);
if (url) {
link.push({href: url});
}
return {
......@@ -71,4 +73,15 @@ function fingerprintToURN(fingerprint) {
return 'urn:x-pdf:' + String(fingerprint);
}
function getPDFURL(app) {
// Local file:// URLs should not be saved in document metadata.
// Entries in document.link should be URIs. In the case of
// local files, omit the URL.
if (app.url.indexOf('file://') !== 0) {
return app.url;
}
return null;
}
module.exports = PDFMetadata;
......@@ -9,19 +9,23 @@ describe('pdf-metadata', function () {
var event = document.createEvent('Event');
event.initEvent('documentload', false, false);
fakeApp.url = 'http://fake.com';
fakeApp.documentFingerprint = 'fakeFingerprint';
window.dispatchEvent(event);
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'urn:x-pdf:fakeFingerprint');
assert.equal(uri, 'http://fake.com');
});
});
it('does not wait for the PDF to load if it has already loaded', function () {
var fakePDFViewerApplication = {documentFingerprint: 'fakeFingerprint'};
var fakePDFViewerApplication = {
url: 'http://fake.com',
documentFingerprint: 'fakeFingerprint',
};
var pdfMetadata = new PDFMetadata(fakePDFViewerApplication);
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'urn:x-pdf:fakeFingerprint');
assert.equal(uri, 'http://fake.com');
});
});
......@@ -37,7 +41,7 @@ describe('pdf-metadata', function () {
'dc:title': 'fakeTitle',
}
},
url: 'fakeUrl',
url: 'http://fake.com',
};
beforeEach(function () {
......@@ -45,7 +49,19 @@ describe('pdf-metadata', function () {
});
describe('#getUri', function () {
it('returns the URN-ified document fingerprint as its URI', function () {
it('returns the non-file URI', function() {
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'http://fake.com');
});
});
it('returns the fingerprint as a URN when the PDF URL is a local file', function () {
var fakePDFViewerApplication = {
url: 'file:///test.pdf',
documentFingerprint: 'fakeFingerprint',
};
var pdfMetadata = new PDFMetadata(fakePDFViewerApplication);
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'urn:x-pdf:fakeFingerprint');
});
......@@ -88,7 +104,7 @@ describe('pdf-metadata', function () {
var pdfMetadata;
var fakePDFViewerApplication = {
documentFingerprint: 'fakeFingerprint',
url: 'file://fakeUrl',
url: 'file://fake.pdf',
};
var expectedMetadata = {
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.documentFingerprint}],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment