overleaf/services/web/public/coffee/ide/binary-files/controllers/BinaryFileController.coffee

130 lines
3.8 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
"base"
2018-02-20 05:37:55 -05:00
"moment"
], (App, moment) ->
2018-02-20 06:49:02 -05:00
App.controller "BinaryFileController", ["$scope", "$rootScope", "$http", "$timeout", "$element", "ide", ($scope, $rootScope, $http, $timeout, $element, ide) ->
2016-05-17 12:00:14 -04:00
TWO_MEGABYTES = 2 * 1024 * 1024
2018-02-20 05:37:55 -05:00
textExtensions = ['bib', 'tex', 'txt', 'cls', 'sty']
imageExtentions = ['png', 'jpg', 'jpeg', 'gif']
previewableExtensions = ['eps', 'pdf']
extension = (file) ->
return file.name.split(".").pop()?.toLowerCase()
$scope.isTextFile = () =>
textExtensions.indexOf(extension($scope.openFile)) > -1
$scope.isImageFile = () =>
imageExtentions.indexOf(extension($scope.openFile)) > -1
$scope.isPreviewableFile = () =>
previewableExtensions.indexOf(extension($scope.openFile)) > -1
$scope.isUnpreviewableFile = () ->
!$scope.isTextFile() and
!$scope.isImageFile() and
!$scope.isPreviewableFile()
$scope.textPreview =
2016-05-17 12:00:14 -04:00
loading: false
shouldShowDots: false
2016-05-17 12:00:14 -04:00
error: false
2016-05-19 06:11:23 -04:00
data: null
2018-02-20 06:49:02 -05:00
$scope.refreshing = false
2018-05-21 06:02:12 -04:00
$scope.refreshError = null
2018-02-20 06:49:02 -05:00
2018-02-20 05:37:55 -05:00
MAX_URL_LENGTH = 60
FRONT_OF_URL_LENGTH = 35
FILLER = '...'
TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length
$scope.displayUrl = (url) ->
if !url?
return
2018-02-20 05:37:55 -05:00
if url.length > MAX_URL_LENGTH
front = url.slice(0, FRONT_OF_URL_LENGTH)
tail = url.slice(url.length - TAIL_OF_URL_LENGTH)
return front + FILLER + tail
else
return url
_tryOpenFile = (new_file_id) ->
iterations = 0
do tryOpen = () ->
if iterations > 10
return
iterations += 1
newFile = ide.fileTreeManager.findEntityById(new_file_id)
if newFile?
ide.binaryFilesManager.openFile(newFile)
else
setTimeout(tryOpen, 500)
2018-02-20 06:49:02 -05:00
$scope.refreshFile = (file) ->
$scope.refreshing = true
2018-05-21 06:02:12 -04:00
$scope.refreshError = null
2018-02-20 06:49:02 -05:00
ide.fileTreeManager.refreshLinkedFile(file)
.then (response) ->
{ data } = response
2018-05-18 06:07:59 -04:00
{ new_file_id } = data
$timeout(
() ->
_tryOpenFile(new_file_id)
, 0
)
2018-05-21 06:02:12 -04:00
$scope.refreshError = null
.catch (response) ->
$scope.refreshError = response.data
2018-02-20 06:49:02 -05:00
.finally () ->
$scope.refreshing = false
# Callback fired when the `img` tag fails to load,
# `failedLoad` used to show the "No Preview" message
$scope.failedLoad = false
window.sl_binaryFilePreviewError = () =>
$scope.failedLoad = true
$scope.$apply()
# Callback fired when the `img` tag is done loading,
# `imgLoaded` is used to show the spinner gif while loading
$scope.imgLoaded = false
window.sl_binaryFilePreviewLoaded = () =>
$scope.imgLoaded = true
$scope.$apply()
2018-02-20 06:49:02 -05:00
do loadTextFileFilePreview = () ->
return unless $scope.isTextFile()
url = "/project/#{project_id}/file/#{$scope.openFile.id}?range=0-#{TWO_MEGABYTES}"
2018-02-20 06:49:02 -05:00
$scope.textPreview.data = null
2018-02-20 05:37:55 -05:00
$scope.textPreview.loading = true
$scope.textPreview.shouldShowDots = false
2016-05-19 06:11:23 -04:00
$scope.$apply()
2018-02-20 06:49:02 -05:00
$http({
url: url,
method: 'GET',
transformResponse: null # Don't parse JSON
})
.then (response) ->
{ data } = response
2018-02-20 05:37:55 -05:00
$scope.textPreview.error = false
# show dots when payload is closs to cutoff
if data.length >= (TWO_MEGABYTES - 200)
2018-02-20 05:37:55 -05:00
$scope.textPreview.shouldShowDots = true
2016-05-17 12:00:14 -04:00
# remove last partial line
2018-05-24 06:35:55 -04:00
data = data?.replace?(/\n.*$/, '')
$scope.textPreview.data = data
2018-02-20 05:37:55 -05:00
$timeout(setHeight, 0)
2018-02-20 06:49:02 -05:00
.catch (error) ->
console.error(error)
2018-02-20 05:37:55 -05:00
$scope.textPreview.error = true
$scope.textPreview.loading = false
2016-05-19 06:11:23 -04:00
2018-02-20 05:37:55 -05:00
setHeight = () ->
$preview = $element.find('.text-preview .scroll-container')
$footer = $element.find('.binary-file-footer')
maxHeight = $element.height() - $footer.height() - 14 # borders + margin
$preview.css('max-height': maxHeight)
# Don't show the preview until we've set the height, otherwise we jump around
$scope.textPreview.loading = false
]