Refactor the upload files folders paths.
This commit is contained in:
@@ -1,20 +0,0 @@
|
|||||||
package org.codiki.core.constant;
|
|
||||||
|
|
||||||
public enum FileEnum {
|
|
||||||
/** Folder in where pictures will be uploaded. */
|
|
||||||
FOLDER_UPLOAD("/opt/codiki/pictures/tmp/"),
|
|
||||||
/** Folder in where profile pictures will be stored. */
|
|
||||||
FOLDER_PROFILE_IMAGES("/opt/codiki/pictures/profiles/"),
|
|
||||||
/** Folder in where images will be stored. */
|
|
||||||
FOLDER_IMAGE("/opt/codiki/pictures/posts/");
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
private FileEnum(final String pValue) {
|
|
||||||
value = pValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String val() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,7 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
import org.apache.commons.lang.RandomStringUtils;
|
import org.apache.commons.lang.RandomStringUtils;
|
||||||
import org.codiki.core.constant.FileEnum;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.UrlResource;
|
import org.springframework.core.io.UrlResource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -17,6 +17,13 @@ public class FileUploadService {
|
|||||||
/** Length of uploaded file names. */
|
/** Length of uploaded file names. */
|
||||||
private static final int DESTINATION_IMAGE_NAME_LENGTH = 30;
|
private static final int DESTINATION_IMAGE_NAME_LENGTH = 30;
|
||||||
|
|
||||||
|
@Value("codiki.files.upload")
|
||||||
|
private String folderUpload;
|
||||||
|
@Value("codiki.files.profile-images")
|
||||||
|
private String folderProfileImages;
|
||||||
|
@Value("codiki.files.images")
|
||||||
|
private String folderImages;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds the destination file name, which is a random string with 30 char
|
* Builds the destination file name, which is a random string with 30 char
|
||||||
* length.
|
* length.
|
||||||
@@ -28,18 +35,18 @@ public class FileUploadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String uploadProfileImage(final MultipartFile pFile) {
|
public String uploadProfileImage(final MultipartFile pFile) {
|
||||||
return uploadFile(pFile, FileEnum.FOLDER_PROFILE_IMAGES);
|
return uploadFile(pFile, folderProfileImages);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String uploadImage(final MultipartFile pFile) {
|
public String uploadImage(final MultipartFile pFile) {
|
||||||
return uploadFile(pFile, FileEnum.FOLDER_IMAGE);
|
return uploadFile(pFile, folderImages);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String uploadFile(final MultipartFile pFile, final FileEnum pPath) {
|
private String uploadFile(final MultipartFile pFile, final String pPath) {
|
||||||
String result;
|
String result;
|
||||||
try {
|
try {
|
||||||
result = buildDestinationFileName();
|
result = buildDestinationFileName();
|
||||||
Files.copy(pFile.getInputStream(), Paths.get(pPath.val()).resolve(result));
|
Files.copy(pFile.getInputStream(), Paths.get(pPath).resolve(result));
|
||||||
return result;
|
return result;
|
||||||
} catch (final Exception pEx) {
|
} catch (final Exception pEx) {
|
||||||
// TODO : Refactor exception
|
// TODO : Refactor exception
|
||||||
@@ -48,16 +55,16 @@ public class FileUploadService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Resource loadAvatar(final String pAvatarFileName) {
|
public Resource loadAvatar(final String pAvatarFileName) {
|
||||||
return loadImage(pAvatarFileName, FileEnum.FOLDER_PROFILE_IMAGES);
|
return loadImage(pAvatarFileName, folderProfileImages);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Resource loadImage(final String pImageLink) {
|
public Resource loadImage(final String pImageLink) {
|
||||||
return loadImage(pImageLink, FileEnum.FOLDER_IMAGE);
|
return loadImage(pImageLink, folderImages);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Resource loadImage(final String pImageLink, final FileEnum pFilePath) {
|
private Resource loadImage(final String pImageLink, final String pFilePath) {
|
||||||
try {
|
try {
|
||||||
Path imageFile = Paths.get(pFilePath.val()).resolve(pImageLink);
|
Path imageFile = Paths.get(pFilePath).resolve(pImageLink);
|
||||||
Resource imageResource = new UrlResource(imageFile.toUri());
|
Resource imageResource = new UrlResource(imageFile.toUri());
|
||||||
if(imageResource.exists() || imageResource.isReadable()) {
|
if(imageResource.exists() || imageResource.isReadable()) {
|
||||||
return imageResource;
|
return imageResource;
|
||||||
|
|||||||
@@ -14,4 +14,8 @@ logging.level.org.hibernate=DEBUG
|
|||||||
|
|
||||||
spring.servlet.multipart.max-file-size=104857600
|
spring.servlet.multipart.max-file-size=104857600
|
||||||
|
|
||||||
|
codiki.files.upload=/opt/codiki/pictures/tmp
|
||||||
|
codiki.files.profile-images=/opt/codiki/pictures/profiles
|
||||||
|
codiki.files.images=/opt/codiki/pictures/posts
|
||||||
|
|
||||||
cors.enabled=false
|
cors.enabled=false
|
||||||
Reference in New Issue
Block a user