BinaryFileController.js 5.8 KB

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