@@ -153,6 +153,7 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) {
153153 logger .info ("Uploading input stream from " + fileInfo .getFullName () + " to " + key );
154154 body = AsyncRequestBody .fromInputStream (fileInfo .getInputStream (), length , executorService );
155155 }
156+ entityFile .setUploading (true );
156157 getClient ().putObject (request , body )
157158 .whenComplete ((response , throwable ) -> {
158159 if (throwable != null ) {
@@ -165,6 +166,7 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) {
165166 if (fileToUpload != null && fileToUpload .delete ()) {
166167 logger .info ("Deleted temporal file: " + fileToUpload );
167168 }
169+ entityFile .setUploading (false );
168170 });
169171
170172
@@ -258,19 +260,28 @@ protected String getAccountFolderName(Long accountId) {
258260 */
259261 protected String generateThumbnailURL (EntityFile entityFile , int w , int h ) {
260262 if (entityFile .getType () == EntityFileType .IMAGE || EntityFileType .getFileType (entityFile .getExtension ()) == EntityFileType .IMAGE ) {
263+ if (entityFile .isUploading ()) {
264+ try {
265+ Thread .sleep (1000 );
266+ } catch (InterruptedException e ) {
267+
268+ }
269+ }
270+
261271 String urlKey = entityFile .getUuid () + w + "x" + h ;
262272 String url = URL_CACHE .get (urlKey );
263273 if (url == null ) {
274+ String bucketName = getBucketName ();
264275 String folder = getAccountFolderName (entityFile .getAccountId ());
265276 String fileName = getFileName (entityFile );
266277 String thumbfileName = w + "x" + h + "/" + fileName ;
267278
268- if (!objectExists (getBucketName (), folder + thumbfileName )) {
269- createAndUploadThumbnail (entityFile , getBucketName (), folder , fileName , thumbfileName , w , h );
279+ if (!objectExists (bucketName , folder + thumbfileName )) {
280+ url = createAndUploadThumbnail (entityFile , bucketName , folder , fileName , thumbfileName , w , h );
281+ }
282+ if (url != null ) {
283+ URL_CACHE .add (urlKey , url );
270284 }
271-
272- url = generateStaticURL (getBucketName (), folder + thumbfileName );
273- URL_CACHE .add (urlKey , url );
274285 }
275286 return url ;
276287 } else {
@@ -281,24 +292,19 @@ protected String generateThumbnailURL(EntityFile entityFile, int w, int h) {
281292 /**
282293 * Create and upload thumbnail
283294 */
284- protected void createAndUploadThumbnail (EntityFile entityFile , String bucketName , String folder , String fileName , String thumbfileName ,
285- int w , int h ) {
295+ protected String createAndUploadThumbnail (EntityFile entityFile , String bucketName , String folder , String fileName , String thumbfileName ,
296+ int w , int h ) {
286297 try {
287298
288299 File localDestination = File .createTempFile (System .currentTimeMillis () + "file" , entityFile .getName ());
289300 File localThumbDestination = File .createTempFile (System .currentTimeMillis () + "thumb" , entityFile .getName ());
290-
291-
292301 var url = download (entityFile ).getUrl ();
293302 Files .copy (new URL (url ).openStream (), localDestination .toPath (), StandardCopyOption .REPLACE_EXISTING );
294-
295303 ImageUtil .resizeImage (localDestination , localThumbDestination , entityFile .getExtension (), w , h );
296304
297-
298305 // metadata
299306 var metadata = Map .of (
300307 "thumbnail" , "true" ,
301- "description" , entityFile .getDescription (),
302308 "uuid" , entityFile .getUuid (),
303309 "width" , String .valueOf (w ),
304310 "height" , String .valueOf (h ));
@@ -307,25 +313,21 @@ protected void createAndUploadThumbnail(EntityFile entityFile, String bucketName
307313 PutObjectRequest request = PutObjectRequest .builder ()
308314 .bucket (bucketName )
309315 .key (key )
316+ .metadata (metadata )
310317 .contentLength (localThumbDestination .length ())
311318 .contentType ("image/" + entityFile .getExtension ())
312319 .acl (ObjectCannedACL .PUBLIC_READ )
313320 .build ();
314321
315322
316- getClient ().putObject (request , AsyncRequestBody .fromFile (localThumbDestination ))
317- .whenComplete ((putObjectResponse , throwable ) -> {
318- if (throwable != null ) {
319- logger .error ("Error uploading thumbnail " + localDestination , throwable );
320- } else {
321- logger .info ("Thumbnail uploaded " + key );
322- }
323-
324- localThumbDestination .delete ();
325- });
323+ var future = getClient ().putObject (request , AsyncRequestBody .fromFile (localThumbDestination ));
324+ var response = future .get ();
325+ localThumbDestination .delete ();
326326
327+ return generateStaticURL (bucketName , key );
327328 } catch (Exception e ) {
328329 logger .error ("Error creating thumbnail for " + entityFile .getName () + " " + w + "x" + h + " " + fileName , e );
330+ return null ;
329331 }
330332 }
331333
0 commit comments