Skip to content

Commit

Permalink
BE add file IO service, config, exception
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Jan 14, 2024
1 parent 2d8371b commit c9b0402
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 0 deletions.
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;
}

}
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);
}
}

}
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();

}
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);
}
}
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);
}
}

0 comments on commit c9b0402

Please sign in to comment.