use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method listStoreContents.
protected ResponseEntity<List<FileModel>> listStoreContents(File directory, File root, HttpServletRequest request) throws IOException {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
if (directory.equals(root) && !root.exists())
return new ResponseEntity<>(Collections.emptyList(), responseHeaders, HttpStatus.OK);
if (!directory.exists()) {
throw new ResourceNotFoundException(relativePath(root, directory));
}
Collection<File> files = Arrays.asList(directory.listFiles());
List<FileModel> found = new ArrayList<>(files.size());
for (File file : files) found.add(fileToModel(file, root, request.getServletContext()));
Set<MediaType> mediaTypes = Sets.newHashSet(MediaType.APPLICATION_JSON_UTF8);
request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
return new ResponseEntity<>(found, responseHeaders, HttpStatus.OK);
}
use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method copyFileOrFolder.
private ResponseEntity<FileModel> copyFileOrFolder(HttpServletRequest request, String fileStoreName, File root, File srcFile, String dst) throws IOException, URISyntaxException {
if (!srcFile.exists()) {
throw new NotFoundRestException();
}
if (srcFile.isDirectory()) {
throw new GenericRestException(HttpStatus.BAD_REQUEST, new TranslatableMessage("filestore.cantCopyDirectory"));
}
Path srcPath = srcFile.toPath();
File dstFile = new File(srcFile.getParentFile(), dst).getCanonicalFile();
Path dstPath = dstFile.toPath();
if (!dstPath.startsWith(root.toPath())) {
throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", dst));
}
if (dstFile.isDirectory()) {
dstPath = dstPath.resolve(srcPath.getFileName());
}
Path copiedPath;
try {
copiedPath = java.nio.file.Files.copy(srcPath, dstPath);
} catch (FileAlreadyExistsException e) {
throw new GenericRestException(HttpStatus.CONFLICT, new TranslatableMessage("filestore.fileExists", dstPath.getFileName()));
}
File copiedFile = new File(copiedPath.toUri());
FileModel fileModel = fileToModel(copiedFile, root, request.getServletContext());
return new ResponseEntity<>(fileModel, HttpStatus.OK);
}
Aggregations