BinaryFileController.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import App from '../../../base'
  2. export default App.controller('BinaryFileController', function(
  3. $scope,
  4. $rootScope,
  5. $http,
  6. $timeout,
  7. $element,
  8. ide,
  9. waitFor
  10. ) {
  11. const MAX_FILE_SIZE = 2 * 1024 * 1024
  12. const MAX_URL_LENGTH = 60
  13. const FRONT_OF_URL_LENGTH = 35
  14. const FILLER = '...'
  15. const TAIL_OF_URL_LENGTH =
  16. MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length
  17. const textExtensions = [
  18. 'tex',
  19. 'latex',
  20. 'sty',
  21. 'cls',
  22. 'bst',
  23. 'bib',
  24. 'bibtex',
  25. 'txt',
  26. 'tikz',
  27. 'rtex',
  28. 'md',
  29. 'asy',
  30. 'latexmkrc',
  31. 'lbx',
  32. 'bbx',
  33. 'cbx',
  34. 'm',
  35. 'lco',
  36. 'dtx',
  37. 'ins',
  38. 'ist',
  39. 'def',
  40. 'clo',
  41. 'ldf',
  42. 'rmd',
  43. 'lua',
  44. 'gv'
  45. ]
  46. const imageExtensions = ['png', 'jpg', 'jpeg', 'gif']
  47. const previewableExtensions = []
  48. const extension = file =>
  49. file.name
  50. .split('.')
  51. .pop()
  52. .toLowerCase()
  53. $scope.isTextFile = () =>
  54. textExtensions.indexOf(extension($scope.openFile)) > -1
  55. $scope.isImageFile = () =>
  56. imageExtensions.indexOf(extension($scope.openFile)) > -1
  57. $scope.isPreviewableFile = () =>
  58. previewableExtensions.indexOf(extension($scope.openFile)) > -1
  59. $scope.isUnpreviewableFile = () =>
  60. !$scope.isTextFile() && !$scope.isImageFile() && !$scope.isPreviewableFile()
  61. $scope.textPreview = {
  62. loading: false,
  63. shouldShowDots: false,
  64. error: false,
  65. data: null
  66. }
  67. $scope.refreshing = false
  68. $scope.refreshError = null
  69. $scope.displayUrl = function(url) {
  70. if (url == null) {
  71. return
  72. }
  73. if (url.length > MAX_URL_LENGTH) {
  74. const front = url.slice(0, FRONT_OF_URL_LENGTH)
  75. const tail = url.slice(url.length - TAIL_OF_URL_LENGTH)
  76. return front + FILLER + tail
  77. }
  78. return url
  79. }
  80. $scope.refreshFile = function(file) {
  81. $scope.refreshing = true
  82. $scope.refreshError = null
  83. ide.fileTreeManager
  84. .refreshLinkedFile(file)
  85. .then(function(response) {
  86. const { data } = response
  87. const newFileId = data.new_file_id
  88. $timeout(
  89. () =>
  90. waitFor(() => ide.fileTreeManager.findEntityById(newFileId), 5000)
  91. .then(newFile => ide.binaryFilesManager.openFile(newFile))
  92. .catch(err => console.warn(err)),
  93. 0
  94. )
  95. $scope.refreshError = null
  96. })
  97. .catch(response => ($scope.refreshError = response.data))
  98. .finally(() => {
  99. $scope.refreshing = false
  100. const provider = file.linkedFileData.provider
  101. if (
  102. provider === 'mendeley' ||
  103. provider === 'zotero' ||
  104. file.name.match(/^.*\.bib$/)
  105. ) {
  106. ide.$scope.$emit('references:should-reindex', {})
  107. }
  108. })
  109. }
  110. // Callback fired when the `img` tag fails to load,
  111. // `failedLoad` used to show the "No Preview" message
  112. $scope.failedLoad = false
  113. window.sl_binaryFilePreviewError = () => {
  114. $scope.failedLoad = true
  115. $scope.$apply()
  116. }
  117. // Callback fired when the `img` tag is done loading,
  118. // `imgLoaded` is used to show the spinner gif while loading
  119. $scope.imgLoaded = false
  120. window.sl_binaryFilePreviewLoaded = () => {
  121. $scope.imgLoaded = true
  122. $scope.$apply()
  123. }
  124. if ($scope.isTextFile()) {
  125. loadTextFilePreview()
  126. }
  127. function loadTextFilePreview() {
  128. const url = `/project/${window.project_id}/file/${$scope.openFile.id}`
  129. let truncated = false
  130. displayPreviewLoading()
  131. getFileSize(url)
  132. .then(fileSize => {
  133. const opts = {}
  134. if (fileSize > MAX_FILE_SIZE) {
  135. truncated = true
  136. opts.maxSize = MAX_FILE_SIZE
  137. }
  138. return getFileContents(url, opts)
  139. })
  140. .then(contents => {
  141. const displayedContents = truncated
  142. ? truncateFileContents(contents)
  143. : contents
  144. displayPreview(displayedContents, truncated)
  145. })
  146. .catch(err => {
  147. console.error(err)
  148. displayPreviewError()
  149. })
  150. }
  151. function getFileSize(url) {
  152. return $http.head(url).then(response => {
  153. const size = parseInt(response.headers('Content-Length'), 10)
  154. if (isNaN(size)) {
  155. throw new Error('Could not parse Content-Length header')
  156. }
  157. return size
  158. })
  159. }
  160. function getFileContents(url, opts = {}) {
  161. const { maxSize } = opts
  162. if (maxSize != null) {
  163. url += `?range=0-${maxSize}`
  164. }
  165. return $http
  166. .get(url, {
  167. transformResponse: null // Don't parse JSON
  168. })
  169. .then(response => {
  170. return response.data
  171. })
  172. }
  173. function truncateFileContents(contents) {
  174. return contents.replace(/\n.*$/, '')
  175. }
  176. function displayPreviewLoading() {
  177. $scope.textPreview.data = null
  178. $scope.textPreview.loading = true
  179. $scope.textPreview.shouldShowDots = false
  180. $scope.$apply()
  181. }
  182. function displayPreview(contents, truncated) {
  183. $scope.textPreview.error = false
  184. $scope.textPreview.data = contents
  185. $scope.textPreview.shouldShowDots = truncated
  186. $timeout(setPreviewHeight, 0)
  187. }
  188. function displayPreviewError() {
  189. $scope.textPreview.error = true
  190. $scope.textPreview.loading = false
  191. }
  192. function setPreviewHeight() {
  193. const $preview = $element.find('.text-preview .scroll-container')
  194. const $header = $element.find('.binary-file-header')
  195. const maxHeight = $element.height() - $header.height() - 14 // borders + margin
  196. $preview.css({ 'max-height': maxHeight })
  197. // Don't show the preview until we've set the height, otherwise we jump around
  198. $scope.textPreview.loading = false
  199. }
  200. })