From 5ee1f5c10fa3d3ecc205cdad0e7e852169eed0f4 Mon Sep 17 00:00:00 2001 From: MDeLuise <66636702+MDeLuise@users.noreply.github.com> Date: Tue, 8 Aug 2023 11:13:03 +0200 Subject: [PATCH] refactor!: abstract uploaded image location - Update README frontend properties --- README.md | 4 + backend/pom.xml | 37 +++++++++ .../FileSystemImageStorageService.java | 13 ++-- .../image/storage/StorageProperties.java | 18 ----- .../main/resources/application-dev.properties | 1 + .../application-integration.properties | 1 + .../src/main/resources/application.properties | 1 + .../dblogs/changelog/changes/changelog-0.xml | 77 ++++++++++--------- frontend/public/index.html | 2 +- frontend/src/App.tsx | 2 +- 10 files changed, 91 insertions(+), 65 deletions(-) delete mode 100644 backend/src/main/java/com/github/mdeluise/plantit/image/storage/StorageProperties.java diff --git a/README.md b/README.md index dd4958f86..8ed71b7ce 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,10 @@ There are 2 configuration file available: MYSQL_ROOT_PASSWORD=root MYSQL_DATABASE=bootdb USERS_LIMIT=-1 # including the admin account, so <= 0 if undefined, >= 2 if defined + CACHE_TTL=86400 + CACHE_HOST=cache + CACHE_PORT=6379 + TRAFLE_KEY= # put you key here, otherwise the "search" feature will include only user generated species ``` Change the properties values according to your system. diff --git a/backend/pom.xml b/backend/pom.xml index 1d24d4baa..f2737c9ee 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -140,6 +140,42 @@ + + + + copy-files + + + copyFiles + + + + + + org.apache.maven.plugins + maven-antrun-plugin + 1.8 + + + test + + run + + + + Copying files... + + + + + + + + + + + @@ -157,6 +193,7 @@ + validate check diff --git a/backend/src/main/java/com/github/mdeluise/plantit/image/storage/FileSystemImageStorageService.java b/backend/src/main/java/com/github/mdeluise/plantit/image/storage/FileSystemImageStorageService.java index c82ae6cbd..e389c7ef1 100644 --- a/backend/src/main/java/com/github/mdeluise/plantit/image/storage/FileSystemImageStorageService.java +++ b/backend/src/main/java/com/github/mdeluise/plantit/image/storage/FileSystemImageStorageService.java @@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.Cacheable; -import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; import org.springframework.util.FileSystemUtils; import org.springframework.web.multipart.MultipartFile; @@ -39,11 +38,11 @@ public class FileSystemImageStorageService implements ImageStorageService { @Autowired - public FileSystemImageStorageService(StorageProperties properties, ImageRepository imageRepository, - PlantImageRepository plantImageRepository, + public FileSystemImageStorageService(@Value("${upload.location}") String rootLocation, + ImageRepository imageRepository, PlantImageRepository plantImageRepository, @Value("${image.max_origin_size}") int maxOriginImgSize, AuthenticatedUserService authenticatedUserService) { - this.rootLocation = properties.getLocation(); + this.rootLocation = rootLocation; this.imageRepository = imageRepository; this.plantImageRepository = plantImageRepository; this.maxOriginImgSize = maxOriginImgSize; @@ -57,10 +56,8 @@ public EntityImage save(MultipartFile file, ImageTarget linkedEntity) { throw new StorageException("Failed to save empty file."); } String fileExtension; - String uploadDir; try { fileExtension = file.getContentType().split("/")[1]; - uploadDir = getClass().getClassLoader().getResource(rootLocation).getPath(); } catch (NullPointerException e) { throw new StorageException("Could not retrieve file information", e); } @@ -80,7 +77,7 @@ public EntityImage save(MultipartFile file, ImageTarget linkedEntity) { } else { throw new UnsupportedOperationException("Could not find suitable class for linkedEntity"); } - final String fileName = String.format("%s/%s.%s", uploadDir, entityImage.getId(), fileExtension); + final String fileName = String.format("%s/%s.%s", rootLocation, entityImage.getId(), fileExtension); final Path pathToFile = Path.of(fileName); Files.copy(fileInputStream, pathToFile); entityImage.setPath(String.format("%s/%s.%s", rootLocation, entityImage.getId(), fileExtension)); @@ -106,7 +103,7 @@ public EntityImage get(String id) { @Override public byte[] getContent(String id) { try { - final File entityImageFile = new ClassPathResource(get(id).getPath()).getFile(); + final File entityImageFile = new File(get(id).getPath()); if (!entityImageFile.exists() || !entityImageFile.canRead()) { throw new StorageFileNotFoundException("Could not read image with id: " + id); } diff --git a/backend/src/main/java/com/github/mdeluise/plantit/image/storage/StorageProperties.java b/backend/src/main/java/com/github/mdeluise/plantit/image/storage/StorageProperties.java deleted file mode 100644 index 6b10a2ce1..000000000 --- a/backend/src/main/java/com/github/mdeluise/plantit/image/storage/StorageProperties.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.github.mdeluise.plantit.image.storage; - -import org.springframework.stereotype.Component; - -@Component -public class StorageProperties { - private String location = "upload-dir"; - - - public String getLocation() { - return location; - } - - - public void setLocation(String location) { - this.location = location; - } -} diff --git a/backend/src/main/resources/application-dev.properties b/backend/src/main/resources/application-dev.properties index ab8d5788e..07d5ae160 100644 --- a/backend/src/main/resources/application-dev.properties +++ b/backend/src/main/resources/application-dev.properties @@ -86,6 +86,7 @@ app.version = @project.version@ # users.max = ${USERS_LIMIT:-1} trefle.key = ${TRAFLE_KEY:} +upload.location = ${UPLOAD_DIR:/tmp} # diff --git a/backend/src/main/resources/application-integration.properties b/backend/src/main/resources/application-integration.properties index 43435408f..6208e6b29 100644 --- a/backend/src/main/resources/application-integration.properties +++ b/backend/src/main/resources/application-integration.properties @@ -65,6 +65,7 @@ app.version = @project.version@ # users.max = -1 trefle.key = +upload.location = /tmp # diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 9360daf51..0249f625c 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -67,6 +67,7 @@ app.version = @project.version@ # users.max = ${USERS_LIMIT:-1} trefle.key = ${TRAFLE_KEY:} +upload.location = ${UPLOAD_DIR:/tmp} # diff --git a/backend/src/main/resources/dblogs/changelog/changes/changelog-0.xml b/backend/src/main/resources/dblogs/changelog/changes/changelog-0.xml index 6163cf540..82501825b 100644 --- a/backend/src/main/resources/dblogs/changelog/changes/changelog-0.xml +++ b/backend/src/main/resources/dblogs/changelog/changes/changelog-0.xml @@ -24,6 +24,7 @@ + @@ -55,37 +56,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -109,17 +79,18 @@ + @@ -142,7 +113,7 @@ - + @@ -157,12 +128,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -177,8 +181,7 @@ - + diff --git a/frontend/public/index.html b/frontend/public/index.html index 45945c4ff..de686b910 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -10,7 +10,7 @@ - +