blob: 110d340b0b2ea05f4e42d0041c0d2600ff701f93 [file] [log] [blame]
/*********************************************************************************
* Copyright (c) 2020 Robert Bosch GmbH and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
********************************************************************************
*/
package org.eclipse.app4mc.cloud.manager.storage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.stream.Stream;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@Service
public class FileSystemStorageService implements StorageService {
private static final String TEMP_DIR_PREFIX = "app4mc_mgr_";
private final String defaultBaseDir = System.getProperty("java.io.tmpdir");
private final Path tempLocation = Paths.get(defaultBaseDir);
@Override
public String store(MultipartFile file) {
try {
if (file.isEmpty()) {
throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
}
Path path = Files.createTempDirectory(TEMP_DIR_PREFIX);
// extract uuid from pathname
String uuid = path.toString().substring(path.toString().lastIndexOf('_') + 1);
Files.copy(file.getInputStream(), path.resolve(file.getOriginalFilename()));
return uuid;
} catch (IOException e) {
throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
}
}
@Override
public Stream<Path> loadAll() {
try {
return Files.walk(this.tempLocation, 1)
.filter(path -> !path.equals(this.tempLocation))
.filter(path -> path.getFileName().toString().startsWith(TEMP_DIR_PREFIX))
.map(path -> this.tempLocation.relativize(path));
} catch (IOException e) {
throw new StorageException("Failed to read stored files", e);
}
}
@Override
public Path load(String uuid, String... path) {
Path result = this.tempLocation.resolve(TEMP_DIR_PREFIX + uuid);
for (String p : path) {
result = result.resolve(p);
}
return result;
}
@Override
public Resource loadAsResource(String uuid, String... path) {
Path file = load(uuid, path);
try {
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
}
else {
throw new StorageFileNotFoundException("Could not read file: " + file);
}
} catch (MalformedURLException e) {
throw new StorageFileNotFoundException("Could not read file: " + file, e);
}
}
@Override
public void delete(String uuid) {
Path path = load(uuid);
try {
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map(java.nio.file.Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
throw new StorageFileNotFoundException("Could not delete: " + path);
}
}
@Override
public void deleteAll() {
// loadAll().forEach(path -> FileSystemUtils.deleteRecursively(path.toFile()));
loadAll().forEach(System.out::println);
}
}