1919import java .util .Map ;
2020import java .util .Optional ;
2121import java .util .concurrent .CompletableFuture ;
22+ import java .util .concurrent .Executor ;
2223import java .util .concurrent .ExecutorService ;
2324
2425import static com .google .common .base .Preconditions .checkNotNull ;
@@ -86,23 +87,20 @@ public CompletableFuture<Void> uploadDirectory(Optional<GooglePhotosAlbum> googl
8687 directoryProgressStatus .updateDescription (googlePhotosAlbum .map (GooglePhotosAlbum ::getTitle ).orElse ("" ));
8788 var createMediaDataResultsFuture = paths .stream ()
8889 .sorted (comparing (path -> path .getFileName ().toString ()))
89- .map (path -> {
90- fileProgressStatus .updateDescription (path .toAbsolutePath ().toString ());
91- return createMediaData (path )
92- .thenApply (itemState -> {
93- itemState .toFailure ().ifPresentOrElse (
94- error -> fileProgressStatus .addFailure (KeyedError .of (path , error )),
95- fileProgressStatus ::incrementSuccess );
96- return PathState .of (path , itemState );
97- });
98- })
90+ .map (path -> createMediaData (path , fileProgressStatus )
91+ .thenApply (itemState -> {
92+ itemState .toFailure ().ifPresentOrElse (
93+ error -> fileProgressStatus .addFailure (KeyedError .of (path , error )),
94+ fileProgressStatus ::incrementSuccess );
95+ return PathState .of (path , itemState );
96+ }))
9997 .collect (toFutureOfList ());
10098 return addToAlbumStrategy .addToAlbum (
10199 createMediaDataResultsFuture ,
102100 googlePhotosAlbum ,
103101 fileProgressStatus ,
104- ( albumId , pathStates ) -> createMediaItems ( albumId , fileProgressStatus , pathStates ) ,
105- this ::getItemState );
102+ directoryProgressStatus ,
103+ ( albumId , pathStates ) -> createMediaItems ( albumId , fileProgressStatus , pathStates ), this ::getItemState );
106104 });
107105 }
108106
@@ -212,14 +210,14 @@ private void forgetUploadState() {
212210 memoryBarrier = true ;
213211 }
214212
215- private CompletableFuture <ResultOrFailure <ItemState >> createMediaData (Path file ) {
213+ private CompletableFuture <ResultOrFailure <ItemState >> createMediaData (Path file , ProgressStatus fileProgressStatus ) {
216214 checkStarted ();
217215 checkState (memoryBarrier );
218216 return uploadedItemStateByPath .compute (file ,
219217 (theFile , currentFuture ) -> {
220218 if (currentFuture == null || currentFuture .isCompletedExceptionally ()) {
221219 logger .info ("Scheduling upload of {}" , file );
222- currentFuture = doCreateMediaData (theFile );
220+ currentFuture = doCreateMediaData (theFile , fileProgressStatus );
223221 } else {
224222 var itemState = currentFuture .getNow (null );
225223 if (itemState != null ) {
@@ -236,7 +234,7 @@ private CompletableFuture<ResultOrFailure<ItemState>> createMediaData(Path file)
236234 })
237235 .orElseGet (() -> {
238236 logger .info ("Uploaded, but upload token expired, re-uploading: {}" , file );
239- return doCreateMediaData (theFile );
237+ return doCreateMediaData (theFile , fileProgressStatus );
240238 });
241239 } else {
242240 logger .error ("Unexpected future state for {}: {}" , file , currentFuture );
@@ -257,7 +255,7 @@ private CompletableFuture<ResultOrFailure<ItemState>> createMediaData(Path file)
257255 .orElseGet (() -> {
258256 if (backOffHandler .handle (operationName , exception ).isPresent ()) {
259257 logger .debug ("Retrying upload of {}" , file );
260- return createMediaData (file );
258+ return createMediaData (file , fileProgressStatus );
261259 } else {
262260 throw new RuntimeException (exception );
263261 }
@@ -274,8 +272,8 @@ private boolean uploadTokenNotExpired(Path file, UploadMediaItemState uploadMedi
274272 return notExpired ;
275273 }
276274
277- private CompletableFuture <ItemState > doCreateMediaData (Path file ) {
278- return googlePhotosClient .uploadMediaData (file , executorService )
275+ private CompletableFuture <ItemState > doCreateMediaData (Path file , ProgressStatus fileProgressStatus ) {
276+ return googlePhotosClient .uploadMediaData (file , statusUpdatingExecutor ( file , fileProgressStatus ) )
279277 .thenApply (uploadToken -> {
280278 logger .info ("Uploaded file {}" , file );
281279 logger .debug ("Upload token {}" , uploadToken );
@@ -284,4 +282,11 @@ private CompletableFuture<ItemState> doCreateMediaData(Path file) {
284282 .build ();
285283 });
286284 }
285+
286+ private Executor statusUpdatingExecutor (Path file , ProgressStatus fileProgressStatus ) {
287+ return command -> executorService .execute (() -> {
288+ fileProgressStatus .updateDescription (file .toAbsolutePath ().toString ());
289+ command .run ();
290+ });
291+ }
287292}
0 commit comments