use of com.infiniteautomation.mango.rest.v2.exception.GenericRestException in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method moveFileOrFolder.
private ResponseEntity<FileModel> moveFileOrFolder(HttpServletRequest request, String fileStoreName, File root, File fileOrFolder, String moveTo) throws IOException, URISyntaxException {
if (!fileOrFolder.exists()) {
throw new NotFoundRestException();
}
Path srcPath = fileOrFolder.toPath();
File dstFile = new File(fileOrFolder.getParentFile(), moveTo).getCanonicalFile();
Path dstPath = dstFile.toPath();
if (!dstPath.startsWith(root.toPath())) {
throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", moveTo));
}
if (dstFile.isDirectory()) {
dstPath = dstPath.resolve(srcPath.getFileName());
}
Path movedPath;
try {
movedPath = java.nio.file.Files.move(srcPath, dstPath);
} catch (FileAlreadyExistsException e) {
throw new GenericRestException(HttpStatus.CONFLICT, new TranslatableMessage("filestore.fileExists", dstPath.getFileName()));
}
File movedFile = new File(movedPath.toUri());
FileModel fileModel = fileToModel(movedFile, root, request.getServletContext());
return new ResponseEntity<>(fileModel, HttpStatus.OK);
}
use of com.infiniteautomation.mango.rest.v2.exception.GenericRestException 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.GenericRestException 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);
}
use of com.infiniteautomation.mango.rest.v2.exception.GenericRestException in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method findUniqueFileName.
private File findUniqueFileName(File directory, String filename, boolean overwrite) throws IOException {
File file = new File(directory, filename).getCanonicalFile();
if (!file.toPath().startsWith(directory.toPath())) {
throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowUploadDirectory", filename));
}
if (overwrite) {
return file;
}
File parent = file.getParentFile();
String originalName = Files.getNameWithoutExtension(filename);
String extension = Files.getFileExtension(filename);
int i = 1;
while (file.exists()) {
if (extension.isEmpty()) {
file = new File(parent, String.format("%s_%03d", originalName, i++));
} else {
file = new File(parent, String.format("%s_%03d.%s", originalName, i++, extension));
}
}
return file;
}
use of com.infiniteautomation.mango.rest.v2.exception.GenericRestException in project ma-modules-public by infiniteautomation.
the class JsonEmportV2Controller method importConfiguration.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Import Configuration", notes = "Submit the request and get a URL for the results")
@RequestMapping(method = { RequestMethod.POST })
public ResponseEntity<ImportStatusProvider> importConfiguration(HttpServletRequest request, UriComponentsBuilder builder, @ApiParam(value = "Optional timeout for resource to expire, defaults to 5 minutes", required = false, allowMultiple = false) @RequestParam(value = "timeout", required = false) Long timeout, @RequestBody(required = true) JsonValue config, @AuthenticationPrincipal User user) {
if (config instanceof JsonObject) {
// Setup the Temporary Resource
String resourceId = importStatusResources.generateResourceId();
ImportStatusProvider statusProvider = new ImportStatusProvider(importStatusResources, resourceId, websocket, timeout, config.toJsonObject(), user);
this.importStatusResources.put(resourceId, statusProvider);
URI location = builder.path("/v2/json-emport/import/{id}").buildAndExpand(resourceId).toUri();
return getResourceCreated(statusProvider, location);
} else {
throw new GenericRestException(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.invalidImportData"));
}
}
Aggregations