Skip to content

Commit

Permalink
refactor!: abstract uploaded image location
Browse files Browse the repository at this point in the history
- Update README frontend properties
  • Loading branch information
MDeLuise committed Aug 8, 2023
1 parent f62635e commit 5ee1f5c
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 65 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
37 changes: 37 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,42 @@

</dependencies>


<profiles>
<profile>
<id>copy-files</id>
<activation>
<property>
<name>copyFiles</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Copying files...</echo>
<copy file="${project.basedir}/src/main/resources/upload-dir/dummy-image.jpg"
tofile="/tmp/dummy-image.jpg"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<build>
<plugins>
<plugin>
Expand All @@ -157,6 +193,7 @@
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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));
Expand All @@ -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);
}
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions backend/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ app.version = @project.version@
#
users.max = ${USERS_LIMIT:-1}
trefle.key = ${TRAFLE_KEY:}
upload.location = ${UPLOAD_DIR:/tmp}


#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ app.version = @project.version@
#
users.max = -1
trefle.key =
upload.location = /tmp


#
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ app.version = @project.version@
#
users.max = ${USERS_LIMIT:-1}
trefle.key = ${TRAFLE_KEY:}
upload.location = ${UPLOAD_DIR:/tmp}


#
Expand Down
77 changes: 40 additions & 37 deletions backend/src/main/resources/dblogs/changelog/changes/changelog-0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</createTable>
</changeSet>


<changeSet id="createApiKeyTable" author="MDeLuise">
<preConditions onFail="MARK_RAN">
<not>
Expand Down Expand Up @@ -55,37 +56,6 @@
</changeSet>


<changeSet id="createEntityImageTable" author="MDeLuise">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="entity_images"/>
</not>
</preConditions>
<createTable tableName="entity_images">
<column name="id" type="varchar(255)">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="image_type" type="tinyint">
<constraints nullable="false"/>
</column>
<column name="botanical_info_entity_id" type="bigint">
</column>
<column name="plant_entity_id" type="bigint">
</column>
<column name="description" type="varchar(100)">
</column>
<column name="saved_at" type="datetime">
<constraints nullable="false"/>
</column>
<column name="url" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="path" type="varchar(255)">
</column>
</createTable>
</changeSet>


<changeSet id="createBotanicalInfoTable" author="MDeLuise">
<preConditions onFail="MARK_RAN">
<not>
Expand All @@ -109,17 +79,18 @@
<constraints nullable="false"/>
</column>
<column name="user_id" type="bigint">
<constraints nullable="true" foreignKeyName="fk_botanicalInfo_userId" references="application_users(id)" />
</column>
</createTable>

<addUniqueConstraint
columnNames="species, user_id"
constraintName="botanical_name_species_unique"
constraintName="botanical_info_species_unique"
tableName="botanical_infos"
/>
<addUniqueConstraint
columnNames="scientific_name, user_id"
constraintName="botanical_name_scientific_name_unique"
constraintName="botanical_info_scientific_name_unique"
tableName="botanical_infos"
/>
</changeSet>
Expand All @@ -142,7 +113,7 @@
<column name="personal_name" type="varchar(30)">
<constraints nullable="false"/>
</column>
<column name="plant_state" type="varchar(20)">
<column name="plant_state" type="varchar(20)" defaultValue="PURCHASED">
<constraints nullable="false"/>
</column>
<column name="note" type="varchar(8500)">
Expand All @@ -157,12 +128,45 @@

<addUniqueConstraint
columnNames="owner_id, personal_name"
constraintName="plants_name_unique"
constraintName="plant_name_unique"
tableName="plants"
/>
</changeSet>


<changeSet id="createEntityImageTable" author="MDeLuise">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="entity_images"/>
</not>
</preConditions>
<createTable tableName="entity_images">
<column name="id" type="varchar(255)">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="image_type" type="tinyint(1)" defaultValue="1">
<constraints nullable="false"/>
</column>
<column name="botanical_info_entity_id" type="bigint">
<constraints nullable="true" foreignKeyName="fk_entityImage_botanicalInfoId" references="botanical_infos(id)"/>
</column>
<column name="plant_entity_id" type="bigint">
<constraints nullable="true" foreignKeyName="fk_entityImage_plantId" references="plants(id)"/>
</column>
<column name="description" type="varchar(100)">
</column>
<column name="saved_at" type="datetime">
<constraints nullable="false"/>
</column>
<column name="url" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="path" type="varchar(255)">
</column>
</createTable>
</changeSet>


<changeSet id="createDiaryTable" author="MDeLuise">
<preConditions onFail="MARK_RAN">
<not>
Expand All @@ -177,8 +181,7 @@
<constraints nullable="false" foreignKeyName="fk_diary_user" references="application_users(id)"/>
</column>
<column name="target_id" type="bigint">
<constraints nullable="false" foreignKeyName="fk_diary_trackedEntity"
references="plants(id)"/>
<constraints nullable="false" foreignKeyName="fk_diary_trackedEntity" references="plants(id)"/>
</column>
</createTable>
</changeSet>
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<link rel="icon" type="image/png" sizes="16x16" href="%PUBLIC_URL%/favicon-16x16.png">
<link rel="mask-icon" href="%PUBLIC_URL%/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<meta name="theme-color" content="#efefef">
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function App() {
const backendURL = window._env_.API_URL != null ? window._env_.API_URL : "http://localhost:8085/api";
const axiosReq = axios.create({
baseURL: backendURL,
//timeout: 5000
timeout: 5000
});

axiosReq.interceptors.request.use(
Expand Down

0 comments on commit 5ee1f5c

Please sign in to comment.