BinaryFileController.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. MAX_URL_LENGTH = 60
  29. FRONT_OF_URL_LENGTH = 35
  30. FILLER = '...'
  31. TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length
  32. $scope.displayUrl = (url) ->
  33. if !url?
  34. return
  35. if url.length > MAX_URL_LENGTH
  36. front = url.slice(0, FRONT_OF_URL_LENGTH)
  37. tail = url.slice(url.length - TAIL_OF_URL_LENGTH)
  38. return front + FILLER + tail
  39. else
  40. return url
  41. $scope.refreshFile = (file) ->
  42. $scope.refreshing = true
  43. ide.fileTreeManager.refreshLinkedFile(file)
  44. .then (response) ->
  45. { data } = response
  46. new_file_id = data.new_file_id
  47. $timeout(
  48. () ->
  49. ide.binaryFilesManager.openFileById(new_file_id)
  50. , 1000
  51. )
  52. .finally () ->
  53. $scope.refreshing = false
  54. # Callback fired when the `img` tag fails to load,
  55. # `failedLoad` used to show the "No Preview" message
  56. $scope.failedLoad = false
  57. window.sl_binaryFilePreviewError = () =>
  58. $scope.failedLoad = true
  59. $scope.$apply()
  60. # Callback fired when the `img` tag is done loading,
  61. # `imgLoaded` is used to show the spinner gif while loading
  62. $scope.imgLoaded = false
  63. window.sl_binaryFilePreviewLoaded = () =>
  64. $scope.imgLoaded = true
  65. $scope.$apply()
  66. do loadTextFileFilePreview = () ->
  67. return unless $scope.isTextFile()
  68. url = "/project/#{project_id}/file/#{$scope.openFile.id}?range=0-#{TWO_MEGABYTES}"
  69. $scope.textPreview.data = null
  70. $scope.textPreview.loading = true
  71. $scope.textPreview.shouldShowDots = false
  72. $scope.$apply()
  73. $http({
  74. url: url,
  75. method: 'GET',
  76. transformResponse: null # Don't parse JSON
  77. })
  78. .then (response) ->
  79. { data } = response
  80. $scope.textPreview.error = false
  81. # show dots when payload is closs to cutoff
  82. if data.length >= (TWO_MEGABYTES - 200)
  83. $scope.textPreview.shouldShowDots = true
  84. try
  85. # remove last partial line
  86. data = data.replace(/\n.*$/, '')
  87. finally
  88. $scope.textPreview.data = data
  89. $timeout(setHeight, 0)
  90. .catch (error) ->
  91. console.error(error)
  92. $scope.textPreview.error = true
  93. $scope.textPreview.loading = false
  94. setHeight = () ->
  95. $preview = $element.find('.text-preview .scroll-container')
  96. $footer = $element.find('.binary-file-footer')
  97. maxHeight = $element.height() - $footer.height() - 14 # borders + margin
  98. $preview.css('max-height': maxHeight)
  99. # Don't show the preview until we've set the height, otherwise we jump around
  100. $scope.textPreview.loading = false
  101. ]