Skip to content

Commit

Permalink
fix image upload with gitlab api
Browse files Browse the repository at this point in the history
  • Loading branch information
lexoyo committed Sep 29, 2023
1 parent 7e58a97 commit 2245ba7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ Then, you can run Silex with the `silex` command. The entry point is `src/ts/ser

You can configure Silex using environment variables and command-line options. All available options can be found in `src/ts/server/cli.ts`.

There are config files (same as plugins) in the `examples/` folder. To start Silex locally with these config:

```sh
$ SILEX_CLIENT_CONFIG=./examples/client-config-transformers.js SILEX_CONFIG=`pwd`/examples/server-config-plugins.js npm run start:debug
```

## Contributing

Contributions are welcome! Please read our [contributing guidelines](CONTRIBUTING.md) for details on how to contribute to Silex.
Expand Down
12 changes: 7 additions & 5 deletions src/ts/plugins/server/GitlabConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ export default class GitlabConnector implements StorageConnector {
// Handle the case when the server returns an non-JSON response (e.g. 400 Bad Request)
const text = await response.text()
if(!response.ok) {
if (response.status === 401 && session?.gitlab?.token?.refresh_token) {
if (text.includes('A file with this name doesn\'t exist')) {
throw new ApiError('Gitlab API error (5): Not Found', 404)
} else if (response.status === 401 && session?.gitlab?.token?.refresh_token) {
// Refresh the token
const token = session?.gitlab?.token
const body = {
Expand Down Expand Up @@ -327,7 +329,6 @@ export default class GitlabConnector implements StorageConnector {
}
} else {
const message = typeof json?.message === 'object' ? Object.entries(json.message).map(entry => entry.join(' ')).join(' ') : json?.message ?? json?.error ?? response.statusText
console.error('Gitlab API error (1)', response.status, response.statusText, {message})
throw new ApiError(`Gitlab API error (1): ${message}`, response.status)
}
}
Expand Down Expand Up @@ -520,7 +521,6 @@ export default class GitlabConnector implements StorageConnector {
branch: this.options.branch,
commit_message: 'Update website data from Silex',
content: JSON.stringify(websiteData),
file_path: WEBSITE_DATA_FILE,
id: websiteId,
})
}
Expand Down Expand Up @@ -648,12 +648,14 @@ export default class GitlabConnector implements StorageConnector {
for (const file of files) {
// Convert to base64
const content = (await contentToBuffer(file.content)).toString('base64')
const path = this.getAssetPath(file.path)

try {
await this.updateFile(session, websiteId, this.getAssetPath(file.path), content, true)
await this.updateFile(session, websiteId, path, content, true)
} catch (e) {
// If the file does not exist, create it
if (e.statusCode === 404 || e.httpStatusCode === 404 || e.message.endsWith('A file with this name doesn\'t exist')) {
await this.createFile(session, websiteId, this.getAssetPath(file.path), content, true)
await this.createFile(session, websiteId, path, content, true)
} else {
throw e
}
Expand Down

0 comments on commit 2245ba7

Please sign in to comment.