Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF preview: count pages again #2621

Merged
merged 2 commits into from
Jan 18, 2022
Merged
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
8 changes: 5 additions & 3 deletions catalog/app/components/Preview/loaders/Pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ async function loadPdf({ endpoint, sign, handle }) {
input: 'pdf',
output: 'raw',
size: 'w1024h768',
countPages: true,
})
const r = await fetch(`${endpoint}/thumbnail${search}`)
if (r.status >= 400) {
const text = await r.text()
throw new HTTPError(r, text)
}
const { page_count: pages } = JSON.parse(r.headers.get('X-Quilt-Info') || '{}')
const firstPageBlob = await r.blob()
return PreviewData.Pdf({ handle, firstPageBlob })
return PreviewData.Pdf({ handle, pages, firstPageBlob })
} catch (e) {
if (e instanceof HTTPError && e.json?.error === 'Forbidden') {
if (e.json.text?.match(utils.GLACIER_ERROR_RE)) {
if (e instanceof HTTPError && e.json && e.json.error === 'Forbidden') {
if (e.json.text && e.json.text.match(utils.GLACIER_ERROR_RE)) {
throw PreviewError.Archived({ handle })
}
throw PreviewError.Forbidden({ handle })
Expand Down
19 changes: 14 additions & 5 deletions catalog/app/components/Preview/renderers/Pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const useStyles = M.makeStyles((t) => ({
},
}))

function Pdf({ handle, firstPageBlob }, { className, ...props }) {
function Pdf({ handle, firstPageBlob, pages }, { className, ...props }) {
const endpoint = Config.use().binaryApiGatewayEndpoint
const sign = AWS.Signer.useS3Signer()
const classes = useStyles()
Expand All @@ -130,9 +130,10 @@ function Pdf({ handle, firstPageBlob }, { className, ...props }) {
const src = useBlob(blob)

const next = React.useCallback(() => {
if (page >= pages) return
setPage(page + 1)
setPageValue(page + 1)
}, [setPage, setPageValue, page])
}, [setPage, setPageValue, page, pages])

const prev = React.useCallback(() => {
if (page <= 1) return
Expand All @@ -152,12 +153,15 @@ function Pdf({ handle, firstPageBlob }, { className, ...props }) {
const parsed = parseInt(val, 10)
if (Number.isNaN(parsed) || parsed < 1) {
setPageValue(page)
} else if (parsed > pages) {
setPage(pages)
setPageValue(pages)
} else {
setPage(parsed)
setPageValue(parsed)
}
},
[setPage, setPageValue, page],
[setPage, setPageValue, page, pages],
)

const handleBlur = React.useCallback(
Expand Down Expand Up @@ -213,9 +217,14 @@ function Pdf({ handle, firstPageBlob }, { className, ...props }) {
onChange={handlePageChange}
onBlur={handleBlur}
onKeyDown={handleKey}
/>
/>{' '}
/ {pages}
</M.Typography>
<M.IconButton onClick={next} className={cx(classes.btn, classes.next)}>
<M.IconButton
onClick={next}
disabled={page >= pages}
className={cx(classes.btn, classes.next)}
>
<M.Icon fontSize="inherit">chevron_right</M.Icon>
</M.IconButton>
</div>
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

## Catalog, Lambdas
* [Changed] `pkgpush` lambda now uses dedicated lambda for hashing files to push larger packages faster from catalog.
* [Changed] PDF preview: count pages again ([#2621](https://github.com/quiltdata/quilt/pull/2621))

## CLI

Expand Down