summaryrefslogtreecommitdiff
path: root/app/client/common/fileViewer.component.js
blob: 4938650a6582b1c4fa06a329263f3e6dc469ba7a (plain)
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
import { h, Component } from 'preact'
import { connect } from 'react-redux'

import actions from '../actions'

const image_types = {
  'jpg': 'image/jpeg',
  'jpeg': 'image/jpeg',
  'png': 'image/png',
  'gif': 'image/gif',
}

const audio_types = {
  'wav': 'audio/wav',
  'mp3': 'audio/mp3',
  'flac': 'audio/flac',
  'aiff': 'audio/aiff',
}

const video_types = {
  'mp4': 'video/mp4',
}

const THROTTLE_FETCH_TIME = 200

class FileViewer extends Component {
  state = {
    loading: false,
    stale: false,
    buffer: {},
    url: null,
  }

  componentDidMount(){
    this.fetch()
  }

  componentDidUpdate(prevProps){
    if (this.props.file !== prevProps.file) {
      this.deferFetch()
    }
  }

  componentWillUnmount(){
    if (this.state.url) {
      window.URL.revokeObjectURL(this.state.url)
    }
  }

  deferFetch(){
    clearTimeout(this.timeout)
    this.timeout = setTimeout(() => {
      this.fetch()
    }, THROTTLE_FETCH_TIME)
  }

  fetch() {
    const { file, path, thumbnail } = this.props
    if (!file) return
    if (this.state.loading) {
      this.setState({ stale: true })
      return
    }
    const fn = [path || file.path, file.name].join('/').replace('//','/')
    const { tool: module } = this.props.app
    this.setState({ buffer: null, loading: true })

    if (thumbnail) {
      // console.log('fetch thumbnail', fn)
      const size = parseInt(thumbnail) || 200
      actions.socket
        .thumbnail({ module, fn, size })
        .then(this.loadBuffer.bind(this))
    } else {
      // console.log('fetch file', fn)
      actions.socket
        .read_file({ module, fn })
        .then(this.loadBuffer.bind(this))
    }
  }

  loadBuffer(buffer) {
    // console.log('fetched buffer', buffer)
    const { name, buf } = buffer
    const ext = extension(name)
    if (this.state.url) {
      window.URL.revokeObjectURL(this.state.url)
    }
    let url
    if (buf) {
      if (ext in image_types) {
        url = getURLFor(buf, image_types[ext])
      } else if (ext in audio_types) {
        url = getURLFor(buf, audio_types[ext])
      } else if (ext in video_types) {
        url = getURLFor(buf, video_types[ext])
      } else {
        url = ab2str(buf)
      }
    }
    const { stale } = this.state
    this.setState({ ext, url, buffer, loading: false, stale: false, }, () => {
      // console.log('loaded')
      if (stale) {
        // console.log('stale, fetching...')
        this.fetch()
      }
    })
  }

  render() {
    const { file } = this.props
    if (!file) {
      return <div className='fileViewer'></div>
    }
    const { loading, buffer, url, ext } = this.state
    if (loading) {
      return <div className='fileViewer'><span>Loading...</span></div>
    }
    const {
      error,
      name, path,
      date, size,
      buf,
    } = buffer
    if (error) {
      return <div className='fileViewer'><span>{error}</span></div>
    }
    if (!name) {
      return <div className='fileViewer'></div>
    }
    if (!buf || !url) {
      return <div className='fileViewer'><span>File empty</span></div>
    }
    let tag;
    if (ext in image_types) {
      tag = <img src={url} />
    } else if (ext in audio_types) {
      tag = <audio src={url} controls autoplay />
    } else if (ext in video_types) {
      tag = <video src={url} controls autoplay />
    } else {
      tag = <div className='text'>{url}</div>
    }
    return (
      <div className='fileViewer'>{tag}</div>
    )
  }
}

const getURLFor = (buf, type) => {
  const arrayBufferView = new Uint8Array(buf)
  const blob = new Blob([arrayBufferView], { type })
  const urlCreator = window.URL || window.webkitURL
  return urlCreator.createObjectURL(blob)
}

const ab2str = buf => String.fromCharCode.apply(null, new Uint8Array(buf))

const extension = fn => fn.split('.').slice(-1)[0].toLowerCase()

const mapStateToProps = state => ({
  app: state.system.app,
})

export default connect(mapStateToProps)(FileViewer)