@@ -153,6 +153,7 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) {
153
153
logger .info ("Uploading input stream from " + fileInfo .getFullName () + " to " + key );
154
154
body = AsyncRequestBody .fromInputStream (fileInfo .getInputStream (), length , executorService );
155
155
}
156
+ entityFile .setUploading (true );
156
157
getClient ().putObject (request , body )
157
158
.whenComplete ((response , throwable ) -> {
158
159
if (throwable != null ) {
@@ -165,6 +166,7 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) {
165
166
if (fileToUpload != null && fileToUpload .delete ()) {
166
167
logger .info ("Deleted temporal file: " + fileToUpload );
167
168
}
169
+ entityFile .setUploading (false );
168
170
});
169
171
170
172
@@ -258,19 +260,28 @@ protected String getAccountFolderName(Long accountId) {
258
260
*/
259
261
protected String generateThumbnailURL (EntityFile entityFile , int w , int h ) {
260
262
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
+
261
271
String urlKey = entityFile .getUuid () + w + "x" + h ;
262
272
String url = URL_CACHE .get (urlKey );
263
273
if (url == null ) {
274
+ String bucketName = getBucketName ();
264
275
String folder = getAccountFolderName (entityFile .getAccountId ());
265
276
String fileName = getFileName (entityFile );
266
277
String thumbfileName = w + "x" + h + "/" + fileName ;
267
278
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 );
270
284
}
271
-
272
- url = generateStaticURL (getBucketName (), folder + thumbfileName );
273
- URL_CACHE .add (urlKey , url );
274
285
}
275
286
return url ;
276
287
} else {
@@ -281,24 +292,19 @@ protected String generateThumbnailURL(EntityFile entityFile, int w, int h) {
281
292
/**
282
293
* Create and upload thumbnail
283
294
*/
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 ) {
286
297
try {
287
298
288
299
File localDestination = File .createTempFile (System .currentTimeMillis () + "file" , entityFile .getName ());
289
300
File localThumbDestination = File .createTempFile (System .currentTimeMillis () + "thumb" , entityFile .getName ());
290
-
291
-
292
301
var url = download (entityFile ).getUrl ();
293
302
Files .copy (new URL (url ).openStream (), localDestination .toPath (), StandardCopyOption .REPLACE_EXISTING );
294
-
295
303
ImageUtil .resizeImage (localDestination , localThumbDestination , entityFile .getExtension (), w , h );
296
304
297
-
298
305
// metadata
299
306
var metadata = Map .of (
300
307
"thumbnail" , "true" ,
301
- "description" , entityFile .getDescription (),
302
308
"uuid" , entityFile .getUuid (),
303
309
"width" , String .valueOf (w ),
304
310
"height" , String .valueOf (h ));
@@ -307,25 +313,21 @@ protected void createAndUploadThumbnail(EntityFile entityFile, String bucketName
307
313
PutObjectRequest request = PutObjectRequest .builder ()
308
314
.bucket (bucketName )
309
315
.key (key )
316
+ .metadata (metadata )
310
317
.contentLength (localThumbDestination .length ())
311
318
.contentType ("image/" + entityFile .getExtension ())
312
319
.acl (ObjectCannedACL .PUBLIC_READ )
313
320
.build ();
314
321
315
322
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 ();
326
326
327
+ return generateStaticURL (bucketName , key );
327
328
} catch (Exception e ) {
328
329
logger .error ("Error creating thumbnail for " + entityFile .getName () + " " + w + "x" + h + " " + fileName , e );
330
+ return null ;
329
331
}
330
332
}
331
333
0 commit comments