Skip to content

Commit

Permalink
Merge df680db into a15d174
Browse files Browse the repository at this point in the history
  • Loading branch information
Pchelolo committed Aug 2, 2018
2 parents a15d174 + df680db commit b846ae6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 31 deletions.
1 change: 1 addition & 0 deletions config.example.wikimedia.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ default_project: &default_project
# Cache PDF for 5 minutes since it's not purged
cache_control: s-maxage=600, max-age=600
uri: https://pdfrender-beta.wmflabs.org
new_uri: https://proton-beta.wmflabs.org
secret: secret
scheme: https
transform:
Expand Down
1 change: 1 addition & 0 deletions config.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ default_project: &default_project
# Cache PDF for 5 minutes since it's not purged
cache_control: s-maxage=600, max-age=600
uri: https://pdfrender-beta.wmflabs.org
new_uri: https://proton-beta.wmflabs.org
secret: secret
transform:
cx_host: https://cxserver-beta.wmflabs.org
Expand Down
52 changes: 45 additions & 7 deletions v1/pdf.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,59 @@
'use strict';

const HyperSwitch = require('hyperswitch');
const URI = HyperSwitch.URI;
const spec = HyperSwitch.utils.loadSpec(`${__dirname}/pdf.yaml`);

function filenameParameters(name) {
// Return two parameters
const encodedName = `${encodeURIComponent(name)}.pdf`;
const quotedName = `"${encodedName.replace(/"/g, '\\"')}"`;
return `filename=${quotedName}; filename*=UTF-8''${encodedName}`;
}

/**
* PDF filename formatting / escaping utilities.
*/

module.exports = options => ({
spec,
globals: {
options,
filenameParameters(name) {
// Return two parameters
const encodedName = `${encodeURIComponent(name)}.pdf`;
const quotedName = `"${encodedName.replace(/"/g, '\\"')}"`;
return `filename=${quotedName}; filename*=UTF-8''${encodedName}`;
options
},
operations: {
generatePDF: (hyper, req) => {
const rp = req.params;
return hyper.get(new URI([rp.domain, 'sys', 'page_revisions', 'page', rp.title]))
.then((latestRevision) => {
if (options.new_uri) {
hyper.get(new URI(
`${options.new_uri}/${rp.domain}/v1/`
+ `pdf/${encodeURIComponent(rp.title)}/a4/desktop`
))
.catch(e => hyper.logger.log('error/proton', e));
}
return hyper.get({
uri: new URI(`${options.uri}/pdf`),
query: {
accessKey: options.secret,
url: `${options.scheme || 'https'}://${rp.domain}/wiki/`
+ `${encodeURIComponent(rp.title)}?printable=yes`
}
})
.then((res) => {
return {
status: 200,
headers: {
'content-disposition': `attachment; ${filenameParameters(rp.title)}`,
'content-type': res.headers['content-type'],
'content-length': res.headers['content-length'],
'cache-control': options['cache-control']
|| "s-maxage=600, max-age=600",
etag: latestRevision.headers.etag
},
body: res.body
};
});
});
}
}
});
48 changes: 24 additions & 24 deletions v1/pdf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,27 @@ paths:
description: Error
schema:
$ref: '#/definitions/problem'

x-request-handler:
- get_latest_revision:
request:
method: get
uri: /{domain}/sys/page_revisions/page/{title}
- get_pdf_from_backend:
request:
method: get
# Note: The title needs to be encoded twice.
uri: '{{options.uri}}/pdf?accessKey={options.secret}&url={{default(options.scheme, "https")}}://{{domain}}/wiki/{_encodeURIComponent(title)}%3Fprintable=yes'
return:
status: 200
headers:
# Firefox supports filename*= syntax while Chrome respect
# filename=. Safari is stupid and understands neither.
# TODO: Quote raw `"` chars in filename.
content-disposition: 'attachment; {{filenameParameters(request.params.title)}}'
content-type: '{{get_pdf_from_backend.headers.content-type}}'
content-length: '{{get_pdf_from_backend.headers.content-length}}'
cache-control: '{{default(options.cache_control, "s-maxage=600, max-age=600")}}'
etag: '{{get_latest_revision.headers.etag}}'
body: '{{get_pdf_from_backend.body}}'
x-monitor: false # PDF generation is expensive and it's not stored, so don't run checker script
operationId: 'generatePDF'
# x-request-handler:
# - get_latest_revision:
# request:
# method: get
# uri: /{domain}/sys/page_revisions/page/{title}
# - get_pdf_from_backend:
# request:
# method: get
# # Note: The title needs to be encoded twice.
# uri: '{{options.uri}}/pdf?accessKey={options.secret}&url={{default(options.scheme, "https")}}://{{domain}}/wiki/{_encodeURIComponent(title)}%3Fprintable=yes'
# return:
# status: 200
# headers:
# # Firefox supports filename*= syntax while Chrome respect
# # filename=. Safari is stupid and understands neither.
# # TODO: Quote raw `"` chars in filename.
# content-disposition: 'attachment; {{filenameParameters(request.params.title)}}'
# content-type: '{{get_pdf_from_backend.headers.content-type}}'
# content-length: '{{get_pdf_from_backend.headers.content-length}}'
# cache-control: '{{default(options.cache_control, "s-maxage=600, max-age=600")}}'
# etag: '{{get_latest_revision.headers.etag}}'
# body: '{{get_pdf_from_backend.body}}'
# x-monitor: false # PDF generation is expensive and it's not stored, so don't run checker script

0 comments on commit b846ae6

Please sign in to comment.