Je rencontre une erreur 500 lors du traitement des images et des vidéos dans mon projet. Ce n'est pas une erreur de protection, car les autres formulaires fonctionnent correctement. Cependant, il semble y avoir un problème spécifique à la gestion des médias.
Afin de faciliter la maintenance du projet à long terme, j'ai choisi une structure simple. Mon projet utilise Angular pour le frontend et Spring Boot pour le backend. Les fichiers multimédias (images et vidéos) sont tous stockés dans le dossier public d'Angular, et Spring Boot les charge directement à partir de ce dossier.
@Service
public class ImageService {
private final String uploadDir = "../public/images/"; // Chemin vers le répertoire public d'Angular
private final String uploadDirVideos = "../public/videos/";
public String uploadImage(MultipartFile file) throws IOException {
if (file.isEmpty()) {
throw new IllegalStateException("Cannot upload empty file.");
}
// Vérifie si le répertoire existe, sinon il le crée
Path uploadPath = Paths.get(uploadDir);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath); // Crée le répertoire s'il n'existe pas
}
String originalFileName = file.getOriginalFilename();
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
String newFileName = UUID.randomUUID().toString() + fileExtension;
Path filePath = uploadPath.resolve(newFileName);
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return newFileName; // Retourne le nouveau nom du fichier pour l'enregistrer dans l'entité Category
}
public String uploadVideo(MultipartFile file) throws IOException {
if (file.isEmpty()) {
throw new IllegalStateException("Cannot upload empty file.");
}
// Vérifie si le répertoire existe, sinon il le crée
Path uploadPath = Paths.get(uploadDirVideos);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath); // Crée le répertoire s'il n'existe pas
}
String originalFileName = file.getOriginalFilename();
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
String newFileName = UUID.randomUUID().toString() + fileExtension;
Path filePath = uploadPath.resolve(newFileName);
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return newFileName; // Retourne le nouveau nom du fichier pour l'enregistrer
}
public byte getImage(String fileName) throws IOException {
Path filePath = Paths.get(uploadDir + fileName);
return Files.readAllBytes(filePath);
}
public byte getVideo(String fileName) throws IOException {
Path filePath = Paths.get(uploadDirVideos + fileName);
return Files.readAllBytes(filePath);
}
}
nginx
server {
listen 80;
server_name cognitiex.com www.cognitiex.com;
# Rediriger toutes les requêtes HTTP vers HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl; # Écouter sur le port 443 pour SSL
server_name cognitiex.com; # Domaine sans www
ssl_certificate /etc/letsencrypt/live/cognitiex.com/fullchain.pem; # Chemin vers le certificat
ssl_certificate_key /etc/letsencrypt/live/cognitiex.com/privkey.pem; # Chemin vers la clé privée
return 301 https://www.cognitiex.com$request_uri; # Redirection vers www.cognitiex.com
}
server {
listen 443 ssl; # Écouter sur le port 443 pour SSL
server_name www.cognitiex.com; # Domaine avec www
ssl_certificate /etc/letsencrypt/live/cognitiex.com/fullchain.pem; # Chemin vers le certificat
ssl_certificate_key /etc/letsencrypt/live/cognitiex.com/privkey.pem; # Chemin vers la clé privée
root /var/www/ecomme/dist/accounts/browser; # Chemin vers les fichiers
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html; # Sert les fichiers Angular
}
location /api/ {
proxy_pass http://localhost:8080/api/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
add_header 'Access-Control-Allow-Origin' 'https://www.cognitiex.com' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
}
location /api/videos/ {
alias /var/www/ecomme/public/videos/; # Notez la barre oblique
}
location /api/images/ {
alias /var/www/ecomme/public/images/; # Notez la barre oblique
}
error_page 403 /403.html; # Gestion des erreurs
location = /403.html {
internal;
}
}