Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

fix PDF loading local files #8644

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ const initPartition = (partition) => {
}
module.exports.initPartition = initPartition

const filterableProtocols = ['http:', 'https:', 'ws:', 'wss:', 'magnet:']
const filterableProtocols = ['http:', 'https:', 'ws:', 'wss:', 'magnet:', 'file:']

function shouldIgnoreUrl (details) {
// internal requests
Expand Down Expand Up @@ -676,6 +676,9 @@ module.exports.isResourceEnabled = (resourceName, url, isPrivate) => {
return true
}

if (resourceName === 'pdfjs') {
return getSetting(settings.PDFJS_ENABLED)
}
if (resourceName === 'webtorrent') {
return getSetting(settings.TORRENT_VIEWER_ENABLED)
}
Expand Down
2 changes: 2 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const TrackingProtection = require('./trackingProtection')
const AdBlock = require('./adBlock')
const AdInsertion = require('./browser/ads/adInsertion')
const HttpsEverywhere = require('./httpsEverywhere')
const PDFJS = require('./pdfJS')
const SiteHacks = require('./siteHacks')
const CmdLine = require('./cmdLine')
const UpdateStatus = require('../js/constants/updateStatus')
Expand Down Expand Up @@ -333,6 +334,7 @@ app.on('ready', () => {
TrackingProtection.init()
AdBlock.init()
AdInsertion.init()
PDFJS.init()

if (!loadedPerWindowState || loadedPerWindowState.length === 0) {
if (!CmdLine.newWindowURL()) {
Expand Down
56 changes: 56 additions & 0 deletions app/pdfJS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'

const UrlUtil = require('../js/lib/urlutil')
const Filtering = require('./filtering')
const config = require('../js/constants/config')
const appActions = require('../js/actions/appActions')
const {getPathFromFileURI} = require('./common/lib/platformUtil')
const extensionState = require('./common/state/extensionState')
const appStore = require('../js/stores/appStore')
const fs = require('fs')
const path = require('path')
const getSetting = require('../js/settings').getSetting
const settings = require('../js/constants/settings')

const pdfjsBaseUrl = `chrome-extension://${config.PDFJSExtensionId}/`
const viewerBaseUrl = `${pdfjsBaseUrl}content/web/viewer.html`

const onBeforeRequest = (details) => {
const result = { resourceName: 'pdfjs' }
if (!(details.resourceType === 'mainFrame' &&
UrlUtil.isFileScheme(details.url) &&
UrlUtil.isFileType(details.url, 'pdf'))) {
return result
}
// Cancel and redirect to the PDF viewer URL
const extensions = extensionState.getExtensions(appStore.getState())
const extensionPath = extensions.getIn([config.PDFJSExtensionId, 'filePath'])
const pdfPath = getPathFromFileURI(details.url)
const pdfName = pdfPath.split('/').pop()
try {
const writeStream = fs.createWriteStream(path.join(extensionPath, 'tmp', pdfName))
writeStream.on('close', () => {
const pdfChromeUrl = `${pdfjsBaseUrl}tmp/${pdfName}`
const viewerUrl = `${viewerBaseUrl}?file=${encodeURIComponent(pdfChromeUrl)}#${pdfPath}`
appActions.loadURLRequested(details.tabId, viewerUrl)
})
fs.createReadStream(pdfPath).pipe(writeStream)
} catch (e) {
return result
}

result.cancel = true
return result
}

/**
* Load PDF.JS
*/
module.exports.init = () => {
if (getSetting(settings.PDFJS_ENABLED)) {
Filtering.registerBeforeRequestFilteringCB(onBeforeRequest)
}
}
12 changes: 10 additions & 2 deletions js/lib/urlutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,16 @@ const UrlUtil = {
* @return {string}
*/
getLocationIfPDF: function (url) {
if (url && url.startsWith(`chrome-extension://${pdfjsExtensionId}/`)) {
return url.replace(`chrome-extension://${pdfjsExtensionId}/`, '')
const fileUrl = require('./appUrlUtil').fileUrl
const pdfjsBaseUrl = `chrome-extension://${pdfjsExtensionId}/`
if (url && url.startsWith(pdfjsBaseUrl)) {
url = url.replace(pdfjsBaseUrl, '')
if (url.startsWith(`${pdfjsBaseUrl}tmp/`)) {
// This is a file: URL loaded by PDFJS. The url hash is the actual
// file path on disk.
let hash = urlParse(url).hash
url = hash ? fileUrl(hash.replace('#', '')) : url
}
}
return url
},
Expand Down
4 changes: 4 additions & 0 deletions test/unit/lib/urlutilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ describe('urlutil', function () {
assert.equal(UrlUtil.getLocationIfPDF('chrome-extension://blank'), 'chrome-extension://blank')
assert.equal(UrlUtil.getLocationIfPDF(null), null)
})
it('gets location for file: PDF URL', function () {
let url = 'chrome-extension://jdbefljfgobbmcidnmpjamcbhnbphjnb/chrome-extension://jdbefljfgobbmcidnmpjamcbhnbphjnb/tmp/test.pdf#/Users/yan/Downloads/test.pdf'
assert.equal(UrlUtil.getLocationIfPDF(url), 'file:///Users/yan/Downloads/test.pdf')
})
})

describe('getDisplayLocation', function () {
Expand Down