11import { Component , OnInit , OnDestroy } from '@angular/core' ;
22import { BackendService } from '../services/backend.service' ;
33import { KartoffelstampfTerminalOutputEntry , KartoffelstampfCompressInstruction } from '../types/kartoffelstampf-server' ;
4- import { TerminalLine } from '../types/kartoffelstampf-client' ;
4+ import { TerminalLine , CompressImageJobItem } from '../types/kartoffelstampf-client' ;
55import { finalize , takeUntil } from 'rxjs/operators' ;
66import { Subject } from 'rxjs' ;
77
@@ -20,17 +20,33 @@ import { Subject } from 'rxjs';
2020 `.drop-container---drag-over { border-color: #0DFF0D; }` ,
2121 `.drop-container---drag-leave { border-color: #ccc; }` ,
2222 `.drop-container---drag-drop { border-color: #00A200; }` ,
23+ `.fileTable {
24+ width:100%;
25+ }` ,
26+ `.fileTable th {
27+ font-weight:bold;
28+ text-align:left;
29+ color:#999;
30+ font-size:12px;
31+ padding-bottom:8px;
32+ }` ,
33+ `.download { background-color:#00A200; color:#fff; padding:3px 6px; text-decoration: none; }` ,
34+ `.download---clicked { background-color:#777; }` ,
35+ `.terminal-line-td { padding:5px 10px 20px 27px; }` ,
36+ `.expandable {
37+ cursor: pointer;
38+ display:flex;
39+ flex-direction: row;
40+ justify-content: flex-start;
41+ align-items: center;
42+ }` ,
2343 ] ,
2444 providers : [ BackendService ]
2545} )
2646export class UploadPageComponent implements OnInit , OnDestroy {
2747
2848 preDestroy = new Subject < boolean > ( ) ;
29- terminalLines : TerminalLine [ ] = [ ] ;
30- uploadedFileBase64URI : string ;
31- originalFileName : string ;
32- temporaryFileName : string ;
33- compressDone = false ;
49+ imageCompressJobs : CompressImageJobItem [ ] = [ ] ;
3450
3551 uiStateDrop = false ;
3652 uiStateDragOver = false ;
@@ -84,53 +100,61 @@ export class UploadPageComponent implements OnInit, OnDestroy {
84100 processFileToBase64DataURI ( files : FileList ) {
85101 const self = this ;
86102 if ( files && files [ 0 ] ) {
87- const fileReader = new FileReader ( ) ;
88- self . originalFileName = files [ 0 ] . name ;
89- fileReader . addEventListener ( 'load' , function ( loadedEvent : any ) {
90- self . uploadedFileBase64URI = loadedEvent . target . result ;
91- // Upload via backend
92- self . backendService
93- . uploadImage ( self . uploadedFileBase64URI , 'PNG' )
94- . pipe (
95- takeUntil ( self . preDestroy )
96- )
97- . subscribe ( uploadResponse => {
98- console . log ( uploadResponse . fileName ) ;
99- self . temporaryFileName = uploadResponse . fileName ;
100- self . runCompressCommand ( ) ;
103+ for ( let i = 0 ; i < files . length ; i ++ ) {
104+ const file = files [ i ] ;
105+ const job = new CompressImageJobItem ( ) ;
106+ const fileReader = new FileReader ( ) ;
107+ job . originalFileName = file . name ;
108+ job . originalSize = file . size ;
109+ fileReader . addEventListener ( 'load' , function ( loadedEvent : any ) {
110+ job . uploadedFileBase64URI = loadedEvent . target . result ;
111+ // Upload via backend
112+ self . backendService
113+ . uploadImage ( job . uploadedFileBase64URI , 'PNG' )
114+ . pipe (
115+ takeUntil ( self . preDestroy )
116+ )
117+ . subscribe ( uploadResponse => {
118+ job . temporaryFileName = uploadResponse . fileName ;
119+ self . imageCompressJobs . push ( job ) ;
120+ self . runCompressCommand ( job ) ;
121+ } ) ;
101122 } ) ;
102- } ) ;
103- fileReader . readAsDataURL ( files [ 0 ] ) ;
123+ fileReader . readAsDataURL ( file ) ;
124+ }
104125 self . activeStep = 2 ;
105126 }
106127 }
107128
108- getDownloadUrl ( ) {
109- return this . backendService . getDownloadUrl ( this . temporaryFileName , this . originalFileName ) ;
129+ getDownloadUrl ( job : CompressImageJobItem ) {
130+ return this . backendService . getDownloadUrl ( job . temporaryFileName , job . originalFileName ) ;
110131 }
111132
112- runCompressCommand ( ) {
133+ runCompressCommand ( job : CompressImageJobItem ) {
113134 const self = this ;
114135 self . backendService . runCompressCommand ( < KartoffelstampfCompressInstruction > {
115136 compressType : KartoffelstampfCompressInstruction . COMPRESS_TYPE_LOSSLESS ,
116- temporaryFileName : this . temporaryFileName ,
137+ temporaryFileName : job . temporaryFileName ,
117138 } )
118139 . pipe (
119140 finalize ( ( ) => {
120- console . log ( 'compress-done!' ) ;
121- self . compressDone = true ;
141+ job . compressDone = true ;
122142 } ) ,
123143 takeUntil ( self . preDestroy )
124144 )
125145 . subscribe ( data => {
126- const terminalLine = new TerminalLine ( data ) ;
127- const previousTerminalLine = self . terminalLines [ self . terminalLines . length - 1 ] ;
128- if ( previousTerminalLine !== undefined &&
129- previousTerminalLine . clearLine === true &&
130- terminalLine . clearLine === true ) {
131- self . terminalLines . pop ( ) ;
146+ if ( data . type === 'compressResult' ) {
147+ job . compressedSize = data . payload [ 'compressedSize' ] ;
148+ } else {
149+ const terminalLine = new TerminalLine ( data ) ;
150+ const previousTerminalLine = job . terminalLines [ job . terminalLines . length - 1 ] ;
151+ if ( previousTerminalLine !== undefined &&
152+ previousTerminalLine . clearLine === true &&
153+ terminalLine . clearLine === true ) {
154+ job . terminalLines . pop ( ) ;
155+ }
156+ job . terminalLines . push ( terminalLine ) ;
132157 }
133- self . terminalLines . push ( terminalLine ) ;
134158 } ) ;
135159 }
136160
0 commit comments