All files / src/gadgets/file-attachment/attachment-preview index.js

100% Statements 50/50
100% Branches 16/16
100% Functions 19/19
100% Lines 50/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                              5x 5x   5x   2x 1x 1x       2x                               5x 5x       1x 1x           1x 1x         19x 19x   19x 19x 7x 12x 2x 2x 10x 9x 9x   1x   19x         19x                             2x 2x                 2x 2x 2x               9x 9x             1x 1x             9x 9x 9x           1x                           2x 1x           1x                     1x             1x                             1x 1x 1x                 7x       5x            
/* Copyright © 2005-2017 Kuali, Inc. - All Rights Reserved
 * You may use and modify this code under the terms of the Kuali, Inc.
 * Pre-Release License Agreement. You may not distribute it.
 *
 * You should have received a copy of the Kuali, Inc. Pre-Release License
 * Agreement with this file. If not, please write to license@kuali.co.
 */
 
import axios from 'axios'
import cookies from 'cookies-js'
import { includes } from 'lodash'
import Pdf from './pdf'
import React from 'react'
import { Dialog, FlatButton, Icon } from '@kuali/kuali-ui'
 
const imageSupportedContentTypes = ['image/jpeg', 'image/png', 'image/gif']
const pdfSupportedContentTypes = ['application/pdf']
 
export const DownloadButton = props => {
  let onClick
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    onClick = () => {
      window.navigator.msSaveOrOpenBlob(props.blob, props.fileName)
    }
  }
 
  return (
    <FlatButton
      href={props.url}
      download={props.fileName || true}
      component='a'
      label='Download'
      labelStyle={{ display: 'none' }}
      onClick={onClick}
    >
      <Icon name='file_download' />
    </FlatButton>
  )
}
 
class AttachmentPreview extends React.Component {
  constructor (props) {
    super(props)
    this.state = { page: 1, scale: 1, pages: 1 }
  }
 
  componentDidMount () {
    const { url } = this.props
    return axios
      .get(url, {
        headers: { Authorization: `Bearer ${cookies.get('authToken')}` },
        responseType: 'blob'
      })
      .then(response => {
        const objectUrl = URL.createObjectURL(response.data) // eslint-disable-line no-undef
        this.setState({ url: objectUrl, blob: response.data })
      })
  }
 
  render () {
    const { url } = this.state
    const { file, onClose } = this.props
    let preview
    let actions = []
    if (!url) {
      preview = this.renderLoading()
    } else if (includes(imageSupportedContentTypes, file.contentType)) {
      preview = this.renderImagePreview()
      actions = this.imagePreviewActions()
    } else if (includes(pdfSupportedContentTypes, file.contentType)) {
      preview = this.renderPdfPreview()
      actions = this.pdfPreviewActions()
    } else {
      preview = this.renderUnsupportedPreview()
    }
    actions.push(
      <FlatButton onClick={onClose} label='Close'>
        <Icon name='close' variant='warning' />
      </FlatButton>
    )
    return (
      <Dialog
        visible
        actions={actions}
        style={{ paddingTop: 0 }}
        title={file.fileName}
        renderNode={document.body}
        onHide={onClose}
      >
        {preview}
      </Dialog>
    )
  }
 
  renderImagePreview () {
    const { url } = this.state
    return (
      <img
        src={url}
        style={{ maxWidth: '100%', height: 'auto', display: 'inline-block' }}
      />
    )
  }
 
  imagePreviewActions () {
    const { url, blob } = this.state
    const { file } = this.props
    return [
      <span key='download'>
        <DownloadButton url={url} blob={blob} fileName={file.fileName} />
      </span>
    ]
  }
 
  renderPdfPreview () {
    const { page, scale, url } = this.state
    return (
      <span>
        <Pdf
          file={url}
          page={page}
          scale={scale}
          pdfUrl={this.props.pdfUrl}
          onDocumentComplete={numPages => this.setState({ pages: numPages })}
          onPageComplete={newPage => this.setState({ currentPage: newPage })}
        />
      </span>
    )
  }
 
  pdfPreviewActions () {
    const { page, pages, scale, url, blob } = this.state
    const { file } = this.props
    return [
      <span
        key='pages'
        style={{ height: '90px !important', minWidth: '375px' }}
      >
        <FlatButton
          onClick={() => this.setState({ page: page - 1 })}
          label='Previous Page'
          labelStyle={{ display: 'none' }}
          disabled={page === 1}
        >
          <Icon name='arrow_back' variant='success' />
        </FlatButton>
        Page
        <input
          type='text'
          size='2'
          value={page}
          style={{ fontSize: 20 }}
          onChange={e => {
            if (e.target.value && !isNaN(Number(e.target.value))) {
              this.setState({ page: Number(e.target.value) })
            }
          }}
        />
        of {pages}
        <FlatButton
          onClick={() => this.setState({ page: page + 1 })}
          label='Next Page'
          labelStyle={{ display: 'none' }}
          disabled={page >= pages}
          iconBefore={false}
        >
          <Icon name='arrow_forward' variant='success' />
        </FlatButton>
      </span>,
      <span key='zoom' style={{ minWidth: '250px' }}>
        <FlatButton
          onClick={() => this.setState({ scale: scale + 0.1 })}
          label='Zoom In'
          labelStyle={{ display: 'none' }}
        >
          <Icon name='add' />
        </FlatButton>
        <FlatButton
          onClick={() => this.setState({ scale: scale - 0.1 })}
          disabled={scale <= 0.2}
          label='Zoom Out'
          labelStyle={{ display: 'none' }}
        >
          <Icon name='remove' />
        </FlatButton>
      </span>,
      <span key='download'>
        <DownloadButton url={url} blob={blob} fileName={file.fileName} />
      </span>
    ]
  }
 
  renderUnsupportedPreview () {
    const { url, blob } = this.state
    const { file } = this.props
    return (
      <div>
        Unable to preview this attachment
        <DownloadButton url={url} blob={blob} fileName={file.fileName} />
      </div>
    )
  }
 
  renderLoading () {
    return <div>Loading...</div>
  }
}
 
AttachmentPreview.propTypes = {
  file: React.PropTypes.object.isRequired,
  url: React.PropTypes.string.isRequired
}
 
export default AttachmentPreview