Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sources/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
<parent>
<artifactId>tools.dynamia.modules.entityfiles.parent</artifactId>
<groupId>tools.dynamia.modules</groupId>
<version>7.2.0</version>
<version>7.2.1</version>
</parent>
<name>Dynamia Modules - EntityFiles - Core</name>
<artifactId>tools.dynamia.modules.entityfiles</artifactId>
<version>7.2.0</version>
<version>7.2.1</version>
<url>https://www.dynamia.tools/modules/entityfiles</url>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
14 changes: 7 additions & 7 deletions sources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<groupId>tools.dynamia.modules</groupId>
<artifactId>tools.dynamia.modules.entityfiles.parent</artifactId>
<packaging>pom</packaging>
<version>7.2.0</version>
<version>7.2.1</version>
<name>Dynamia Modules - EntityFiles</name>
<url>https://dynamia.tools/modules/entityfiles</url>
<description>DynamiaTools extension to attach files to entities</description>
Expand Down Expand Up @@ -63,9 +63,9 @@

<properties>
<file.encoding>UTF-8</file.encoding>
<dynamiatools.version>5.2.1</dynamiatools.version>
<springboot.version>3.3.3</springboot.version>
<aws.version>2.28.11</aws.version>
<dynamiatools.version>5.3.1</dynamiatools.version>
<springboot.version>3.4.1</springboot.version>
<aws.version>2.29.47</aws.version>
<java.version>17</java.version>
<maven.compiler>3.13.0</maven.compiler>
<source.encoding>UTF-8</source.encoding>
Expand Down Expand Up @@ -99,7 +99,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.10.0</version>
<version>3.11.2</version>
<configuration>
<failOnError>false</failOnError>
<doclint>none</doclint>
Expand All @@ -122,7 +122,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
<version>2.18.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -175,7 +175,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.1.0</version>
<version>3.2.4</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
4 changes: 2 additions & 2 deletions sources/s3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<parent>
<groupId>tools.dynamia.modules</groupId>
<artifactId>tools.dynamia.modules.entityfiles.parent</artifactId>
<version>7.2.0</version>
<version>7.2.1</version>
</parent>

<name>Dynamia Modules - EntityFiles - S3</name>
<artifactId>tools.dynamia.modules.entityfiles.s3</artifactId>
<version>7.2.0</version>
<version>7.2.1</version>
<url>https://www.dynamia.tools/modules/entityfiles</url>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});


Expand Down Expand Up @@ -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 {
Expand All @@ -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));
Expand All @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions sources/ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
<parent>
<artifactId>tools.dynamia.modules.entityfiles.parent</artifactId>
<groupId>tools.dynamia.modules</groupId>
<version>7.2.0</version>
<version>7.2.1</version>
</parent>
<name>Dynamia Modules - EntityFiles UI</name>
<artifactId>tools.dynamia.modules.entityfiles.ui</artifactId>
<version>7.2.0</version>
<version>7.2.1</version>
<url>https://www.dynamia.tools/modules/entityfiles</url>

<build>
Expand Down
Loading