Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit ccf86aa

Browse files
authored
Merge pull request #60 from noobdevsam/26-optimize_docker_container
merge from branch 26 - 5
2 parents f252baa + bfbccc5 commit ccf86aa

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

compose.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ services:
2323
ports:
2424
- "8080:8080"
2525
mem_limit: 2g
26+
user: "0:0" # Run as root user initially
2627
environment:
2728
# Connect to the MySQL container
2829
- DATABASE_URL=jdbc:mysql://db-mysql:3306/stego
@@ -33,11 +34,15 @@ services:
3334
# Other environment variables as needed
3435
- JAVA_TOOL_OPTIONS=-XX:MaxRAMPercentage=75.0
3536
volumes:
36-
# Mount the local './storage' directory into the container.
37-
# This makes it easy to see generated files on your host machine.
38-
- ./storage:/app/storage
37+
- stego_storage:/app/storage
3938
command: >
40-
sh -c "mkdir -p /app/storage && chown -R cnb:cnb /app/storage && java -jar /workspace/BOOT-INF/lib/spring-project-steganography-tool-v1.jar"
39+
sh -c "
40+
mkdir -p /app/storage &&
41+
chown -R 1000:1000 /app/storage &&
42+
chmod -R 755 /app/storage &&
43+
cd /workspace &&
44+
exec setpriv --reuid=1000 --regid=1000 --clear-groups java org.springframework.boot.loader.launch.JarLauncher
45+
"
4146
networks:
4247
- stego-network
4348
depends_on:
@@ -50,3 +55,5 @@ networks:
5055
volumes:
5156
mysql_data:
5257
driver: local
58+
stego_storage:
59+
driver: local

src/main/java/com/example/springprojectsteganographytool/services/impl/StorageServiceImpl.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.example.springprojectsteganographytool.exceptions.data.StorageFileNotFoundException;
55
import com.example.springprojectsteganographytool.exceptions.data.StorageSecurityException;
66
import com.example.springprojectsteganographytool.services.StorageService;
7+
import jakarta.annotation.PostConstruct;
78
import lombok.extern.slf4j.Slf4j;
89
import org.springframework.beans.factory.annotation.Value;
910
import org.springframework.core.io.Resource;
@@ -37,8 +38,29 @@ public StorageServiceImpl(@Value("${app.storage.base-path}") Path basePath) thro
3738
throw new IllegalArgumentException("app.storage.base-path must not be null");
3839
}
3940

40-
log.info("Using storage base path: {}", basePath.toAbsolutePath().normalize());
4141
this.basePath = basePath.toAbsolutePath().normalize();
42+
log.info("Storage base path configured: {}", this.basePath);
43+
}
44+
45+
/**
46+
* Post-construct initialization to ensure the storage directory exists.
47+
* This method is called after dependency injection is complete and BEFORE lazy initialization.
48+
*/
49+
@PostConstruct
50+
public void init() {
51+
try {
52+
Files.createDirectories(this.basePath);
53+
log.info("Storage directory initialized successfully: {}", this.basePath);
54+
55+
// Verify write permissions
56+
if (!Files.isWritable(this.basePath)) {
57+
log.error("Storage directory is not writable: {}", this.basePath);
58+
throw new IllegalStateException("Storage directory is not writable: " + this.basePath);
59+
}
60+
} catch (IOException e) {
61+
log.error("Failed to create storage directory: {}", this.basePath, e);
62+
throw new IllegalStateException("Cannot create storage directory: " + this.basePath, e);
63+
}
4264
}
4365

4466
/**
@@ -56,7 +78,12 @@ public Path save(String relativeFileName, byte[] content) throws StorageSecurity
5678

5779
try {
5880
var targetPath = safeResolve(relativeFileName);
59-
Files.createDirectories(targetPath.getParent());
81+
82+
// Ensure parent directory exists (though basePath should already exist from init())
83+
if (targetPath.getParent() != null && !Files.exists(targetPath.getParent())) {
84+
Files.createDirectories(targetPath.getParent());
85+
}
86+
6087
Files.write(targetPath, content, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
6188
log.debug("Saved file: {} ({} bytes)", targetPath, content.length);
6289
return targetPath;

0 commit comments

Comments
 (0)