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

Commit 4cc8a00

Browse files
author
clouless
committed
fully working png and jpg lossless compression for one file
1 parent bdfac4a commit 4cc8a00

File tree

10 files changed

+60
-34
lines changed

10 files changed

+60
-34
lines changed

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": "1.0.0",
3+
"version": "0.1.0",
44
"license": "MIT",
55
"scripts": {
66
"ng": "ng",

src/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="container">
2-
<div class="app-logo">
3-
<img [src]="logoSrc" class="img-fluid" />
2+
<div class="logo">
3+
<img [src]="logoSrc" class="logo-img" />
44
</div>
55
<router-outlet></router-outlet>
66
</div>

src/app/app.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Component } from '@angular/core';
44
selector: 'app-root',
55
templateUrl: './app.component.html',
66
styles: [
7-
'.foo {}'
7+
'.container { padding:20px; }',
8+
'.logo { display: flex; align-items: center; flex-direction: column; margin-bottom:30px;}',
9+
'.logo-img { width:60%; max-width:1000px; }',
810
]
911
})
1012
export class AppComponent {

src/app/backend.service.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
KartoffelstampfTerminalOutputEntry,
44
KartoffelstampfImageUploadResponse,
55
KartoffelstampfImageUploadRequest,
6+
KartoffelstampfCompressInstruction,
67
} from './types/kartoffelstampf-server';
78
import { Observable, Subject, throwError } from 'rxjs';
89
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
@@ -24,16 +25,14 @@ export class BackendService {
2425

2526
constructor(private http: HttpClient) { }
2627

27-
getDownloadUrl(filePathOnServer: string) {
28-
return BackendService.REST_API_DOWNLOAD_URL + '/' + filePathOnServer;
28+
getDownloadUrl(temporaryFileName: string, originalFileName: string) {
29+
return `${BackendService.REST_API_DOWNLOAD_URL}/${temporaryFileName}/${originalFileName}`;
2930
}
3031

3132
uploadImage(base64Image: string, type: string) {
32-
const sanitizedBase64 = base64Image.replace('data:image/png;base64,', '');
3333
return this.http.post<KartoffelstampfImageUploadResponse>(
3434
BackendService.REST_API_UPLOAD_URL, {
35-
fileContent: sanitizedBase64,
36-
fileType: type
35+
contentDataUri: base64Image,
3736
} as KartoffelstampfImageUploadRequest, httpOptions)
3837
.pipe(
3938
catchError((e: HttpErrorResponse) => {
@@ -45,17 +44,11 @@ export class BackendService {
4544
);
4645
}
4746

48-
runCompressPngCommand(filePathOnServer: string): Observable<KartoffelstampfTerminalOutputEntry> {
47+
runCompressCommand(compressInstruction: KartoffelstampfCompressInstruction): Observable<KartoffelstampfTerminalOutputEntry> {
4948
const ws = new WebSocket(BackendService.WEB_SOCKET_COMPRESS_URL);
5049
const subject = new Subject<KartoffelstampfTerminalOutputEntry>();
5150
ws.onopen = function (event) {
52-
ws.send(JSON.stringify({
53-
command: 'optipng',
54-
commandArguments: [
55-
'-o5',
56-
'/u/' + filePathOnServer // e.g. foo.png
57-
]
58-
}));
51+
ws.send(JSON.stringify(compressInstruction));
5952
};
6053
ws.onmessage = function(event: MessageEvent) {
6154
const kartoffelstampfTerminalOutputEntry: KartoffelstampfTerminalOutputEntry = JSON.parse(event.data);

src/app/terminal-output/terminal-output.component.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
<p>
2-
terminal-output works!
3-
x
4-
</p>
5-
61
<div *ngFor="let line of lines; let i=index" class="{{ line.type }}">
72
<div [innerHTML]="replaceNewlineWithBr(line.html)"></div>
83
</div>

src/app/terminal-output/terminal-output.component.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import { TerminalLine } from '../types/kartoffelstampf-client';
1313
border-left: 10px solid green;
1414
font-family: "Lucida Console", Monaco, monospace;
1515
}`,
16+
`.cmd {
17+
width: 90%;
18+
background-color: #333;
19+
color: #fff;
20+
padding: 8px;
21+
border-left: 10px solid #333;
22+
font-family: "Lucida Console", Monaco, monospace;
23+
}`,
1624
`.stderr {
1725
width: 90%;
1826
background-color: #555;
@@ -34,13 +42,19 @@ import { TerminalLine } from '../types/kartoffelstampf-client';
3442
export class TerminalOutputComponent {
3543

3644
linesInternal: TerminalLine[] = [];
45+
46+
@Input() temporaryFileName: string;
47+
@Input() originalFileName: string;
48+
3749
@Output() linesChange = new EventEmitter();
3850

3951
constructor() { }
4052

4153
replaceNewlineWithBr(text: string): string {
4254
if (text !== undefined && text !== null) {
43-
return text.replace(/\n/g, '<br/>');
55+
return text
56+
.replace(/\n/g, '<br/>')
57+
.replace(this.temporaryFileName, this.originalFileName);
4458
}
4559
return text;
4660
}

src/app/types/kartoffelstampf-server.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ export interface KartoffelstampfTerminalOutputEntry {
1212
}
1313

1414
export interface KartoffelstampfImageUploadRequest {
15-
fileContent: string;
16-
fileType: string;
15+
contentDataUri: string;
1716
}
1817
export interface KartoffelstampfImageUploadResponse {
1918
fileName: string;
2019
}
20+
21+
export class KartoffelstampfCompressInstruction {
22+
23+
public static COMPRESS_TYPE_LOSSLESS = 'LOSSLESS';
24+
25+
public compressType: string;
26+
public temporaryFileName: string;
27+
28+
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<div class="container">
22
<input type="file" (change)="processFileToBase64DataURI($event)">
3-
<pre>{{uploadedFileBase64URI}}</pre>
4-
<p>{{compressDone}}</p>
3+
<p *ngIf="originalFileName && !compressDone!">COMPRESSING PLEASE WAIT ...</p>
4+
<p *ngIf="originalFileName && compressDone!">COMPRESSING DONE :)</p>
5+
<div *ngIf="originalFileName">
6+
<h3>{{originalFileName}}</h3>
7+
<app-terminal-output
8+
[temporaryFileName]="temporaryFileName"
9+
[originalFileName]="originalFileName"
10+
[lines]="terminalLines"
11+
></app-terminal-output>
12+
</div>
513
<a *ngIf="compressDone == true" [href]="getDownloadUrl()">download</a>
6-
<h3>Terminal output</h3>
7-
<app-terminal-output [lines]="terminalLines"></app-terminal-output>
814
</div>

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { BackendService } from '../backend.service';
3-
import { KartoffelstampfTerminalOutputEntry } from '../types/kartoffelstampf-server';
3+
import { KartoffelstampfTerminalOutputEntry, KartoffelstampfCompressInstruction } from '../types/kartoffelstampf-server';
44
import { TerminalLine } from '../types/kartoffelstampf-client';
55
import { finalize } from 'rxjs/operators';
66

@@ -14,7 +14,8 @@ export class UploadPageComponent implements OnInit {
1414

1515
terminalLines: TerminalLine[] = [];
1616
uploadedFileBase64URI: string;
17-
compressedFilePath: string;
17+
originalFileName: string;
18+
temporaryFileName: string;
1819
compressDone = false;
1920

2021
constructor(private backendService: BackendService) { }
@@ -29,13 +30,14 @@ export class UploadPageComponent implements OnInit {
2930
const el = event.srcElement as HTMLInputElement;
3031
if (el.files && el.files[0]) {
3132
const fileReader = new FileReader();
33+
self.originalFileName = el.files[0].name;
3234
fileReader.addEventListener('load', function(loadedEvent: any) {
3335
self.uploadedFileBase64URI = loadedEvent.target.result;
3436
// Upload via backend
3537
self.backendService.uploadImage(self.uploadedFileBase64URI, 'PNG')
3638
.subscribe(uploadResponse => {
3739
console.log(uploadResponse.fileName);
38-
self.compressedFilePath = uploadResponse.fileName;
40+
self.temporaryFileName = uploadResponse.fileName;
3941
self.runCompressCommand();
4042
});
4143
});
@@ -44,12 +46,15 @@ export class UploadPageComponent implements OnInit {
4446
}
4547

4648
getDownloadUrl() {
47-
return this.backendService.getDownloadUrl(this.compressedFilePath);
49+
return this.backendService.getDownloadUrl(this.temporaryFileName, this.originalFileName);
4850
}
4951

5052
runCompressCommand() {
5153
const self = this;
52-
self.backendService.runCompressPngCommand(self.compressedFilePath)
54+
self.backendService.runCompressCommand(<KartoffelstampfCompressInstruction>{
55+
compressType: KartoffelstampfCompressInstruction.COMPRESS_TYPE_LOSSLESS,
56+
temporaryFileName: this.temporaryFileName,
57+
})
5358
.pipe(
5459
finalize(() => {
5560
console.log('compress-done!');

src/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/* You can add global styles to this file, and also import other style files */
22

3+
body {
4+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
5+
}

0 commit comments

Comments
 (0)