Skip to content

Commit

Permalink
Merge pull request #9 from dynamiatools/7.x
Browse files Browse the repository at this point in the history
7.3.0 upgrade to AWS SDK 2.28
  • Loading branch information
marioserrano09 authored Sep 30, 2024
2 parents ea86fbc + a373520 commit 2cd56b4
Show file tree
Hide file tree
Showing 24 changed files with 560 additions and 249 deletions.
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.1.2</version>
<version>7.2.0</version>
</parent>
<name>Dynamia Modules - EntityFiles - Core</name>
<artifactId>tools.dynamia.modules.entityfiles</artifactId>
<version>7.1.2</version>
<version>7.2.0</version>
<url>https://www.dynamia.tools/modules/entityfiles</url>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public void setContentType(String contentType) {
this.contentType = contentType;
}

public boolean hasInputStream() {
return inputStream != null;
}

public InputStream getInputStream() {
if (inputStream == null) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tools.dynamia.modules.entityfile.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import tools.dynamia.integration.sterotypes.Controller;
import tools.dynamia.modules.entityfile.local.LocalEntityFileStorageHandler;

@Controller
public class LocalEntityFileStorageController {

private final LocalEntityFileStorageHandler handler;

public LocalEntityFileStorageController(LocalEntityFileStorageHandler handler) {
this.handler = handler;
}

@GetMapping(value = "/storage/{file}")
public ResponseEntity<Resource> get(@PathVariable String file, @RequestParam("uuid") String uuid, HttpServletRequest request) {
var resource = handler.getResource(file, uuid, request);
if (resource != null && resource.exists() && resource.isReadable()) {
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;

import java.util.List;

@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package tools.dynamia.modules.entityfile.local;

import org.springframework.core.env.Environment;
import tools.dynamia.commons.logger.LoggingService;
import tools.dynamia.commons.logger.SLF4JLoggingService;
import tools.dynamia.domain.ValidationError;
import tools.dynamia.domain.query.Parameters;
import tools.dynamia.domain.services.CrudService;
Expand All @@ -37,6 +39,8 @@
@Service
public class LocalEntityFileStorage implements EntityFileStorage {

private final LoggingService logger = new SLF4JLoggingService(LocalEntityFileStorage.class, "Local: ");

public static final String ID = "LocalStorage";
private static final String LOCAL_FILES_LOCATION = "LOCAL_FILES_LOCATION";
private static final String LOCAL_USE_HTTPS = "LOCAL_USE_HTTPS";
Expand Down Expand Up @@ -71,9 +75,12 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) {
File realFile = getRealFile(entityFile);

try {

IOUtils.copy(fileInfo.getInputStream(), realFile);
entityFile.setSize(realFile.length());
logger.info("Uploaded to server: " + realFile);
} catch (IOException e) {
logger.error("Error upload local file " + realFile, e);
throw new EntityFileException("Error upload local file " + realFile, e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,16 @@

package tools.dynamia.modules.entityfile.local;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import tools.dynamia.modules.entityfile.service.EntityFileService;

@Configuration
class LocalEntityFileStorageConfig {

@Bean
public SimpleUrlHandlerMapping localHandler() {

ResourceHttpRequestHandler handler = localEntityFileStorageHandler();

Map<String, Object> map = new HashMap<>();

map.put("storage/**", handler);

SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(map);

return mapping;
}

@Bean
public LocalEntityFileStorageHandler localEntityFileStorageHandler() {
return new LocalEntityFileStorageHandler();
}
@Bean
public LocalEntityFileStorageHandler localEntityFileStorageHandler(LocalEntityFileStorage storage, EntityFileService service) {
return new LocalEntityFileStorageHandler(storage, service);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import tools.dynamia.commons.StringUtils;
import tools.dynamia.integration.Containers;
Expand All @@ -36,21 +35,21 @@
import tools.dynamia.modules.entityfile.enums.EntityFileType;
import tools.dynamia.modules.entityfile.service.EntityFileService;

public class LocalEntityFileStorageHandler extends ResourceHttpRequestHandler {
public class LocalEntityFileStorageHandler {

private static final String UUID = "/uuid/";
private LocalEntityFileStorage storage;
private EntityFileService service;
private final LocalEntityFileStorage storage;
private final EntityFileService service;
private EntityFileAccountProvider accountProvider;

@Override
protected Resource getResource(HttpServletRequest request) {
if (service == null) {
service = Containers.get().findObject(EntityFileService.class);
}
if (storage == null) {
storage = Containers.get().findObject(LocalEntityFileStorage.class);
}
public LocalEntityFileStorageHandler(LocalEntityFileStorage storage, EntityFileService service) {
this.storage = storage;
this.service = service;
}


public Resource getResource(String fileName, String uuid, HttpServletRequest request) {


if (accountProvider == null) {
accountProvider = Containers.get().findObject(EntityFileAccountProvider.class);
Expand All @@ -60,23 +59,11 @@ protected Resource getResource(HttpServletRequest request) {
}

File file = null;
String uuid = getParam(request, "uuid", null);

if (uuid == null) {
String path = request.getPathInfo();
if (path.contains(UUID)) {
uuid = path.substring(path.lastIndexOf(UUID) + UUID.length());
uuid = StringUtils.removeFilenameExtension(uuid);
}
}

if (uuid == null) {
return null;
}

Long currentAccountId = accountProvider.getAccountId();
EntityFile entityFile = service.getEntityFile(uuid);



if (entityFile != null && (currentAccountId == null || currentAccountId.equals(0L) || entityFile.isShared() || entityFile.getAccountId().equals(currentAccountId))) {

StoredEntityFile storedEntityFile = storage.download(entityFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package tools.dynamia.modules.entityfile.service.impl;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -101,7 +104,7 @@ private EntityFile createDir(EntityFile parent, Object targetEntity, String name
@Override
@Transactional
public EntityFile createEntityFile(UploadedFileInfo fileInfo, Object target, String description) {
logger.info("Creating new entity file for " + target + ", file: " + fileInfo.getFullName());
logger.info("Creating new entity file for " + (target != null ? target : "temporal entity") + ", file: " + fileInfo.getFullName());
EntityFile entityFile = new EntityFile();
entityFile.setDescription(description);
entityFile.setContentType(fileInfo.getContentType());
Expand Down Expand Up @@ -294,8 +297,11 @@ public void download(EntityFile entityFile, File outputFile) {
@Override
public EntityFile getEntityFile(String uuid) {
try {
return crudService.findSingle(EntityFile.class, QueryParameters.with("uuid", QueryConditions.eq(uuid))
.add("accountId", QueryConditions.isNotNull()));
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<EntityFile> query = cb.createQuery(EntityFile.class);
Root<EntityFile> root = query.from(EntityFile.class);
query.select(root).where(cb.equal(root.get("uuid"), uuid));
return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
} catch (Exception e) {
logger.error("Error loading entity file with uuid: " + uuid + ". " + e.getMessage(), e);

Expand Down
16 changes: 8 additions & 8 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.1.2</version>
<version>7.2.0</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.0</dynamiatools.version>
<dynamiatools.version>5.2.1</dynamiatools.version>
<springboot.version>3.3.3</springboot.version>
<aws.version>1.12.771</aws.version>
<aws.version>2.28.11</aws.version>
<java.version>17</java.version>
<maven.compiler>3.13.0</maven.compiler>
<source.encoding>UTF-8</source.encoding>
Expand All @@ -88,7 +88,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<version>1.7.0</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -99,7 +99,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<version>3.10.0</version>
<configuration>
<failOnError>false</failOnError>
<doclint>none</doclint>
Expand All @@ -108,7 +108,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
Expand All @@ -122,7 +122,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
<version>2.16.1</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.0.1</version>
<version>3.1.0</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
8 changes: 4 additions & 4 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.1.2</version>
<version>7.2.0</version>
</parent>

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

<build>
Expand All @@ -53,8 +53,8 @@
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>${aws.version}</version>
</dependency>

Expand Down
Loading

0 comments on commit 2cd56b4

Please sign in to comment.