Skip to content

Commit a373520

Browse files
upgrade to AWS SDK v2.28
1 parent dd87835 commit a373520

File tree

12 files changed

+136
-115
lines changed

12 files changed

+136
-115
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tools.dynamia.modules.entityfile.controller;
2+
3+
import jakarta.servlet.http.HttpServletRequest;
4+
import org.springframework.core.io.Resource;
5+
import org.springframework.http.HttpHeaders;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import tools.dynamia.integration.sterotypes.Controller;
12+
import tools.dynamia.modules.entityfile.local.LocalEntityFileStorageHandler;
13+
14+
@Controller
15+
public class LocalEntityFileStorageController {
16+
17+
private final LocalEntityFileStorageHandler handler;
18+
19+
public LocalEntityFileStorageController(LocalEntityFileStorageHandler handler) {
20+
this.handler = handler;
21+
}
22+
23+
@GetMapping(value = "/storage/{file}")
24+
public ResponseEntity<Resource> get(@PathVariable String file, @RequestParam("uuid") String uuid, HttpServletRequest request) {
25+
var resource = handler.getResource(file, uuid, request);
26+
if (resource != null && resource.exists() && resource.isReadable()) {
27+
return ResponseEntity.ok()
28+
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
29+
.body(resource);
30+
} else {
31+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
32+
}
33+
}
34+
}

sources/core/src/main/java/tools/dynamia/modules/entityfile/domain/EntityFile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import jakarta.persistence.*;
3434
import jakarta.validation.constraints.NotNull;
35+
3536
import java.util.List;
3637

3738
@Entity

sources/core/src/main/java/tools/dynamia/modules/entityfile/local/LocalEntityFileStorage.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package tools.dynamia.modules.entityfile.local;
1919

2020
import org.springframework.core.env.Environment;
21+
import tools.dynamia.commons.logger.LoggingService;
22+
import tools.dynamia.commons.logger.SLF4JLoggingService;
2123
import tools.dynamia.domain.ValidationError;
2224
import tools.dynamia.domain.query.Parameters;
2325
import tools.dynamia.domain.services.CrudService;
@@ -37,6 +39,8 @@
3739
@Service
3840
public class LocalEntityFileStorage implements EntityFileStorage {
3941

42+
private final LoggingService logger = new SLF4JLoggingService(LocalEntityFileStorage.class, "Local: ");
43+
4044
public static final String ID = "LocalStorage";
4145
private static final String LOCAL_FILES_LOCATION = "LOCAL_FILES_LOCATION";
4246
private static final String LOCAL_USE_HTTPS = "LOCAL_USE_HTTPS";
@@ -71,9 +75,12 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) {
7175
File realFile = getRealFile(entityFile);
7276

7377
try {
78+
7479
IOUtils.copy(fileInfo.getInputStream(), realFile);
7580
entityFile.setSize(realFile.length());
81+
logger.info("Uploaded to server: " + realFile);
7682
} catch (IOException e) {
83+
logger.error("Error upload local file " + realFile, e);
7784
throw new EntityFileException("Error upload local file " + realFile, e);
7885
}
7986

sources/core/src/main/java/tools/dynamia/modules/entityfile/local/LocalEntityFileStorageConfig.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,16 @@
1717

1818
package tools.dynamia.modules.entityfile.local;
1919

20-
import java.util.HashMap;
21-
import java.util.Map;
22-
2320
import org.springframework.context.annotation.Bean;
2421
import org.springframework.context.annotation.Configuration;
25-
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
26-
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
22+
import tools.dynamia.modules.entityfile.service.EntityFileService;
2723

2824
@Configuration
2925
class LocalEntityFileStorageConfig {
3026

31-
@Bean
32-
public SimpleUrlHandlerMapping localHandler() {
33-
34-
ResourceHttpRequestHandler handler = localEntityFileStorageHandler();
35-
36-
Map<String, Object> map = new HashMap<>();
37-
38-
map.put("storage/**", handler);
39-
40-
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
41-
mapping.setUrlMap(map);
42-
43-
return mapping;
44-
}
4527

46-
@Bean
47-
public LocalEntityFileStorageHandler localEntityFileStorageHandler() {
48-
return new LocalEntityFileStorageHandler();
49-
}
28+
@Bean
29+
public LocalEntityFileStorageHandler localEntityFileStorageHandler(LocalEntityFileStorage storage, EntityFileService service) {
30+
return new LocalEntityFileStorageHandler(storage, service);
31+
}
5032
}

sources/core/src/main/java/tools/dynamia/modules/entityfile/local/LocalEntityFileStorageHandler.java

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.springframework.core.io.FileSystemResource;
2525
import org.springframework.core.io.Resource;
26-
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
2726

2827
import tools.dynamia.commons.StringUtils;
2928
import tools.dynamia.integration.Containers;
@@ -36,21 +35,21 @@
3635
import tools.dynamia.modules.entityfile.enums.EntityFileType;
3736
import tools.dynamia.modules.entityfile.service.EntityFileService;
3837

39-
public class LocalEntityFileStorageHandler extends ResourceHttpRequestHandler {
38+
public class LocalEntityFileStorageHandler {
4039

4140
private static final String UUID = "/uuid/";
42-
private LocalEntityFileStorage storage;
43-
private EntityFileService service;
41+
private final LocalEntityFileStorage storage;
42+
private final EntityFileService service;
4443
private EntityFileAccountProvider accountProvider;
4544

46-
@Override
47-
protected Resource getResource(HttpServletRequest request) {
48-
if (service == null) {
49-
service = Containers.get().findObject(EntityFileService.class);
50-
}
51-
if (storage == null) {
52-
storage = Containers.get().findObject(LocalEntityFileStorage.class);
53-
}
45+
public LocalEntityFileStorageHandler(LocalEntityFileStorage storage, EntityFileService service) {
46+
this.storage = storage;
47+
this.service = service;
48+
}
49+
50+
51+
public Resource getResource(String fileName, String uuid, HttpServletRequest request) {
52+
5453

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

6261
File file = null;
63-
String uuid = getParam(request, "uuid", null);
64-
65-
if (uuid == null) {
66-
String path = request.getPathInfo();
67-
if (path.contains(UUID)) {
68-
uuid = path.substring(path.lastIndexOf(UUID) + UUID.length());
69-
uuid = StringUtils.removeFilenameExtension(uuid);
70-
}
71-
}
72-
73-
if (uuid == null) {
74-
return null;
75-
}
76-
7762
Long currentAccountId = accountProvider.getAccountId();
7863
EntityFile entityFile = service.getEntityFile(uuid);
7964

65+
66+
8067
if (entityFile != null && (currentAccountId == null || currentAccountId.equals(0L) || entityFile.isShared() || entityFile.getAccountId().equals(currentAccountId))) {
8168

8269
StoredEntityFile storedEntityFile = storage.download(entityFile);

sources/core/src/main/java/tools/dynamia/modules/entityfile/service/impl/EntityFileServiceImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

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

21+
import jakarta.persistence.criteria.CriteriaBuilder;
22+
import jakarta.persistence.criteria.CriteriaQuery;
23+
import jakarta.persistence.criteria.Root;
2124
import org.springframework.beans.factory.annotation.Autowired;
2225
import org.springframework.stereotype.Service;
2326
import org.springframework.transaction.annotation.Transactional;
@@ -101,7 +104,7 @@ private EntityFile createDir(EntityFile parent, Object targetEntity, String name
101104
@Override
102105
@Transactional
103106
public EntityFile createEntityFile(UploadedFileInfo fileInfo, Object target, String description) {
104-
logger.info("Creating new entity file for " + target + ", file: " + fileInfo.getFullName());
107+
logger.info("Creating new entity file for " + (target != null ? target : "temporal entity") + ", file: " + fileInfo.getFullName());
105108
EntityFile entityFile = new EntityFile();
106109
entityFile.setDescription(description);
107110
entityFile.setContentType(fileInfo.getContentType());
@@ -294,8 +297,11 @@ public void download(EntityFile entityFile, File outputFile) {
294297
@Override
295298
public EntityFile getEntityFile(String uuid) {
296299
try {
297-
return crudService.findSingle(EntityFile.class, QueryParameters.with("uuid", QueryConditions.eq(uuid))
298-
.add("accountId", QueryConditions.isNotNull()));
300+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
301+
CriteriaQuery<EntityFile> query = cb.createQuery(EntityFile.class);
302+
Root<EntityFile> root = query.from(EntityFile.class);
303+
query.select(root).where(cb.equal(root.get("uuid"), uuid));
304+
return entityManager.createQuery(query).setMaxResults(1).getSingleResult();
299305
} catch (Exception e) {
300306
logger.error("Error loading entity file with uuid: " + uuid + ". " + e.getMessage(), e);
301307

sources/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@
6363

6464
<properties>
6565
<file.encoding>UTF-8</file.encoding>
66-
<dynamiatools.version>5.3.0</dynamiatools.version>
66+
<dynamiatools.version>5.2.1</dynamiatools.version>
6767
<springboot.version>3.3.3</springboot.version>
68-
<aws.version>2.28.0</aws.version>
68+
<aws.version>2.28.11</aws.version>
6969
<java.version>17</java.version>
7070
<maven.compiler>3.13.0</maven.compiler>
7171
<source.encoding>UTF-8</source.encoding>

sources/s3/src/main/java/tools/dynamia/modules/entityfiles/s3/S3EntityFileStorage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class S3EntityFileStorage implements EntityFileStorage {
6565
public static final String AWS_S3_REGION = "AWS_S3_REGION";
6666
public static final String AWS_S3_BUCKET = "AWS_S3_BUCKET";
6767
private static final Logger log = LoggerFactory.getLogger(S3EntityFileStorage.class);
68-
private final LoggingService logger = new SLF4JLoggingService(S3EntityFileStorage.class, "S3");
68+
private final LoggingService logger = new SLF4JLoggingService(S3EntityFileStorage.class, "S3: ");
6969

7070
private final SimpleCache<String, String> URL_CACHE = new SimpleCache<>();
7171
private final SimpleCache<String, String> PARAMS_CACHE = new SimpleCache<>();
@@ -126,10 +126,10 @@ public void upload(EntityFile entityFile, UploadedFileInfo fileInfo) {
126126

127127

128128
final var metadata = Map.of(
129-
"accountId", entityFile.getAccountId().toString(),
129+
"accountId", entityFile.getAccountId() != null ? entityFile.getAccountId().toString() : "",
130130
"uuid", entityFile.getUuid(),
131-
"creator", entityFile.getCreator(),
132-
"databaseId", String.valueOf(entityFile.getId())
131+
"creator", entityFile.getCreator() != null ? entityFile.getCreator() : "anonymous",
132+
"databaseId", entityFile.getId() != null ? String.valueOf(entityFile.getId()) : ""
133133
);
134134

135135
final var contentType = URLConnection.guessContentTypeFromName(entityFile.getName());
@@ -198,8 +198,8 @@ protected String generateSignedURL(String bucketName, String fileName) {
198198

199199

200200
PresignedGetObjectRequest presignedRequest = S3Utils.generatePresignedObjetRequest(bucketName, fileName, Duration.ofMinutes(30));
201-
logger.info("Presigned URL: [{}]", presignedRequest.url().toString());
202-
logger.info("HTTP method: [{}]", presignedRequest.httpRequest().method());
201+
logger.info("Presigned URL: " + presignedRequest.url().toString());
202+
logger.info("HTTP method: " + presignedRequest.httpRequest().method());
203203

204204
return presignedRequest.url().toExternalForm();
205205

sources/ui/src/main/java/tools/dynamia/modules/entityfile/ui/EntityFilesInstaller.java renamed to sources/ui/src/main/java/tools/dynamia/modules/entityfile/ui/EntityFilesModuleProvider.java

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
1-
2-
/*
3-
* Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1
4-
* Colombia / South America
5-
*
6-
* Licensed under the Apache License, Version 2.0 (the "License");
7-
* you may not use this file except in compliance with the License.
8-
* You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing, software
13-
* distributed under the License is distributed on an "AS IS" BASIS,
14-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* See the License for the specific language governing permissions and
16-
* limitations under the License.
17-
*/
18-
19-
package tools.dynamia.modules.entityfile.ui;
20-
21-
import tools.dynamia.crud.cfg.ConfigPage;
22-
import tools.dynamia.integration.sterotypes.Provider;
23-
import tools.dynamia.navigation.Module;
24-
import tools.dynamia.navigation.ModuleProvider;
25-
import tools.dynamia.navigation.PageGroup;
26-
27-
28-
/**
29-
*
30-
* @author Mario Serrano Leones
31-
*/
32-
@Provider
33-
public class EntityFilesInstaller implements ModuleProvider {
34-
35-
@Override
36-
public Module getModule() {
37-
Module module = Module.getRef("system");
38-
39-
PageGroup pg = new PageGroup("config", "Configuracion");
40-
module.addPageGroup(pg);
41-
{
42-
pg.addPage(new ConfigPage("entityFile", "Archivos", "EntityFileCFG"));
43-
44-
}
45-
46-
return module;
47-
}
48-
49-
}
1+
2+
/*
3+
* Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1
4+
* Colombia / South America
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package tools.dynamia.modules.entityfile.ui;
20+
21+
import tools.dynamia.crud.cfg.ConfigPage;
22+
import tools.dynamia.integration.sterotypes.Provider;
23+
import tools.dynamia.navigation.Module;
24+
import tools.dynamia.navigation.ModuleProvider;
25+
import tools.dynamia.navigation.PageGroup;
26+
27+
28+
/**
29+
* @author Mario Serrano Leones
30+
*/
31+
@Provider
32+
public class EntityFilesModuleProvider implements ModuleProvider {
33+
34+
@Override
35+
public Module getModule() {
36+
Module module = Module.getRef("system")
37+
.name("System")
38+
.icon("settings");
39+
40+
PageGroup pg = new PageGroup("config", "Configuration");
41+
module.addPageGroup(pg);
42+
{
43+
pg.addPage(new ConfigPage("entityFile", "Entity Files", "EntityFileCFG"));
44+
45+
}
46+
47+
return module;
48+
}
49+
50+
}

sources/ui/src/main/java/tools/dynamia/modules/entityfile/ui/actions/ViewFileURLAction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import tools.dynamia.actions.ActionGroup;
2222
import tools.dynamia.actions.InstallAction;
2323
import tools.dynamia.actions.ReadableOnly;
24+
import tools.dynamia.ui.UIMessages;
2425

2526
@InstallAction
2627
public class ViewFileURLAction extends AbstractEntityFileAction implements ReadableOnly {
@@ -35,7 +36,8 @@ public ViewFileURLAction() {
3536
@Override
3637
public void actionPerformed(EntityFileActionEvent evt) {
3738
if (evt.getEntityFile() != null) {
38-
Messagebox.show(evt.getEntityFile().getStoredEntityFile().getUrl());
39+
String url = evt.getEntityFile().getStoredEntityFile().getUrl();
40+
UIMessages.showMessageDialog("<pre>" + url + "</pre>");
3941
}
4042
}
4143
}

0 commit comments

Comments
 (0)