BinaryFileController.js 5.4 KB

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