2020-05-19 09:02:56 +00:00
|
|
|
import App from '../base'
|
|
|
|
App.controller(
|
|
|
|
'TokenAccessPageController',
|
|
|
|
($scope, $http, $location, localStorage) => {
|
|
|
|
window.S = $scope
|
|
|
|
$scope.mode = 'accessAttempt' // 'accessAttempt' | 'v1Import'
|
2020-02-25 09:39:53 +00:00
|
|
|
|
2020-05-19 09:02:56 +00:00
|
|
|
$scope.v1ImportData = null
|
2020-02-25 09:39:53 +00:00
|
|
|
|
2020-05-19 09:02:56 +00:00
|
|
|
$scope.accessInFlight = false
|
|
|
|
$scope.accessSuccess = false
|
|
|
|
$scope.accessError = false
|
2020-02-25 09:39:53 +00:00
|
|
|
|
2020-05-19 09:02:56 +00:00
|
|
|
$scope.currentPath = () => {
|
|
|
|
return $location.path()
|
|
|
|
}
|
2020-02-25 09:39:53 +00:00
|
|
|
|
2020-05-19 09:02:56 +00:00
|
|
|
$scope.buildZipDownloadPath = projectId => {
|
|
|
|
return `/overleaf/project/${projectId}/download/zip`
|
|
|
|
}
|
2020-02-25 09:39:53 +00:00
|
|
|
|
2020-05-19 09:02:56 +00:00
|
|
|
$scope.getProjectName = () => {
|
|
|
|
if (!$scope.v1ImportData || !$scope.v1ImportData.name) {
|
|
|
|
return 'This project'
|
|
|
|
} else {
|
|
|
|
return $scope.v1ImportData.name
|
2020-02-25 09:39:53 +00:00
|
|
|
}
|
2020-05-19 09:02:56 +00:00
|
|
|
}
|
2020-02-25 09:39:53 +00:00
|
|
|
|
2020-05-19 09:02:56 +00:00
|
|
|
$scope.post = () => {
|
|
|
|
$scope.mode = 'accessAttempt'
|
|
|
|
const textData = $('#overleaf-token-access-data').text()
|
2021-05-05 13:05:04 +00:00
|
|
|
const parsedData = JSON.parse(textData)
|
2020-05-19 09:02:56 +00:00
|
|
|
const { postUrl, csrfToken } = parsedData
|
|
|
|
$scope.accessInFlight = true
|
2020-02-25 09:39:53 +00:00
|
|
|
|
2020-05-19 09:02:56 +00:00
|
|
|
$http({
|
|
|
|
method: 'POST',
|
|
|
|
url: postUrl,
|
|
|
|
data: {
|
2021-04-27 07:52:58 +00:00
|
|
|
_csrf: csrfToken,
|
|
|
|
},
|
2020-05-19 09:02:56 +00:00
|
|
|
}).then(
|
|
|
|
function successCallback(response) {
|
|
|
|
$scope.accessInFlight = false
|
|
|
|
$scope.accessError = false
|
|
|
|
const { data } = response
|
|
|
|
if (data.redirect) {
|
|
|
|
const redirect = response.data.redirect
|
|
|
|
if (!redirect) {
|
2020-02-25 09:39:53 +00:00
|
|
|
console.warn(
|
2020-05-19 09:02:56 +00:00
|
|
|
'no redirect supplied in success response data',
|
2020-02-25 09:39:53 +00:00
|
|
|
response
|
|
|
|
)
|
|
|
|
$scope.accessError = true
|
2020-05-19 09:02:56 +00:00
|
|
|
return
|
2020-02-25 09:39:53 +00:00
|
|
|
}
|
2020-05-19 09:02:56 +00:00
|
|
|
window.location.replace(redirect)
|
|
|
|
} else if (data.v1Import) {
|
|
|
|
$scope.mode = 'v1Import'
|
|
|
|
$scope.v1ImportData = data.v1Import
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
'invalid data from server in success response',
|
|
|
|
response
|
|
|
|
)
|
|
|
|
$scope.accessError = true
|
2020-02-25 09:39:53 +00:00
|
|
|
}
|
2020-05-19 09:02:56 +00:00
|
|
|
},
|
|
|
|
function errorCallback(response) {
|
|
|
|
console.warn('error response from server', response)
|
|
|
|
$scope.accessInFlight = false
|
|
|
|
$scope.accessError = response.status === 404 ? 'not_found' : 'error'
|
|
|
|
}
|
|
|
|
)
|
2020-02-25 09:39:53 +00:00
|
|
|
}
|
2020-05-19 09:02:56 +00:00
|
|
|
}
|
|
|
|
)
|