-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BE add file IO service, config, exception
- Loading branch information
Showing
5 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...orm/FlinkRestService/src/main/java/com/yen/FlinkRestService/Config/StorageProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.yen.FlinkRestService.Config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
//@ConfigurationProperties("storage") | ||
public class StorageProperties { | ||
|
||
/** | ||
* Folder location for storing files | ||
*/ | ||
private String location = "upload-dir"; | ||
|
||
public String getLocation() { | ||
return location; | ||
} | ||
|
||
public void setLocation(String location) { | ||
this.location = location; | ||
} | ||
|
||
} |
126 changes: 126 additions & 0 deletions
126
...kRestService/src/main/java/com/yen/FlinkRestService/Service/FileSystemStorageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.yen.FlinkRestService.Service; | ||
|
||
/** | ||
* Service for file IO | ||
* | ||
* https://spring.io/guides/gs/uploading-files/ | ||
*/ | ||
|
||
import com.yen.FlinkRestService.Config.StorageProperties; | ||
import com.yen.FlinkRestService.exception.StorageException; | ||
import com.yen.FlinkRestService.exception.StorageFileNotFoundException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.UrlResource; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.FileSystemUtils; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.stream.Stream; | ||
|
||
@Service | ||
public class FileSystemStorageService implements StorageService { | ||
|
||
private final Path rootLocation; | ||
|
||
@Autowired | ||
public FileSystemStorageService(StorageProperties properties) { | ||
|
||
if(properties.getLocation().trim().length() == 0){ | ||
throw new StorageException("File upload location can not be Empty."); | ||
} | ||
|
||
this.rootLocation = Paths.get(properties.getLocation()); | ||
} | ||
|
||
@Override | ||
public void store(MultipartFile file) { | ||
|
||
try { | ||
if (file.isEmpty()) { | ||
throw new StorageException("Failed to store empty file."); | ||
} | ||
Path destinationFile = this.rootLocation.resolve( | ||
Paths.get(file.getOriginalFilename())) | ||
.normalize().toAbsolutePath(); | ||
if (!destinationFile.getParent().equals(this.rootLocation.toAbsolutePath())) { | ||
// This is a security check | ||
throw new StorageException( | ||
"Cannot store file outside current directory."); | ||
} | ||
try (InputStream inputStream = file.getInputStream()) { | ||
Files.copy(inputStream, destinationFile, | ||
StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
} | ||
catch (IOException e) { | ||
throw new StorageException("Failed to store file.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public Stream<Path> loadAll() { | ||
|
||
try { | ||
return Files.walk(this.rootLocation, 1) | ||
.filter(path -> !path.equals(this.rootLocation)) | ||
.map(this.rootLocation::relativize); | ||
} | ||
catch (IOException e) { | ||
throw new StorageException("Failed to read stored files", e); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public Path load(String filename) { | ||
|
||
return rootLocation.resolve(filename); | ||
} | ||
|
||
@Override | ||
public Resource loadAsResource(String filename) { | ||
|
||
try { | ||
Path file = load(filename); | ||
Resource resource = new UrlResource(file.toUri()); | ||
if (resource.exists() || resource.isReadable()) { | ||
return resource; | ||
} | ||
else { | ||
throw new StorageFileNotFoundException( | ||
"Could not read file: " + filename); | ||
|
||
} | ||
} | ||
catch (MalformedURLException e) { | ||
throw new StorageFileNotFoundException("Could not read file: " + filename, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteAll() { | ||
|
||
FileSystemUtils.deleteRecursively(rootLocation.toFile()); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
|
||
try { | ||
Files.createDirectories(rootLocation); | ||
} | ||
catch (IOException e) { | ||
throw new StorageException("Could not initialize storage", e); | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...tform/FlinkRestService/src/main/java/com/yen/FlinkRestService/Service/StorageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.yen.FlinkRestService.Service; | ||
|
||
/** https://spring.io/guides/gs/uploading-files/ */ | ||
|
||
import org.springframework.core.io.Resource; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
|
||
public interface StorageService { | ||
|
||
void init(); | ||
|
||
void store(MultipartFile file); | ||
|
||
Stream<Path> loadAll(); | ||
|
||
Path load(String filename); | ||
|
||
Resource loadAsResource(String filename); | ||
|
||
void deleteAll(); | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...m/FlinkRestService/src/main/java/com/yen/FlinkRestService/exception/StorageException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.yen.FlinkRestService.exception; | ||
|
||
public class StorageException extends RuntimeException { | ||
|
||
public StorageException(String message) { | ||
super(message); | ||
} | ||
|
||
public StorageException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ervice/src/main/java/com/yen/FlinkRestService/exception/StorageFileNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.yen.FlinkRestService.exception; | ||
|
||
public class StorageFileNotFoundException extends StorageException { | ||
|
||
public StorageFileNotFoundException(String message) { | ||
super(message); | ||
} | ||
|
||
public StorageFileNotFoundException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |