BinaryFileController.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. define [
  2. "base"
  3. "moment"
  4. ], (App, moment) ->
  5. App.controller "BinaryFileController", ["$scope", "$rootScope", "$http", "$timeout", "$element", ($scope, $rootScope, $http, $timeout, $element) ->
  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. MAX_URL_LENGTH = 60
  28. FRONT_OF_URL_LENGTH = 35
  29. FILLER = '...'
  30. TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length
  31. $scope.displayUrl = (url) ->
  32. if url.length > MAX_URL_LENGTH
  33. front = url.slice(0, FRONT_OF_URL_LENGTH)
  34. tail = url.slice(url.length - TAIL_OF_URL_LENGTH)
  35. return front + FILLER + tail
  36. else
  37. return url
  38. # Callback fired when the `img` tag fails to load,
  39. # `failedLoad` used to show the "No Preview" message
  40. $scope.failedLoad = false
  41. window.sl_binaryFilePreviewError = () =>
  42. $scope.failedLoad = true
  43. $scope.$apply()
  44. # Callback fired when the `img` tag is done loading,
  45. # `imgLoaded` is used to show the spinner gif while loading
  46. $scope.imgLoaded = false
  47. window.sl_binaryFilePreviewLoaded = () =>
  48. $scope.imgLoaded = true
  49. $scope.$apply()
  50. loadTextFileFilePreview = () ->
  51. url = "/project/#{project_id}/file/#{$scope.openFile.id}?range=0-#{TWO_MEGABYTES}"
  52. $scope.textPreview.loading = true
  53. $scope.textPreview.shouldShowDots = false
  54. $scope.$apply()
  55. $http.get(url)
  56. .then (response) ->
  57. { data } = response
  58. $scope.textPreview.error = false
  59. # show dots when payload is closs to cutoff
  60. if data.length >= (TWO_MEGABYTES - 200)
  61. $scope.textPreview.shouldShowDots = true
  62. try
  63. # remove last partial line
  64. data = data.replace(/\n.*$/, '')
  65. finally
  66. $scope.textPreview.data = data
  67. $timeout(setHeight, 0)
  68. .catch () ->
  69. $scope.textPreview.error = true
  70. $scope.textPreview.loading = false
  71. setHeight = () ->
  72. $preview = $element.find('.text-preview .scroll-container')
  73. $footer = $element.find('.binary-file-footer')
  74. maxHeight = $element.height() - $footer.height() - 14 # borders + margin
  75. $preview.css('max-height': maxHeight)
  76. # Don't show the preview until we've set the height, otherwise we jump around
  77. $scope.textPreview.loading = false
  78. do loadTextFileIfRequired = () ->
  79. if $scope.isTextFile()
  80. $scope.textPreview.data = null
  81. loadTextFileFilePreview()
  82. ]