This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
api.js
186 lines (169 loc) · 5.5 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* @description This module communicates with Lychee's API
*/
/**
* @callback APISuccessCB
* @param {Object} data the decoded JSON response
* @returns {void}
*/
/**
* @callback APIErrorCB
* @param {XMLHttpRequest} jqXHR the jQuery XMLHttpRequest object, see {@link https://api.jquery.com/jQuery.ajax/#jqXHR}.
* @param {Object} params the original JSON parameters of the request
* @param {?LycheeException} lycheeException the Lychee exception
* @returns {boolean} `true`, if the callback has already handled the error
* and does not want the API layer to handle the error any
* further (i.e. show an error flash);
* `false`, if the API layer should handle the error, too.
*/
/**
* @callback APIProgressCB
* @param {ProgressEvent} event the progress event
* @returns {void}
*/
/**
* The main API object
*/
let api = {
/**
* Global, default error handler
*
* @type {?APIErrorCB}
*/
onError: null,
};
/**
* Checks whether the returned error is probably due to an expired HTTP session.
*
* There seem to be two variants how an expired session may be reported:
*
* 1. The web-application has already been loaded, is fully initialized
* and a user tries to navigate to another part of the gallery.
* In this case, the AJAX request sends the previous, expired CSRF token
* and the backend responds with a 419 status code.
* 2. The user completely reloads the website (e.g. typically be hitting
* F5 in most browsers).
* In this case, the CSRF token is re-generated by the backend, so no
* CSRF mismatch occurs, but the user is no longer authenticated. and the
* backend responds with a 401 status code.
*
* Note, case 2 also happens if a user directly navigates to a link
* of the form `#/album-id/` or `#/album-id/photo-id` unless the album is
* public, but password protected.
* In that case, the backend also sends a 401 status code, but with a
* special "Password Required" exception which is handled specially in
* `album.js`.
*
* @param {XMLHttpRequest} jqXHR the jQuery XMLHttpRequest object, see {@link https://api.jquery.com/jQuery.ajax/#jqXHR}.
* @param {?LycheeException} lycheeException the Lychee exception
*
* @returns {boolean}
*/
api.hasSessionExpired = function (jqXHR, lycheeException) {
return (
(jqXHR.status === 419 && !!lycheeException && lycheeException.exception.endsWith("SessionExpiredException")) ||
(jqXHR.status === 401 && !!lycheeException && lycheeException.exception.endsWith("UnauthenticatedException"))
);
};
/**
*
* @param {string} fn
* @param {Object} params
* @param {?APISuccessCB} successCallback
* @param {?APIProgressCB} responseProgressCB
* @param {?APIErrorCB} errorCallback called if the request fails;
* the callback should return `true`,
* if the callback has already
* handled the error and does not
* want the API layer to handle the
* error any further
* (i.e. show an error flash),
* the callback should return `false`,
* if the API layer should handle
* the error, too.
* @returns {void}
*/
api.post = function (fn, params, successCallback = null, responseProgressCB = null, errorCallback = null) {
loadingBar.show();
/**
* The success handler
* @param {Object} data the decoded JSON object of the response
*/
const successHandler = (data) => {
setTimeout(loadingBar.hide, 100);
if (successCallback) successCallback(data);
};
/**
* The error handler
* @param {XMLHttpRequest} jqXHR the jQuery XMLHttpRequest object, see {@link https://api.jquery.com/jQuery.ajax/#jqXHR}.
*/
const errorHandler = (jqXHR) => {
/**
* @type {?LycheeException}
*/
const lycheeException = jqXHR.responseJSON;
if (errorCallback) {
let isHandled = errorCallback(jqXHR, params, lycheeException);
if (isHandled) {
setTimeout(loadingBar.hide, 100);
return;
}
}
// Call global error handler for unhandled errors
api.onError(jqXHR, params, lycheeException);
};
let ajaxParams = {
type: "POST",
url: "api/" + fn,
contentType: "application/json",
data: JSON.stringify(params),
dataType: "json",
headers: {
"X-XSRF-TOKEN": csrf.getCSRFCookieValue(),
},
success: successHandler,
error: errorHandler,
};
if (responseProgressCB !== null) {
ajaxParams.xhrFields = {
onprogress: responseProgressCB,
};
}
$.ajax(ajaxParams);
};
/**
* Given a URL return the text raw content of the file.
*
* @param {string} url
* @param {APISuccessCB} callback
* @returns {void}
*/
api.getRawContent = function (url, callback) {
loadingBar.show();
/**
* The success handler
* @param {Object} data the decoded JSON object of the response
*/
const successHandler = (data) => {
setTimeout(loadingBar.hide, 100);
callback(data);
};
/**
* The error handler
* @param {XMLHttpRequest} jqXHR the jQuery XMLHttpRequest object, see {@link https://api.jquery.com/jQuery.ajax/#jqXHR}.
*/
const errorHandler = (jqXHR) => {
api.onError(jqXHR, {}, null);
};
$.ajax({
type: "GET",
url: url,
data: {},
dataType: "text",
headers: {
"X-XSRF-TOKEN": csrf.getCSRFCookieValue(),
},
success: successHandler,
error: errorHandler,
});
};