diff --git a/sources/core/pom.xml b/sources/core/pom.xml index 3479cc3..b19b3dd 100644 --- a/sources/core/pom.xml +++ b/sources/core/pom.xml @@ -22,11 +22,11 @@ tools.dynamia.modules.entityfiles.parent tools.dynamia.modules - 7.2.0 + 7.2.1 Dynamia Modules - EntityFiles - Core tools.dynamia.modules.entityfiles - 7.2.0 + 7.2.1 https://www.dynamia.tools/modules/entityfiles diff --git a/sources/core/src/main/java/tools/dynamia/modules/entityfile/domain/EntityFile.java b/sources/core/src/main/java/tools/dynamia/modules/entityfile/domain/EntityFile.java index f128e7c..f8f5dbd 100644 --- a/sources/core/src/main/java/tools/dynamia/modules/entityfile/domain/EntityFile.java +++ b/sources/core/src/main/java/tools/dynamia/modules/entityfile/domain/EntityFile.java @@ -80,6 +80,9 @@ public class EntityFile extends BaseEntity implements URLable { private Long accountId; private String externalRef; + @Transient + private boolean uploading; + public String getStoredFileName() { return storedFileName; } @@ -298,4 +301,12 @@ public void url(String url) { public void name(String name) { setStoredFileName(name); } + + public boolean isUploading() { + return uploading; + } + + public void setUploading(boolean uploading) { + this.uploading = uploading; + } } diff --git a/sources/pom.xml b/sources/pom.xml index 978a58b..75d68b1 100644 --- a/sources/pom.xml +++ b/sources/pom.xml @@ -22,7 +22,7 @@ tools.dynamia.modules tools.dynamia.modules.entityfiles.parent pom - 7.2.0 + 7.2.1 Dynamia Modules - EntityFiles https://dynamia.tools/modules/entityfiles DynamiaTools extension to attach files to entities @@ -63,9 +63,9 @@ UTF-8 - 5.2.1 - 3.3.3 - 2.28.11 + 5.3.1 + 3.4.1 + 2.29.47 17 3.13.0 UTF-8 @@ -99,7 +99,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.10.0 + 3.11.2 false none @@ -122,7 +122,7 @@ commons-io commons-io - 2.16.1 + 2.18.0 @@ -175,7 +175,7 @@ org.apache.maven.plugins maven-gpg-plugin - 3.1.0 + 3.2.4 sign-artifacts diff --git a/sources/s3/pom.xml b/sources/s3/pom.xml index 2849e36..8a3d3cc 100644 --- a/sources/s3/pom.xml +++ b/sources/s3/pom.xml @@ -23,12 +23,12 @@ tools.dynamia.modules tools.dynamia.modules.entityfiles.parent - 7.2.0 + 7.2.1 Dynamia Modules - EntityFiles - S3 tools.dynamia.modules.entityfiles.s3 - 7.2.0 + 7.2.1 https://www.dynamia.tools/modules/entityfiles diff --git a/sources/s3/src/main/java/tools/dynamia/modules/entityfiles/s3/S3EntityFileStorage.java b/sources/s3/src/main/java/tools/dynamia/modules/entityfiles/s3/S3EntityFileStorage.java index a30cf70..1549714 100644 --- a/sources/s3/src/main/java/tools/dynamia/modules/entityfiles/s3/S3EntityFileStorage.java +++ b/sources/s3/src/main/java/tools/dynamia/modules/entityfiles/s3/S3EntityFileStorage.java @@ -153,6 +153,7 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) { logger.info("Uploading input stream from " + fileInfo.getFullName() + " to " + key); body = AsyncRequestBody.fromInputStream(fileInfo.getInputStream(), length, executorService); } + entityFile.setUploading(true); getClient().putObject(request, body) .whenComplete((response, throwable) -> { if (throwable != null) { @@ -165,6 +166,7 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) { if (fileToUpload != null && fileToUpload.delete()) { logger.info("Deleted temporal file: " + fileToUpload); } + entityFile.setUploading(false); }); @@ -258,19 +260,28 @@ protected String getAccountFolderName(Long accountId) { */ protected String generateThumbnailURL(EntityFile entityFile, int w, int h) { if (entityFile.getType() == EntityFileType.IMAGE || EntityFileType.getFileType(entityFile.getExtension()) == EntityFileType.IMAGE) { + if (entityFile.isUploading()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + + } + } + String urlKey = entityFile.getUuid() + w + "x" + h; String url = URL_CACHE.get(urlKey); if (url == null) { + String bucketName = getBucketName(); String folder = getAccountFolderName(entityFile.getAccountId()); String fileName = getFileName(entityFile); String thumbfileName = w + "x" + h + "/" + fileName; - if (!objectExists(getBucketName(), folder + thumbfileName)) { - createAndUploadThumbnail(entityFile, getBucketName(), folder, fileName, thumbfileName, w, h); + if (!objectExists(bucketName, folder + thumbfileName)) { + url = createAndUploadThumbnail(entityFile, bucketName, folder, fileName, thumbfileName, w, h); + } + if (url != null) { + URL_CACHE.add(urlKey, url); } - - url = generateStaticURL(getBucketName(), folder + thumbfileName); - URL_CACHE.add(urlKey, url); } return url; } else { @@ -281,24 +292,19 @@ protected String generateThumbnailURL(EntityFile entityFile, int w, int h) { /** * Create and upload thumbnail */ - protected void createAndUploadThumbnail(EntityFile entityFile, String bucketName, String folder, String fileName, String thumbfileName, - int w, int h) { + protected String createAndUploadThumbnail(EntityFile entityFile, String bucketName, String folder, String fileName, String thumbfileName, + int w, int h) { try { File localDestination = File.createTempFile(System.currentTimeMillis() + "file", entityFile.getName()); File localThumbDestination = File.createTempFile(System.currentTimeMillis() + "thumb", entityFile.getName()); - - var url = download(entityFile).getUrl(); Files.copy(new URL(url).openStream(), localDestination.toPath(), StandardCopyOption.REPLACE_EXISTING); - ImageUtil.resizeImage(localDestination, localThumbDestination, entityFile.getExtension(), w, h); - // metadata var metadata = Map.of( "thumbnail", "true", - "description", entityFile.getDescription(), "uuid", entityFile.getUuid(), "width", String.valueOf(w), "height", String.valueOf(h)); @@ -307,25 +313,21 @@ protected void createAndUploadThumbnail(EntityFile entityFile, String bucketName PutObjectRequest request = PutObjectRequest.builder() .bucket(bucketName) .key(key) + .metadata(metadata) .contentLength(localThumbDestination.length()) .contentType("image/" + entityFile.getExtension()) .acl(ObjectCannedACL.PUBLIC_READ) .build(); - getClient().putObject(request, AsyncRequestBody.fromFile(localThumbDestination)) - .whenComplete((putObjectResponse, throwable) -> { - if (throwable != null) { - logger.error("Error uploading thumbnail " + localDestination, throwable); - } else { - logger.info("Thumbnail uploaded " + key); - } - - localThumbDestination.delete(); - }); + var future = getClient().putObject(request, AsyncRequestBody.fromFile(localThumbDestination)); + var response = future.get(); + localThumbDestination.delete(); + return generateStaticURL(bucketName, key); } catch (Exception e) { logger.error("Error creating thumbnail for " + entityFile.getName() + " " + w + "x" + h + " " + fileName, e); + return null; } } diff --git a/sources/ui/pom.xml b/sources/ui/pom.xml index 8ed45fc..386095d 100644 --- a/sources/ui/pom.xml +++ b/sources/ui/pom.xml @@ -22,11 +22,11 @@ tools.dynamia.modules.entityfiles.parent tools.dynamia.modules - 7.2.0 + 7.2.1 Dynamia Modules - EntityFiles UI tools.dynamia.modules.entityfiles.ui - 7.2.0 + 7.2.1 https://www.dynamia.tools/modules/entityfiles