use of com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method download.
@ApiOperation(value = "List a directory or download a file from a store")
@RequestMapping(method = RequestMethod.GET, produces = {}, value = "/{name}/**")
public ResponseEntity<?> download(@ApiParam(value = "Valid File Store name", required = true, allowMultiple = false) @PathVariable("name") String name, @ApiParam(value = "Set content disposition to attachment", required = false, defaultValue = "true", allowMultiple = false) @RequestParam(required = false, defaultValue = "true") boolean download, @AuthenticationPrincipal User user, HttpServletRequest request, HttpServletResponse response) throws IOException, HttpMediaTypeNotAcceptableException {
FileStoreDefinition def = ModuleRegistry.getFileStoreDefinition(name);
if (def == null)
throw new ResourceNotFoundException("File store: " + name);
// Check permissions
def.ensureStoreReadPermission(user);
File root = def.getRoot().getCanonicalFile();
String path = parsePath(request);
File file = new File(root, path).getCanonicalFile();
if (!file.toPath().startsWith(root.toPath())) {
throw new AccessDeniedException("Path is below file store root");
}
// TODO Allow downloading directory as a zip
if (file.isFile()) {
return getFile(file, download, request, response);
} else {
return listStoreContents(file, root, request);
}
}
use of com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method delete.
@ApiOperation(value = "Delete a file or directory")
@RequestMapping(method = RequestMethod.DELETE, produces = {}, value = "/{name}/**")
public ResponseEntity<Void> delete(@ApiParam(value = "Valid File Store name", required = true, allowMultiple = false) @PathVariable("name") String name, @ApiParam(value = "Recurisve delete of directory", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean recursive, @AuthenticationPrincipal User user, HttpServletRequest request) throws IOException, HttpMediaTypeNotAcceptableException {
FileStoreDefinition def = ModuleRegistry.getFileStoreDefinition(name);
if (def == null)
throw new ResourceNotFoundException("File store: " + name);
// Check permissions
def.ensureStoreWritePermission(user);
File root = def.getRoot().getCanonicalFile();
String path = parsePath(request);
File file = new File(root, path).getCanonicalFile();
if (!file.toPath().startsWith(root.toPath())) {
throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", path));
}
if (!file.exists())
throw new NotFoundRestException();
if (file.isDirectory() && recursive) {
FileUtils.deleteDirectory(file);
} else {
if (!file.delete()) {
throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("filestore.errorDeletingFile"));
}
}
return new ResponseEntity<>(null, HttpStatus.OK);
}
use of com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException 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);
}
Aggregations