Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit bcfe81e

Browse files
author
clouless
committed
fix hardcoded urls. add error handling.
1 parent 81ba6be commit bcfe81e

File tree

7 files changed

+64
-21
lines changed

7 files changed

+64
-21
lines changed

DEVELOPMENT.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Development
22

3+
4+
5+
```
6+
yarn start
7+
```
8+
9+
GoTo http://localhost:4200 and enter in console `localStorage.setItem('LOCAL_DEV', true);`
10+
11+
----
12+
13+
 
14+
315
### CI and Releasing
416

517
Git Tagging

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
[![](https://codeclou.github.io/kartoffelstampf/img/kartoffelstampf.svg)](https://github.com/codeclou/kartoffelstampf/)
2+
3+
 
4+
5+
> Compress, Squash and Stampf your Images in a convenient way. Out-of-the-Box Docker Image with Web-GUI to perform lossless PNG and JPG compression.
6+
7+
 
8+
19
# Kartoffelstampf Client
210

311
Microservice of Kartoffelstampf Project that provides the UI as a Browser Single-Page-Application

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kartoffelstampf-client",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"license": "MIT",
55
"scripts": {
66
"ng": "ng",

src/app/services/backend.service.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,43 @@ export type FnOnMessageCallback = (arg1: KartoffelstampfTerminalOutputEntry) =>
1919
@Injectable()
2020
export class BackendService {
2121

22-
private static WEB_SOCKET_COMPRESS_URL = `ws://${window.location.hostname}:9999/`;
23-
private static REST_API_UPLOAD_URL = `http://${window.location.hostname}:9999/upload`;
24-
private static REST_API_DOWNLOAD_URL = `http://${window.location.hostname}:9999/download`;
22+
private restApiBaseUrl: string;
23+
private webSocketBaseUrl: string;
2524

26-
constructor(private http: HttpClient) { }
25+
constructor(private http: HttpClient) {
26+
// Autodetect URLs
27+
const hostname = window.location.hostname;
28+
const protocol = window.location.protocol;
29+
const port = window.location.port;
30+
if (protocol === 'https:') {
31+
this.restApiBaseUrl = `https://${hostname}${port ? ':' + port : ''}`;
32+
this.webSocketBaseUrl = `wss://${hostname}${port ? ':' + port : ''}`;
33+
} else {
34+
this.restApiBaseUrl = `http://${hostname}${port ? ':' + port : ''}`;
35+
this.webSocketBaseUrl = `ws://${hostname}${port ? ':' + port : ''}`;
36+
}
37+
//
38+
// Note on localhost we need to overwrite the URLs
39+
//
40+
if (localStorage && localStorage.getItem('LOCAL_DEV')) {
41+
this.restApiBaseUrl = `http://localhost:9999`;
42+
this.webSocketBaseUrl = `ws://localhost:9999`;
43+
}
44+
}
2745

2846
getDownloadUrl(temporaryFileName: string, originalFileName: string) {
29-
return `${BackendService.REST_API_DOWNLOAD_URL}/${temporaryFileName}/${originalFileName}`;
47+
return `${this.restApiBaseUrl}/download/${temporaryFileName}/${originalFileName}`;
3048
}
3149

3250
uploadImage(base64Image: string, type: string) {
3351
return this.http.post<KartoffelstampfImageUploadResponse>(
34-
BackendService.REST_API_UPLOAD_URL, {
52+
`${this.restApiBaseUrl}/upload`, {
3553
contentDataUri: base64Image,
36-
} as KartoffelstampfImageUploadRequest, httpOptions)
37-
.pipe(
38-
catchError((e: HttpErrorResponse) => {
39-
console.log(e);
40-
return throwError(
41-
'Something bad happened; please try again later.'
42-
);
43-
})
44-
);
54+
} as KartoffelstampfImageUploadRequest, httpOptions);
4555
}
4656

4757
runCompressCommand(compressInstruction: KartoffelstampfCompressInstruction): Observable<KartoffelstampfTerminalOutputEntry> {
48-
const ws = new WebSocket(BackendService.WEB_SOCKET_COMPRESS_URL);
58+
const ws = new WebSocket(`${this.webSocketBaseUrl}/`);
4959
const subject = new Subject<KartoffelstampfTerminalOutputEntry>();
5060
ws.onopen = function (event) {
5161
ws.send(JSON.stringify(compressInstruction));

src/app/types/kartoffelstampf-client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ export class CompressImageJobItem {
2929
compressDone = false;
3030
downloadClicked = false;
3131
terminalLinesExpanded = false;
32+
serverError: string = null;
3233
}

src/app/upload-page/upload-page.component.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@
5757
{{imageCompressJob.compressedSize ? (1 - (imageCompressJob.compressedSize / imageCompressJob.originalSize) | percent) : '...'}}
5858
</td>
5959
<td>
60-
<app-spinner *ngIf="!imageCompressJob.compressDone"></app-spinner>
60+
<app-spinner *ngIf="imageCompressJob.serverError == null && !imageCompressJob.compressDone"></app-spinner>
61+
<span class="error" *ngIf="imageCompressJob.serverError != null">
62+
{{imageCompressJob.serverError}}
63+
</span>
6164
<a
6265
*ngIf="imageCompressJob.compressDone == true"
6366
[href]="getDownloadUrl(imageCompressJob)"
@@ -71,6 +74,7 @@
7174
<tr *ngIf="imageCompressJob.terminalLinesExpanded == true">
7275
<td colspan="5" class="terminal-line-td">
7376
<app-terminal-output
77+
*ngIf="imageCompressJob.serverError == null"
7478
[temporaryFileName]="imageCompressJob.temporaryFileName"
7579
[originalFileName]="imageCompressJob.originalFileName"
7680
[lines]="imageCompressJob.terminalLines"

src/app/upload-page/upload-page.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { BackendService } from '../services/backend.service';
33
import { KartoffelstampfTerminalOutputEntry, KartoffelstampfCompressInstruction } from '../types/kartoffelstampf-server';
44
import { TerminalLine, CompressImageJobItem } from '../types/kartoffelstampf-client';
55
import { finalize, takeUntil } from 'rxjs/operators';
6-
import { Subject } from 'rxjs';
6+
import { Subject, throwError, of, EMPTY } from 'rxjs';
7+
import { catchError } from 'rxjs/operators';
8+
import { HttpErrorResponse } from '@angular/common/http';
79

810
@Component({
911
selector: 'app-upload-page',
@@ -40,6 +42,7 @@ import { Subject } from 'rxjs';
4042
justify-content: flex-start;
4143
align-items: center;
4244
}`,
45+
`.error { display:block; padding:2px 4px; background-color: #A60000; color:#fff;}`,
4346
],
4447
providers: [BackendService]
4548
})
@@ -112,9 +115,14 @@ export class UploadPageComponent implements OnInit, OnDestroy {
112115
self.backendService
113116
.uploadImage(job.uploadedFileBase64URI, 'PNG')
114117
.pipe(
115-
takeUntil(self.preDestroy)
118+
takeUntil(self.preDestroy),
119+
catchError((e: any) => {
120+
self.imageCompressJobs.push(job);
121+
job.serverError = e.error.error;
122+
return EMPTY;
123+
})
116124
)
117-
.subscribe(uploadResponse => {
125+
.subscribe((uploadResponse: any) => {
118126
job.temporaryFileName = uploadResponse.fileName;
119127
self.imageCompressJobs.push(job);
120128
self.runCompressCommand(job);

0 commit comments

Comments
 (0)