BinaryFileController.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. define [
  2. "base"
  3. "moment"
  4. ], (App, moment) ->
  5. App.controller "BinaryFileController", ["$scope", "$rootScope", "$http", "$timeout", "$element", "ide", ($scope, $rootScope, $http, $timeout, $element, ide) ->
  6. TWO_MEGABYTES = 2 * 1024 * 1024
  7. textExtensions = ['bib', 'tex', 'txt', 'cls', 'sty']
  8. imageExtentions = ['png', 'jpg', 'jpeg', 'gif']
  9. previewableExtensions = ['eps', 'pdf']
  10. extension = (file) ->
  11. return file.name.split(".").pop()?.toLowerCase()
  12. $scope.isTextFile = () =>
  13. textExtensions.indexOf(extension($scope.openFile)) > -1
  14. $scope.isImageFile = () =>
  15. imageExtentions.indexOf(extension($scope.openFile)) > -1
  16. $scope.isPreviewableFile = () =>
  17. previewableExtensions.indexOf(extension($scope.openFile)) > -1
  18. $scope.isUnpreviewableFile = () ->
  19. !$scope.isTextFile() and
  20. !$scope.isImageFile() and
  21. !$scope.isPreviewableFile()
  22. $scope.textPreview =
  23. loading: false
  24. shouldShowDots: false
  25. error: false
  26. data: null
  27. $scope.refreshing = false
  28. $scope.refreshError = null
  29. MAX_URL_LENGTH = 60
  30. FRONT_OF_URL_LENGTH = 35
  31. FILLER = '...'
  32. TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length
  33. $scope.displayUrl = (url) ->
  34. if !url?
  35. return
  36. if url.length > MAX_URL_LENGTH
  37. front = url.slice(0, FRONT_OF_URL_LENGTH)
  38. tail = url.slice(url.length - TAIL_OF_URL_LENGTH)
  39. return front + FILLER + tail
  40. else
  41. return url
  42. _tryOpenFile = (new_file_id) ->
  43. iterations = 0
  44. do tryOpen = () ->
  45. if iterations > 10
  46. return
  47. iterations += 1
  48. newFile = ide.fileTreeManager.findEntityById(new_file_id)
  49. if newFile?
  50. ide.binaryFilesManager.openFile(newFile)
  51. else
  52. setTimeout(tryOpen, 500)
  53. $scope.refreshFile = (file) ->
  54. $scope.refreshing = true
  55. $scope.refreshError = null
  56. ide.fileTreeManager.refreshLinkedFile(file)
  57. .then (response) ->
  58. { data } = response
  59. { new_file_id } = data
  60. $timeout(
  61. () ->
  62. _tryOpenFile(new_file_id)
  63. , 0
  64. )
  65. $scope.refreshError = null
  66. .catch (response) ->
  67. $scope.refreshError = response.data
  68. .finally () ->
  69. $scope.refreshing = false
  70. # Callback fired when the `img` tag fails to load,
  71. # `failedLoad` used to show the "No Preview" message
  72. $scope.failedLoad = false
  73. window.sl_binaryFilePreviewError = () =>
  74. $scope.failedLoad = true
  75. $scope.$apply()
  76. # Callback fired when the `img` tag is done loading,
  77. # `imgLoaded` is used to show the spinner gif while loading
  78. $scope.imgLoaded = false
  79. window.sl_binaryFilePreviewLoaded = () =>
  80. $scope.imgLoaded = true
  81. $scope.$apply()
  82. do loadTextFileFilePreview = () ->
  83. return unless $scope.isTextFile()
  84. url = "/project/#{project_id}/file/#{$scope.openFile.id}?range=0-#{TWO_MEGABYTES}"
  85. $scope.textPreview.data = null
  86. $scope.textPreview.loading = true
  87. $scope.textPreview.shouldShowDots = false
  88. $scope.$apply()
  89. $http({
  90. url: url,
  91. method: 'GET',
  92. transformResponse: null # Don't parse JSON
  93. })
  94. .then (response) ->
  95. { data } = response
  96. $scope.textPreview.error = false
  97. # show dots when payload is closs to cutoff
  98. if data.length >= (TWO_MEGABYTES - 200)
  99. $scope.textPreview.shouldShowDots = true
  100. # remove last partial line
  101. data = data.replace(/\n.*$/, '')
  102. $scope.textPreview.data = data
  103. $timeout(setHeight, 0)
  104. .catch (error) ->
  105. console.error(error)
  106. $scope.textPreview.error = true
  107. $scope.textPreview.loading = false
  108. setHeight = () ->
  109. $preview = $element.find('.text-preview .scroll-container')
  110. $footer = $element.find('.binary-file-footer')
  111. maxHeight = $element.height() - $footer.height() - 14 # borders + margin
  112. $preview.css('max-height': maxHeight)
  113. # Don't show the preview until we've set the height, otherwise we jump around
  114. $scope.textPreview.loading = false
  115. ]