@@ -3,7 +3,7 @@ import { Http } from '@angular/http';
33import { BehaviorSubject , Subject } from 'rxjs' ;
44import { ParsersService } from './parsers.service' ;
55import { LoggerService } from './logger.service' ;
6- import { PreviewData , ImageContent , ParsedUserConfiguration , Images , PreviewStateVariables , LoadStatus , PreferedImages , ImagesStatusAndContent } from '../models' ;
6+ import { PreviewData , ImageContent , ParsedUserConfiguration , Images , PreviewStateVariables , LoadStatus , PreferedImages , ImagesStatusAndContent , ImageProviderData } from '../models' ;
77import { VDFList , Reference , ImageProvider } from "../lib" ;
88import { union } from "lodash" ;
99import * as fs from 'fs-extra' ;
@@ -26,6 +26,7 @@ export class PreviewService {
2626 listIsBeingSaved : false ,
2727 listIsUpdating : false ,
2828 skipDownloading : false ,
29+ greedySearch : false ,
2930 numberOfListItems : 0
3031 } ;
3132 this . previewDataChanged = new Subject < boolean > ( ) ;
@@ -51,8 +52,6 @@ export class PreviewService {
5152 else if ( this . stateVariables . listIsBeingSaved )
5253 return this . loggerService . info ( 'Files are being saved. Please wait.' , { invokeAlert : true , alertTimeout : 3000 } ) ;
5354
54- this . loggerService . info ( 'Please shutdown Steam if it is running when saving, otherwise it might not save correctly.' , { invokeAlert : true , alertTimeout : 10000 } ) ;
55-
5655 this . stateVariables . listIsUpdating = true ;
5756 this . imageProvider . stopUrlDownload ( ) ;
5857 this . generatePreviewDataCallback ( ) ;
@@ -183,15 +182,14 @@ export class PreviewService {
183182 }
184183 else {
185184 this . previewData . next ( undefined ) ;
185+ this . loggerService . info ( 'Executing parsers.' , { invokeAlert : true } ) ;
186186 this . parsersService . executeFileParser ( ) . then ( ( data ) => {
187187 if ( data . parsedData . length > 0 ) {
188- let titles = this . getAllTitles ( data . parsedData ) ;
188+ this . loggerService . info ( 'Please shutdown Steam if it is running when saving, otherwise it might not save correctly.' , { invokeAlert : true , alertTimeout : 5000 } ) ;
189+
189190 this . steamDirectories = this . getAllSteamDirectories ( data . parsedData ) ;
190191 this . images = { } ;
191192
192- for ( let i = 0 ; i < titles . length ; i ++ )
193- this . images [ titles [ i ] ] = { status : this . stateVariables . skipDownloading ? 'retrieved' : 'none' , content : [ ] } ;
194-
195193 let previewData : { numberOfItems : number , data : PreviewData } = this . createPreviewData ( data . parsedData ) ;
196194
197195 if ( previewData . numberOfItems > 0 ) {
@@ -239,17 +237,6 @@ export class PreviewService {
239237 }
240238 }
241239
242- private getAllTitles ( data : ParsedUserConfiguration [ ] ) {
243- let title : string [ ] = [ ] ;
244- for ( let i = 0 ; i < data . length ; i ++ ) {
245- for ( let j = 0 ; j < data [ i ] . files . length ; j ++ ) {
246- if ( title . indexOf ( data [ i ] . files [ j ] . fuzzyTitle ) === - 1 )
247- title . push ( data [ i ] . files [ j ] . fuzzyTitle ) ;
248- }
249- }
250- return title ;
251- }
252-
253240 private getAllSteamDirectories ( data : ParsedUserConfiguration [ ] ) {
254241 let title : string [ ] = [ ] ;
255242 for ( let i = 0 ; i < data . length ; i ++ ) {
@@ -291,6 +278,14 @@ export class PreviewService {
291278 }
292279 }
293280
281+ if ( this . images [ data [ i ] . files [ j ] . fuzzyTitle ] === undefined )
282+ this . images [ data [ i ] . files [ j ] . fuzzyTitle ] = { status : this . stateVariables . skipDownloading ? 'retrieved' : 'none' , searchTitles : [ data [ i ] . files [ j ] . fuzzyTitle ] , content : [ ] } ;
283+
284+ if ( this . stateVariables . greedySearch ) {
285+ if ( this . images [ data [ i ] . files [ j ] . fuzzyTitle ] . searchTitles . indexOf ( data [ i ] . files [ j ] . extractedTitle ) === - 1 )
286+ this . images [ data [ i ] . files [ j ] . fuzzyTitle ] . searchTitles . push ( data [ i ] . files [ j ] . extractedTitle ) ;
287+ }
288+
294289 if ( data [ i ] . files [ j ] . localImages . length ) {
295290 for ( let k = 0 ; k < data [ i ] . files [ j ] . localImages . length ; k ++ ) {
296291 this . addUniqueImage ( data [ i ] . files [ j ] . fuzzyTitle , { imageProvider : 'LocalStorage' , imageUrl : data [ i ] . files [ j ] . localImages [ k ] , loadStatus : 'downloaded' } )
@@ -315,27 +310,45 @@ export class PreviewService {
315310 let promises : Promise < any > [ ] = [ ] ;
316311 for ( let fuzzyTitle in this . images ) {
317312 this . images [ fuzzyTitle ] . status = 'retrieving' ;
318- promises . push ( this . imageProvider . retrieveUrls ( fuzzyTitle , ...imageProviders ) . then ( ( data ) => {
313+ promises . push ( new Promise ( ( resolve , reject ) => {
314+ let searchPromises : Promise < ImageProviderData > [ ] = [ ] ;
315+ for ( let i = 0 ; i < this . images [ fuzzyTitle ] . searchTitles . length ; i ++ ) {
316+ searchPromises . push ( this . imageProvider . retrieveUrls ( this . images [ fuzzyTitle ] . searchTitles [ i ] , ...imageProviders ) /*.then((data) => {
317+ return Promise.resolve(data);
318+ })*/ ) ;
319+ }
320+ Promise . all ( searchPromises ) . then ( ( dataArray ) => {
321+ let data : ImageProviderData = { failed : [ ] , images : [ ] } ;
319322
320- if ( data . failed . length ) {
321- this . loggerService . error ( `Failed to retrieve some image urls for "${ fuzzyTitle } "` , { invokeAlert : true , alertTimeout : 3000 } ) ;
322- for ( let i = 0 ; i < data . failed . length ; i ++ ) {
323- this . loggerService . error ( data . failed [ i ] ) ;
323+ for ( let j = 0 ; j < dataArray . length ; j ++ ) {
324+ data . failed = data . failed . concat ( dataArray [ j ] . failed ) ;
325+ data . images = data . images . concat ( dataArray [ j ] . images ) ;
326+ }
327+
328+ if ( data . failed . length ) {
329+ this . loggerService . error ( `Failed to retrieve some image urls for "${ fuzzyTitle } "` , { invokeAlert : true , alertTimeout : 3000 } ) ;
330+ for ( let i = 0 ; i < data . failed . length ; i ++ ) {
331+ this . loggerService . error ( data . failed [ i ] ) ;
332+ }
324333 }
325- }
326334
327- this . addUniqueImage ( fuzzyTitle , ...data . images ) ;
335+ this . addUniqueImage ( fuzzyTitle , ...data . images ) ;
328336
329- if ( this . preferedImages !== undefined ) {
330- for ( let title in this . preferedImages ) {
331- let index = this . images [ fuzzyTitle ] . content . findIndex ( ( image ) => this . preferedImages [ title ] === image . imageUrl ) ;
332- if ( index !== - 1 && previewData [ title ] !== undefined )
333- previewData [ title ] . currentImageIndex = index ;
337+ if ( this . preferedImages !== undefined ) {
338+ for ( let title in this . preferedImages ) {
339+ let index = this . images [ fuzzyTitle ] . content . findIndex ( ( image ) => this . preferedImages [ title ] === image . imageUrl ) ;
340+ if ( index !== - 1 && previewData [ title ] !== undefined )
341+ previewData [ title ] . currentImageIndex = index ;
342+ }
334343 }
335- }
336344
337- this . images [ fuzzyTitle ] . status = 'retrieved' ;
338- this . previewDataChanged . next ( ) ;
345+ this . images [ fuzzyTitle ] . status = 'retrieved' ;
346+ this . previewDataChanged . next ( ) ;
347+
348+ resolve ( ) ;
349+ } ) . catch ( ( error ) => {
350+ reject ( error ) ;
351+ } ) ;
339352 } ) ) ;
340353 }
341354 Promise . all ( promises ) . then ( ( ) => {
0 commit comments