@silexlabs/silex 3.0.0-alpha.43 → 3.0.0-alpha.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/dist/client/client/api.d.ts +6 -6
  2. package/dist/client/client/config.d.ts +26 -211
  3. package/dist/client/client/events.d.ts +2 -0
  4. package/dist/client/client/expose.d.ts +0 -1
  5. package/dist/client/client/grapesjs/LoginDialog.d.ts +1 -0
  6. package/dist/client/client/grapesjs/index.d.ts +3 -209
  7. package/dist/client/client/index.d.ts +1 -0
  8. package/dist/client/constants.d.ts +2 -3
  9. package/dist/client/css/admin.css +2 -0
  10. package/dist/client/js/main.js +47 -47
  11. package/dist/client/types.d.ts +7 -1
  12. package/dist/plugins/client/client/events.d.ts +2 -0
  13. package/dist/plugins/client/client/events.js +2 -0
  14. package/dist/plugins/client/client/events.js.map +1 -1
  15. package/dist/plugins/client/plugins/client/template.js +4 -4
  16. package/dist/plugins/client/plugins/client/template.js.map +1 -1
  17. package/dist/plugins/client/types.d.ts +7 -1
  18. package/dist/plugins/client/types.js +8 -0
  19. package/dist/plugins/client/types.js.map +1 -1
  20. package/dist/plugins/server/constants.d.ts +2 -3
  21. package/dist/plugins/server/constants.js +3 -4
  22. package/dist/plugins/server/constants.js.map +1 -1
  23. package/dist/plugins/server/plugins/server/FtpConnector.d.ts +17 -28
  24. package/dist/plugins/server/plugins/server/FtpConnector.js +17 -16
  25. package/dist/plugins/server/plugins/server/FtpConnector.js.map +1 -1
  26. package/dist/plugins/server/plugins/server/GitlabConnector.d.ts +81 -0
  27. package/dist/plugins/server/plugins/server/GitlabConnector.js +373 -0
  28. package/dist/plugins/server/plugins/server/GitlabConnector.js.map +1 -0
  29. package/dist/plugins/server/server/config.js +1 -1
  30. package/dist/plugins/server/server/config.js.map +1 -1
  31. package/dist/plugins/server/server/connectors/FsStorage.d.ts +13 -12
  32. package/dist/plugins/server/server/connectors/FsStorage.js +22 -15
  33. package/dist/plugins/server/server/connectors/FsStorage.js.map +1 -1
  34. package/dist/plugins/server/server/connectors/connectors.d.ts +5 -2
  35. package/dist/plugins/server/server/connectors/connectors.js +31 -3
  36. package/dist/plugins/server/server/connectors/connectors.js.map +1 -1
  37. package/dist/plugins/server/types.d.ts +7 -1
  38. package/dist/plugins/server/types.js +10 -1
  39. package/dist/plugins/server/types.js.map +1 -1
  40. package/dist/server/constants.js +3 -4
  41. package/dist/server/server/api/connectorApi.js +16 -11
  42. package/dist/server/server/api/publicationApi.js +18 -20
  43. package/dist/server/server/api/websiteApi.js +36 -40
  44. package/dist/server/server/cli.js +34 -16
  45. package/dist/server/server/config.js +1 -1
  46. package/dist/server/server/connectors/FsStorage.js +19 -15
  47. package/dist/server/server/connectors/connectors.js +27 -3
  48. package/dist/server/server/expose.js +27 -0
  49. package/dist/server/server/index.js +28 -2
  50. package/dist/server/types.js +10 -1
  51. package/package.json +3 -1
  52. package/src/css/_variables.scss +1 -0
  53. package/src/css/loader.scss +2 -0
  54. package/src/ts/client/api.ts +8 -11
  55. package/src/ts/client/config.ts +30 -14
  56. package/src/ts/client/events.ts +2 -0
  57. package/src/ts/client/expose.ts +0 -1
  58. package/src/ts/client/grapesjs/LoginDialog.ts +12 -1
  59. package/src/ts/client/grapesjs/PublicationManager.ts +1 -1
  60. package/src/ts/client/grapesjs/index.ts +8 -13
  61. package/src/ts/client/grapesjs/storage.ts +18 -10
  62. package/src/ts/client/index.ts +32 -14
  63. package/src/ts/constants.ts +2 -3
  64. package/src/ts/plugins/client/template.ts +4 -4
  65. package/src/ts/plugins/server/FtpConnector.test.ts +82 -0
  66. package/src/ts/plugins/server/FtpConnector.ts +31 -37
  67. package/src/ts/plugins/server/GitlabConnector.ts +434 -0
  68. package/src/ts/server/api/connectorApi.ts +31 -26
  69. package/src/ts/server/api/publicationApi.ts +23 -23
  70. package/src/ts/server/api/websiteApi.ts +42 -43
  71. package/src/ts/server/cli.ts +23 -11
  72. package/src/ts/server/config.ts +1 -1
  73. package/src/ts/server/connectors/FsStorage.test.ts +51 -0
  74. package/src/ts/server/connectors/FsStorage.ts +35 -25
  75. package/src/ts/server/connectors/connectors.ts +32 -4
  76. package/{dist/plugins/client/server/utils/validation.js → src/ts/server/expose.ts} +3 -18
  77. package/src/ts/server/index.ts +24 -2
  78. package/src/ts/types.ts +10 -1
  79. package/dist/plugins/client/constants.d.ts +0 -30
  80. package/dist/plugins/client/constants.js +0 -47
  81. package/dist/plugins/client/constants.js.map +0 -1
  82. package/dist/plugins/client/server/config.d.ts +0 -59
  83. package/dist/plugins/client/server/config.js +0 -178
  84. package/dist/plugins/client/server/config.js.map +0 -1
  85. package/dist/plugins/client/server/connectors/FsHosting.d.ts +0 -13
  86. package/dist/plugins/client/server/connectors/FsHosting.js +0 -51
  87. package/dist/plugins/client/server/connectors/FsHosting.js.map +0 -1
  88. package/dist/plugins/client/server/connectors/FsStorage.d.ts +0 -41
  89. package/dist/plugins/client/server/connectors/FsStorage.js +0 -220
  90. package/dist/plugins/client/server/connectors/FsStorage.js.map +0 -1
  91. package/dist/plugins/client/server/connectors/connectors.d.ts +0 -83
  92. package/dist/plugins/client/server/connectors/connectors.js +0 -91
  93. package/dist/plugins/client/server/connectors/connectors.js.map +0 -1
  94. package/dist/plugins/client/server/events.d.ts +0 -26
  95. package/dist/plugins/client/server/events.js +0 -12
  96. package/dist/plugins/client/server/events.js.map +0 -1
  97. package/dist/plugins/client/server/jobs.d.ts +0 -6
  98. package/dist/plugins/client/server/jobs.js +0 -67
  99. package/dist/plugins/client/server/jobs.js.map +0 -1
  100. package/dist/plugins/client/server/utils/validation.d.ts +0 -7
  101. package/dist/plugins/client/server/utils/validation.js.map +0 -1
@@ -0,0 +1,434 @@
1
+ /*
2
+ * Silex website builder, free/libre no-code tool for makers.
3
+ * Copyright (c) 2023 lexoyo and Silex Labs foundation
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Affero General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Affero General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Affero General Public License
15
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import { API_CONNECTOR_LOGIN_CALLBACK, API_CONNECTOR_PATH, API_PATH, WEBSITE_DATA_FILE, WEBSITE_META_DATA_FILE } from '../../constants'
19
+ import { ServerConfig } from '../../server/config'
20
+ import { ConnectorFile, ConnectorFileContent, StatusCallback, StorageConnector, contentToBuffer, contentToString, toConnectorData } from '../../server/connectors/connectors'
21
+ import { ApiError, ConnectorType, ConnectorUser, WebsiteData, WebsiteId, WebsiteMeta, WebsiteMetaFileContent } from '../../types'
22
+ import fetch from 'node-fetch'
23
+ import crypto, { createHash } from 'crypto'
24
+ import { PassThrough, Readable } from 'stream'
25
+
26
+ /**
27
+ * Gitlab connector
28
+ * @fileoverview Gitlab connector for Silex, connect to the user's Gitlab account to store websites
29
+ * @see https://docs.gitlab.com/ee/api/oauth2.html
30
+ */
31
+
32
+ export interface GitlabOptions {
33
+ clientId: string
34
+ clientSecret: string
35
+ branch: string
36
+ assetsFolder: string
37
+ }
38
+
39
+ interface GitlabToken {
40
+ state: string
41
+ codeVerifier: string
42
+ codeChallenge: string
43
+ token?: {
44
+ access_token: string
45
+ token_type: string
46
+ expires_in: number
47
+ refresh_token: string
48
+ created_at: number
49
+ id_token: string
50
+ scope: string
51
+ }
52
+ userId?: number
53
+ }
54
+
55
+ interface GitlabSession {
56
+ gitlab?: GitlabToken
57
+ }
58
+
59
+ interface GitlabAction {
60
+ action: 'create' | 'delete' | 'move' | 'update'
61
+ file_path: string
62
+ content?: string
63
+ }
64
+
65
+ interface GitlabWriteFile {
66
+ branch: string
67
+ commit_message: string
68
+ id: string
69
+ actions?: GitlabAction[]
70
+ content?: string
71
+ file_path?: string
72
+ encoding?: 'base64' | 'text'
73
+ }
74
+
75
+ interface GitlabGetToken {
76
+ grant_type: 'authorization_code'
77
+ client_id: string
78
+ client_secret: string
79
+ code: string
80
+ redirect_uri: string
81
+ code_verifier: string
82
+ }
83
+
84
+ interface GitlabWebsiteName {
85
+ name: string
86
+ }
87
+
88
+ interface GitlabCreateBranch {
89
+ branch: string
90
+ ref: string
91
+ }
92
+
93
+ async function wait(ms: number) {
94
+ return new Promise(resolve => setTimeout(resolve, ms))
95
+ }
96
+
97
+ export default class GitlabConnector implements StorageConnector {
98
+ connectorId = 'gitlab'
99
+ connectorType = ConnectorType.STORAGE
100
+ displayName = 'GitLab'
101
+ icon = 'http://gitlab.lcqb.upmc.fr/assets/favicon-075eba76312e8421991a0c1f89a89ee81678bcde72319dd3e8047e2a47cd3a42.ico'
102
+ disableLogout = false
103
+ color = '#ffffff'
104
+ background = '#FC6D26'
105
+ options: GitlabOptions
106
+
107
+ constructor(private config: ServerConfig, opts: Partial<GitlabOptions>) {
108
+ this.options = {
109
+ branch: 'main',
110
+ assetsFolder: '',
111
+ ...opts,
112
+ } as GitlabOptions
113
+ if(!this.options.clientId) throw new Error('Missing Gitlab client ID')
114
+ if(!this.options.clientSecret) throw new Error('Missing Gitlab client secret')
115
+ }
116
+
117
+ // **
118
+ // Convenience methods for the Gitlab API
119
+ private getAssetPath(path: string): string {
120
+ return `${this.options.assetsFolder}${path}`
121
+ }
122
+
123
+ private async createFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
124
+ // Remove leading slash
125
+ const safePath = path.replace(/^\//, '')
126
+ return this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${safePath}`, 'POST', {
127
+ id: websiteId,
128
+ branch: this.options.branch,
129
+ content,
130
+ commit_message: `Create file ${path} from Silex`,
131
+ encoding: isBase64 ? 'base64' : undefined,
132
+ })
133
+ }
134
+
135
+ private async updateFile(session: GitlabSession, websiteId: WebsiteId, path: string, content: string, isBase64 = false): Promise<void> {
136
+ // Remove leading slash
137
+ const safePath = path.replace(/^\//, '')
138
+ return this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${safePath}`, 'PUT', {
139
+ id: websiteId,
140
+ branch: this.options.branch,
141
+ content: await contentToString(content),
142
+ commit_message: `Update website asset ${path} from Silex`,
143
+ encoding: isBase64 ? 'base64' : undefined,
144
+ })
145
+ }
146
+
147
+ private async callApi(session: GitlabSession, path: string, method: 'POST' | 'GET' | 'PUT' | 'DELETE' = 'GET', body: GitlabWriteFile | GitlabGetToken | GitlabWebsiteName | GitlabCreateBranch | null = null, params: any = {}): Promise<any> {
148
+ const token = session?.gitlab?.token
149
+ const tokenParam = token ? `access_token=${token.access_token}&` : ''
150
+ const paramsStr = Object.entries(params).map(([k, v]) => `${k}=${encodeURIComponent((v as any).toString())}`).join('&')
151
+ const url = `https://gitlab.com/${path}?${tokenParam}${paramsStr}`
152
+ const response = await fetch(url, {
153
+ method,
154
+ headers: {
155
+ 'Content-Type': 'application/json',
156
+ },
157
+ body: body ? JSON.stringify(body) : undefined
158
+ })
159
+ let json: { message: string, error: string } | any
160
+ // Handle the case when the server returns an non-JSON response (e.g. 400 Bad Request)
161
+ const text = await response.text()
162
+ try {
163
+ json = JSON.parse(text)
164
+ } catch (e) {
165
+ if(!response.ok) {
166
+ // A real error
167
+ throw e
168
+ } else {
169
+ // Useless error linked to the fact that the response is not JSON
170
+ //console.error('Gitlab API error - could not parse response', response.status, response.statusText, {url, method, body, params, text})
171
+ return text
172
+ }
173
+ }
174
+ if(!response.ok) {
175
+ if (response.status === 401 && session?.gitlab?.token?.refresh_token) {
176
+ // Refresh the token
177
+ const token = session?.gitlab?.token
178
+ const body = {
179
+ grant_type: 'refresh_token',
180
+ refresh_token: token.refresh_token,
181
+ client_id: this.options.clientId,
182
+ client_secret: this.options.clientSecret,
183
+ }
184
+ const response = await fetch('https://gitlab.com/oauth/token', {
185
+ method: 'POST',
186
+ headers: {
187
+ 'Content-Type': 'application/json',
188
+ },
189
+ body: JSON.stringify(body)
190
+ })
191
+ const refreshJson = await response.json()
192
+ if (response.ok) {
193
+ session.gitlab.token = {
194
+ ...token,
195
+ ...refreshJson,
196
+ }
197
+ return await this.callApi(session, path, method, body as any, params)
198
+ } else {
199
+ //console.error('Gitlab API error - could not refresh token', response.status, response.statusText, { url, method, body, params, refreshJson })
200
+ throw new ApiError(`Gitlab API error: ${refreshJson?.message ?? response.statusText}`, response.status)
201
+ }
202
+ } else {
203
+ //console.error('Gitlab API error', response.status, response.statusText, { url, method, body, params, json })
204
+ throw new ApiError(`Gitlab API error: ${json?.message ?? json?.error ?? response.statusText}`, response.status)
205
+ }
206
+ }
207
+ return json
208
+ }
209
+
210
+ private generateCodeVerifier() {
211
+ return crypto.randomBytes(64).toString('base64')
212
+ .replace(/\+/g, '-')
213
+ .replace(/\//g, '_')
214
+ .replace(/=/g, '')
215
+ .substr(0, 128)
216
+ }
217
+
218
+ private async generateCodeChallenge(verifier) {
219
+ const hashed = createHash('sha256').update(verifier).digest()
220
+ let base64Url = hashed.toString('base64')
221
+ // Replace '+' with '-', '/' with '_', and remove '='
222
+ base64Url = base64Url.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
223
+ return base64Url
224
+ }
225
+
226
+
227
+ private getRedirect() {
228
+ const params = `connectorId=${this.connectorId}&type=${this.connectorType}`
229
+ return `${this.config.url}${API_PATH}${API_CONNECTOR_PATH}${API_CONNECTOR_LOGIN_CALLBACK}?${params}`
230
+ }
231
+
232
+ /**
233
+ * Get the OAuth URL to redirect the user to
234
+ * The URL should look like
235
+ * https://gitlab.example.com/oauth/authorize?client_id=APP_ID&redirect_uri=REDIRECT_URI&response_type=code&state=STATE&scope=REQUESTED_SCOPES&code_challenge=CODE_CHALLENGE&code_challenge_method=S256
236
+ * OAuth2 Step #1 from https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-with-proof-key-for-code-exchange-pkce
237
+ */
238
+ async getOAuthUrl(session: GitlabSession): Promise<string> {
239
+ const redirect_uri = encodeURIComponent(this.getRedirect())
240
+
241
+ const state = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
242
+ const codeVerifier = this.generateCodeVerifier()
243
+
244
+ // Create the code challenge
245
+ const codeChallenge = await this.generateCodeChallenge(codeVerifier)
246
+
247
+ // Store the code verifier and code challenge in the session
248
+ session.gitlab = {
249
+ ...session.gitlab,
250
+ state,
251
+ codeVerifier,
252
+ codeChallenge,
253
+ }
254
+ const scope = 'api+read_api+read_user+read_repository+write_repository+email+sudo+profile+openid'
255
+ return `https://gitlab.com/oauth/authorize?client_id=${this.options.clientId}&redirect_uri=${redirect_uri}&response_type=code&state=${session.gitlab.state}&scope=${scope}&code_challenge=${codeChallenge}&code_challenge_method=S256`
256
+ }
257
+
258
+ getOptions(formData: object): object {
259
+ return {} // FIXME: store branch
260
+ }
261
+
262
+ async getLoginForm(session: GitlabSession, redirectTo: string): Promise<null> {
263
+ return null
264
+ }
265
+
266
+ async getSettingsForm(session: GitlabSession, redirectTo: string): Promise<null> {
267
+ return null
268
+ }
269
+
270
+ async isLoggedIn(session: GitlabSession): Promise<boolean> {
271
+ return !!session?.gitlab?.token
272
+ }
273
+
274
+ /**
275
+ * Get the token from return code
276
+ * Set the token in the session
277
+ * OAuth2 Step #2 from https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-with-proof-key-for-code-exchange-pkce
278
+ */
279
+ async setToken(session: GitlabSession, loginResult: any): Promise<void> {
280
+ if(!loginResult.state || loginResult.state !== session.gitlab?.state) throw new ApiError('Invalid state', 401)
281
+ if(!session.gitlab?.codeVerifier) throw new ApiError('Missing code verifier', 401)
282
+ if(!session.gitlab?.codeChallenge) throw new ApiError('Missing code challenge', 401)
283
+
284
+ const token = await fetch('https://gitlab.com/oauth/token', {
285
+ method: 'POST',
286
+ headers: {
287
+ 'Content-Type': 'application/json',
288
+ },
289
+ body: JSON.stringify({
290
+ client_id: this.options.clientId,
291
+ client_secret: this.options.clientSecret,
292
+ code: loginResult.code,
293
+ grant_type: 'authorization_code',
294
+ redirect_uri: this.getRedirect(),
295
+ code_verifier: session.gitlab.codeVerifier,
296
+ }),
297
+ })
298
+ .then((response) => response.json())
299
+
300
+ session.gitlab = { token, state: session.gitlab.state, codeVerifier: session.gitlab.codeVerifier, codeChallenge: session.gitlab.codeChallenge }
301
+
302
+ // We need to get the user ID for listWebsites
303
+ const user = await this.callApi(session, 'api/v4/user') as any
304
+ session.gitlab.userId = user.id
305
+ }
306
+
307
+ async logout(session: GitlabSession): Promise<void> {
308
+ delete session.gitlab
309
+ }
310
+
311
+ async getUser(session: GitlabSession): Promise<ConnectorUser> {
312
+ const user = await this.callApi(session, 'api/v4/user') as any
313
+ return {
314
+ name: user.name,
315
+ email: user.email,
316
+ storage: await toConnectorData(session, this as StorageConnector),
317
+ }
318
+ }
319
+
320
+ async listWebsites(session: GitlabSession): Promise<WebsiteMeta[]> {
321
+ const projects = await this.callApi(session, `api/v4/users/${session.gitlab?.userId}/projects`) as any[]
322
+ return projects.map(p => ({
323
+ websiteId: p.id,
324
+ name: p.name,
325
+ createdAt: p.created_at,
326
+ updatedAt: p.last_activity_at,
327
+ connectorUserSettings: {},
328
+ }))
329
+ }
330
+
331
+ async readWebsite(session: GitlabSession, websiteId: string): Promise<WebsiteData> {
332
+ const result = await this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${WEBSITE_DATA_FILE}`, 'GET', null, {
333
+ ref: this.options.branch,
334
+ }) as any
335
+ const { content } = result
336
+ const contentDecoded = Buffer.from(content, 'base64').toString('utf8')
337
+ const websiteData = JSON.parse(contentDecoded) as WebsiteData
338
+ return websiteData
339
+ }
340
+
341
+ async createWebsite(session: GitlabSession, websiteMeta: WebsiteMetaFileContent): Promise<WebsiteId> {
342
+ const project = await this.callApi(session, 'api/v4/projects/', 'POST', {
343
+ name: websiteMeta.name,
344
+ }) as any
345
+ await this.createFile(session, project.id, WEBSITE_DATA_FILE, JSON.stringify({} as WebsiteData))
346
+ await this.createFile(session, project.id, WEBSITE_META_DATA_FILE, JSON.stringify(websiteMeta))
347
+ //await this.updateWebsite(session, project.id, {} as WebsiteData)
348
+ //await this.setWebsiteMeta(session, project.id, websiteMeta)
349
+ return project.id
350
+ }
351
+
352
+ async updateWebsite(session: GitlabSession, websiteId: WebsiteId, websiteData: WebsiteData): Promise<void> {
353
+ const project = await this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${WEBSITE_DATA_FILE}`, 'PUT', {
354
+ branch: this.options.branch,
355
+ commit_message: 'Update website data from Silex',
356
+ content: JSON.stringify(websiteData),
357
+ file_path: WEBSITE_DATA_FILE,
358
+ id: websiteId,
359
+ })
360
+ }
361
+
362
+ async deleteWebsite(session: GitlabSession, websiteId: WebsiteId): Promise<void> {
363
+ await this.callApi(session, `api/v4/projects/${websiteId}`, 'DELETE')
364
+ }
365
+
366
+ async getWebsiteMeta(session: GitlabSession, websiteId: WebsiteId): Promise<WebsiteMeta> {
367
+ const project = await this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${WEBSITE_META_DATA_FILE}`) as any
368
+ return {
369
+ websiteId: project.id,
370
+ name: project.name,
371
+ createdAt: project.created_at,
372
+ updatedAt: project.last_activity_at,
373
+ connectorUserSettings: {},
374
+ }
375
+ }
376
+
377
+ async setWebsiteMeta(session: GitlabSession, websiteId: WebsiteId, websiteMeta: WebsiteMetaFileContent): Promise<void> {
378
+ const project = await this.callApi(session, `api/v4/projects/${websiteId}/repository/files/${WEBSITE_META_DATA_FILE}`, 'PUT', {
379
+ branch: this.options.branch,
380
+ commit_message: 'Update website meta data from Silex',
381
+ content: JSON.stringify(websiteMeta),
382
+ file_path: WEBSITE_META_DATA_FILE,
383
+ id: websiteId,
384
+ })
385
+ }
386
+
387
+ async writeAssets(session: GitlabSession, websiteId: string, files: ConnectorFile[], status?: StatusCallback | undefined): Promise<void> {
388
+ // For each file
389
+ for (const file of files) {
390
+ // Convert to base64
391
+ const content = (await contentToBuffer(file.content)).toString('base64')
392
+ try {
393
+ await this.updateFile(session, websiteId, this.getAssetPath(file.path), content, true)
394
+ } catch (e) {
395
+ // If the file does not exist, create it
396
+ if (e.statusCode === 404 || e.message.endsWith('A file with this name doesn\'t exist')) {
397
+ await this.createFile(session, websiteId, file.path, content, true)
398
+ } else {
399
+ throw e
400
+ }
401
+ }
402
+ }
403
+ }
404
+
405
+ async readAsset(session: GitlabSession, websiteId: string, fileName: string): Promise<ConnectorFileContent> {
406
+ // Remove leading slash
407
+ const finalPath = this.getAssetPath(fileName)
408
+ // Call the API
409
+ const url = `https://gitlab.com/api/v4/projects/${websiteId}/repository/files/${finalPath}?ref=${this.options.branch}&access_token=${session.gitlab?.token?.access_token}`
410
+ const response = await fetch(url, {
411
+ method: 'GET',
412
+ headers: {
413
+ 'Content-Type': 'application/json',
414
+ },
415
+ })
416
+ const json = await response.json()
417
+ if(!response.ok) throw new ApiError(`Gitlab API error: ${json?.message ?? json?.error ?? response.statusText}`, response.status)
418
+ // From base64 string to buffer
419
+ const buf = Buffer.from(json.content, 'base64')
420
+ return buf
421
+ }
422
+
423
+ async deleteAssets(session: GitlabSession, websiteId: string, fileNames: string[]): Promise<void> {
424
+ return this.callApi(session, `api/v4/projects/${websiteId}/repository/commits`, 'POST', {
425
+ id: websiteId,
426
+ branch: this.options.branch,
427
+ commit_message: `Delete assets from Silex: ${fileNames.join(', ')}`,
428
+ actions: fileNames.map(f => ({
429
+ action: 'delete',
430
+ file_path: this.getAssetPath(f),
431
+ })),
432
+ })
433
+ }
434
+ }
@@ -3,7 +3,7 @@ import { API_CONNECTOR_LIST, API_CONNECTOR_LOGIN_CALLBACK, API_CONNECTOR_USER, A
3
3
  import { ServerConfig } from '../config'
4
4
  import { requiredParam } from '../utils/validation'
5
5
  import { Connector, getConnector, toConnectorData, toConnectorEnum } from '../connectors/connectors'
6
- import { ApiConnectorListQuery, ApiConnectorListResponse, ApiConnectorLoggedInPostMessage, ApiConnectorLoginQuery, ApiConnectorUserQuery, ApiConnectorUserResponse, ApiConnectorLogoutQuery, ApiError, ConnectorId, ApiConnectorLoginCbkQuery, ApiConnectorLoginCbkBody, ConnectorType, ConnectorOptions } from '../../types'
6
+ import { ApiConnectorListQuery, ApiConnectorListResponse, ApiConnectorLoggedInPostMessage, ApiConnectorLoginQuery, ApiConnectorUserQuery, ApiConnectorUserResponse, ApiConnectorLogoutQuery, ConnectorId, ApiConnectorLoginCbkQuery, ApiConnectorLoginCbkBody, ConnectorOptions, ApiResponseError } from '../../types'
7
7
 
8
8
  /**
9
9
  * @fileoverview The connector API adds routes to handle the connectors and the methods they implement, this includes authentication and user data.
@@ -62,7 +62,7 @@ async function routeUser(req: Request, res: Response) {
62
62
  .json({
63
63
  error: true,
64
64
  message: `Connector not found: ${type} ${query.connectorId}`,
65
- } as ApiError)
65
+ } as ApiResponseError)
66
66
  return
67
67
  }
68
68
  if(!await connector.isLoggedIn(session)) {
@@ -71,7 +71,7 @@ async function routeUser(req: Request, res: Response) {
71
71
  .json({
72
72
  error: true,
73
73
  message: 'Not logged in',
74
- } as ApiError)
74
+ } as ApiResponseError)
75
75
  return
76
76
  }
77
77
  // User logged in, return user data
@@ -79,10 +79,10 @@ async function routeUser(req: Request, res: Response) {
79
79
  res.json(user as ApiConnectorUserResponse)
80
80
  } catch (error) {
81
81
  console.error('Error in the login status request', error)
82
- res.status(error?.code ?? 500).json({
82
+ res.status(error?.code ?? error?.httpStatusCode ?? 500).json({
83
83
  error: true,
84
84
  message: error.message,
85
- } as ApiError)
85
+ } as ApiResponseError)
86
86
  }
87
87
  }
88
88
 
@@ -101,17 +101,17 @@ async function routeListConnectors(req: Request, res: Response) {
101
101
  res.json(list as ApiConnectorListResponse)
102
102
  } catch (error) {
103
103
  console.error('Error while listing connectors', error)
104
- res.status(error?.code ?? 500).json({
104
+ res.status(error?.code ?? error?.httpStatusCode ?? 500).json({
105
105
  error: true,
106
106
  message: 'Error while listing connectors: ' + error.message,
107
- } as ApiError)
107
+ } as ApiResponseError)
108
108
  }
109
109
  } catch (error) {
110
110
  console.error('Error in the list connectors request', error)
111
- res.status(error?.code ?? 400).json({
111
+ res.status(error?.code ?? error?.httpStatusCode ?? 400).json({
112
112
  error: true,
113
113
  message: 'Error in the list connectors request: ' + error.message,
114
- } as ApiError)
114
+ } as ApiResponseError)
115
115
  }
116
116
  }
117
117
 
@@ -148,10 +148,10 @@ async function routeLogin(req: Request, res: Response) {
148
148
  }
149
149
  } catch (error) {
150
150
  console.error('Error in the login request', error)
151
- res.status(error?.code ?? 400).json({
151
+ res.status(error?.code ?? error?.httpStatusCode ?? 400).json({
152
152
  error: true,
153
153
  message: 'Error in the login request: ' + error.message,
154
- } as ApiError)
154
+ } as ApiResponseError)
155
155
  }
156
156
  }
157
157
 
@@ -178,7 +178,7 @@ async function routeLogin(req: Request, res: Response) {
178
178
  // res.send(await connector.getSettingsForm(session, ``) as ApiConnectorSettingsResponse)
179
179
  // } catch (error) {
180
180
  // console.error('Error in the login request', error)
181
- // res.status(error?.code ?? 400).json({
181
+ // res.status(error?.code ?? error?.httpStatusCode ?? 400).json({
182
182
  // error: true,
183
183
  // message: 'Error in the login request: ' + error.message,
184
184
  // } as ApiError)
@@ -209,10 +209,10 @@ async function routeLogin(req: Request, res: Response) {
209
209
  // await connector.setWebsiteMeta(session, websiteId, body)
210
210
  // } catch (error) {
211
211
  // console.error('Error in the login request', error)
212
- // res.status(error?.code ?? 400).json({
212
+ // res.status(error?.code ?? error?.httpStatusCode ?? 400).json({
213
213
  // error: true,
214
214
  // message: 'Error in the login request: ' + error.message,
215
- // } as ApiError)
215
+ // } as ApiResponseError)
216
216
  // }
217
217
  // }
218
218
 
@@ -233,17 +233,22 @@ async function routeLoginSuccess(req: Request, res: Response) {
233
233
  const type = toConnectorEnum(requiredParam(query.type, 'Connector type'))
234
234
  const connector = await getConnector<Connector>(config, req['session'], type, connectorId)
235
235
  if (!connector) throw new Error('Connector not found ' + connectorId)
236
- // Store the auth info in the session
237
- // This is useful for storage only
238
- await connector.setToken(session, {
239
- ...query,
240
- ...body,
241
- })
236
+ // Check if the user is already logged in
237
+ if (await connector.isLoggedIn(session)) {
238
+ console.info('User already logged in for connector ' + connectorId)
239
+ } else {
240
+ // Store the auth info in the session
241
+ // This is useful for storage only
242
+ await connector.setToken(session, {
243
+ ...query,
244
+ ...body,
245
+ })
246
+ }
242
247
  // End the auth flow
243
248
  res.send(getEndAuthHtml('Logged in', false, connectorId, connector.getOptions(body)))
244
249
  } catch (error) {
245
250
  res
246
- .status(error?.code ?? 500)
251
+ .status(error?.code ?? error?.httpStatusCode ?? 500)
247
252
  .send(getEndAuthHtml(error.message, true))
248
253
  }
249
254
  }
@@ -268,21 +273,21 @@ async function routeLogout(req: Request, res: Response) {
268
273
  res.json({
269
274
  error: false,
270
275
  message: 'OK',
271
- } as ApiError)
276
+ } as ApiResponseError)
272
277
  } catch (error) {
273
278
  console.error('Error while logging out', error)
274
- res.status(error?.code ?? 500).json({
279
+ res.status(error?.code ?? error?.httpStatusCode ?? 500).json({
275
280
  error: true,
276
281
  message: 'Error while logging out: ' + error.message,
277
- } as ApiError)
282
+ } as ApiResponseError)
278
283
  return
279
284
  }
280
285
  } catch (error) {
281
286
  console.error('Error in the logout request', error)
282
- res.status(error?.code ?? 400).json({
287
+ res.status(error?.code ?? error?.httpStatusCode ?? 400).json({
283
288
  error: true,
284
289
  message: 'Error in the logout request: ' + error.message,
285
- } as ApiError)
290
+ } as ApiResponseError)
286
291
  return
287
292
  }
288
293
  }
@@ -16,9 +16,9 @@
16
16
  */
17
17
 
18
18
  import { Router } from 'express'
19
- import { API_PUBLICATION_PUBLISH, API_PUBLICATION_STATUS } from '../../constants'
19
+ import { API_PUBLICATION_PUBLISH, API_PUBLICATION_STATUS, API_WEBSITE_ASSET_READ } from '../../constants'
20
20
  import { getJob } from '../jobs'
21
- import { ApiPublicationPublishBody, ApiPublicationPublishQuery, ApiPublicationPublishResponse, ApiPublicationStatusQuery, ApiPublicationStatusResponse, ApiError, JobId, PublicationSettings, PublicationData as PublicationData, ConnectorType, ConnectorId, WebsiteId} from '../../types'
21
+ import { ApiPublicationPublishBody, ApiPublicationPublishQuery, ApiPublicationPublishResponse, ApiPublicationStatusQuery, ApiPublicationStatusResponse, JobId, PublicationSettings, PublicationData as PublicationData, ConnectorType, ConnectorId, WebsiteId} from '../../types'
22
22
  import { ConnectorFile, HostingConnector, StorageConnector, getConnector } from '../connectors/connectors'
23
23
  import { ServerConfig } from '../config'
24
24
  import { requiredParam } from '../utils/validation'
@@ -41,6 +41,8 @@ export class PublicationError extends Error {
41
41
  }
42
42
  }
43
43
 
44
+ const PROJECT_ROOT = require.main ? require.main.path : process.cwd()
45
+
44
46
  /**
45
47
  * Get the desired connector
46
48
  * Can be the default connector or a specific one
@@ -86,7 +88,7 @@ export default function (config: ServerConfig): Router {
86
88
  console.error(`Error: job not found with id ${jobId}`)
87
89
  res.status(404).json({
88
90
  message: 'Error: job not found.',
89
- } as ApiError)
91
+ })
90
92
  return
91
93
  }
92
94
  res.json({
@@ -122,10 +124,18 @@ export default function (config: ServerConfig): Router {
122
124
  const storage = await getStorageConnector(session, config, storageId)
123
125
 
124
126
  // Publication
125
- const assetsFiles: ConnectorFile[] = await Promise.all(assets.map(async asset => ({
126
- path: asset.path ? join(...asset.path.split('/')) : '', // resolve relative path
127
- content: await storage.readAsset(session, websiteId, asset.src),
128
- })))
127
+ const assetsFiles: ConnectorFile[] = await Promise.all(assets.map(async asset => {
128
+ if(!asset.path) throw new PublicationError('Missing path in asset', 400)
129
+ if(!asset.src) throw new PublicationError('Missing src in asset', 400)
130
+ // Remove the / from the asset root if it starts with one
131
+ const prefix = API_WEBSITE_ASSET_READ.replace(/^\//, '')
132
+ // Get the asset url relative to the asset root
133
+ const src = asset.src.startsWith(prefix) ? asset.src.substring(prefix.length) : asset.src
134
+ return {
135
+ path: asset.path,
136
+ content: await storage.readAsset(session, websiteId, src),
137
+ }
138
+ }))
129
139
  const filesList: ConnectorFile[] = files.flatMap<ConnectorFile>(file => ([{
130
140
  path: file.htmlPath,
131
141
  content: file.html,
@@ -133,27 +143,17 @@ export default function (config: ServerConfig): Router {
133
143
  path: file.cssPath,
134
144
  content: file.css,
135
145
  }])).concat(assetsFiles)
136
- try {
137
- res.json({
138
- url: await hostingConnector.getUrl(session, websiteId),
139
- job: await hostingConnector.publish(session, websiteId, filesList),
140
- } as ApiPublicationPublishResponse)
141
- } catch (err) {
142
- console.error('Error publishing the website', err)
143
- res.status(500).json({
144
- message: `Error publishing the website. ${err.message}`
145
- } as ApiError)
146
- // Hook for plugins
147
- config.emit(ServerEvent.PUBLISH_END, err as PublishEndEventType)
148
- return
149
- }
146
+ res.json({
147
+ url: await hostingConnector.getUrl(session, websiteId),
148
+ job: await hostingConnector.publish(session, websiteId, filesList),
149
+ } as ApiPublicationPublishResponse)
150
150
  } catch (err) {
151
151
  console.error('Error publishing the website', err)
152
152
  res
153
- .status(err.code ?? 500)
153
+ .status(typeof err.code === 'number' ? err.code : 500)
154
154
  .json({
155
155
  message: `Error publishing the website. ${err.message}`
156
- } as ApiError)
156
+ })
157
157
  // Hook for plugins
158
158
  config.emit(ServerEvent.PUBLISH_END, err as PublishEndEventType)
159
159
  return