use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class LoggingRestController method list.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "List Log Files", notes = "Returns a list of logfile metadata")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/files")
public ResponseEntity<List<FileModel>> list(@RequestParam(value = "limit", required = false) Integer limit, HttpServletRequest request) throws IOException {
List<FileModel> modelList = new ArrayList<FileModel>();
File logsDir = Common.getLogsDir();
int count = 0;
for (File file : logsDir.listFiles()) {
if ((limit != null) && (count >= limit.intValue()))
break;
if (!file.getName().startsWith(".")) {
FileModel model = FileStoreRestV2Controller.fileToModel(file, logsDir, request.getServletContext());
model.setMimeType("text/plain");
modelList.add(model);
count++;
}
}
return ResponseEntity.ok(modelList);
}
use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method fileToModel.
public static FileModel fileToModel(File file, File root, ServletContext context) throws UnsupportedEncodingException {
FileModel model = new FileModel();
model.setFilename(file.getName());
model.setFolderPath(relativePath(root, file.getParentFile()));
model.setDirectory(file.isDirectory());
model.setLastModified(new Date(file.lastModified()));
model.setMimeType(context.getMimeType(file.getName()));
if (!file.isDirectory())
model.setSize(file.length());
return model;
}
use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method createNewFolder.
@ApiOperation(value = "Create a folder or copy/move/rename an existing file or folder", notes = "Must have write access to the store")
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, value = "/{fileStoreName}/**")
public ResponseEntity<FileModel> createNewFolder(@ApiParam(value = "Valid File Store name", required = true, allowMultiple = false) @PathVariable("fileStoreName") String fileStoreName, @ApiParam(value = "Move file/folder to", required = false, allowMultiple = false) @RequestParam(required = false) String moveTo, @ApiParam(value = "Copy file/folder to", required = false, allowMultiple = false) @RequestParam(required = false) String copyTo, @AuthenticationPrincipal User user, HttpServletRequest request) throws IOException, URISyntaxException {
FileStoreDefinition def = ModuleRegistry.getFileStoreDefinition(fileStoreName);
if (def == null)
throw new NotFoundRestException();
// Check Permissions
def.ensureStoreWritePermission(user);
String pathInStore = parsePath(request);
File root = def.getRoot().getCanonicalFile();
File fileOrFolder = new File(root, pathInStore).getCanonicalFile();
if (!fileOrFolder.toPath().startsWith(root.toPath())) {
throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", pathInStore));
}
if (copyTo != null) {
return copyFileOrFolder(request, fileStoreName, root, fileOrFolder, copyTo);
} else if (moveTo != null) {
return moveFileOrFolder(request, fileStoreName, root, fileOrFolder, moveTo);
} else {
return createFolder(request, fileStoreName, root, fileOrFolder);
}
}
use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class FileStoreRestV2Controller method uploadWithPath.
@ApiOperation(value = "Upload a file to a store with a path", notes = "Must have write access to the store")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, value = "/{name}/**")
public ResponseEntity<List<FileModel>> uploadWithPath(@ApiParam(value = "Valid File Store name", required = true, allowMultiple = false) @PathVariable("name") String name, @AuthenticationPrincipal User user, @RequestParam(required = false, defaultValue = "false") boolean overwrite, MultipartHttpServletRequest multipartRequest, HttpServletRequest request) throws IOException {
FileStoreDefinition def = ModuleRegistry.getFileStoreDefinition(name);
if (def == null)
throw new NotFoundRestException();
// Check Permissions
def.ensureStoreWritePermission(user);
String pathInStore = parsePath(request);
File root = def.getRoot().getCanonicalFile();
Path rootPath = root.toPath();
File outputDirectory = new File(root, pathInStore).getCanonicalFile();
if (!outputDirectory.toPath().startsWith(rootPath)) {
throw new GenericRestException(HttpStatus.FORBIDDEN, new TranslatableMessage("filestore.belowRoot", pathInStore));
}
if (outputDirectory.exists() && !outputDirectory.isDirectory()) {
throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("filestore.cannotCreateDir", removeToRoot(root, outputDirectory), name));
}
if (!outputDirectory.exists()) {
if (!outputDirectory.mkdirs())
throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("filestore.cannotCreateDir", removeToRoot(root, outputDirectory), name));
}
// Put the file where it belongs
List<FileModel> fileModels = new ArrayList<>();
MultiValueMap<String, MultipartFile> filemap = multipartRequest.getMultiFileMap();
for (String nameField : filemap.keySet()) {
for (MultipartFile file : filemap.get(nameField)) {
String filename;
if (file instanceof CommonsMultipartFile) {
FileItem fileItem = ((CommonsMultipartFile) file).getFileItem();
filename = fileItem.getName();
} else {
filename = file.getName();
}
File newFile = findUniqueFileName(outputDirectory, filename, overwrite);
File parent = newFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try (OutputStream output = new FileOutputStream(newFile, false)) {
try (InputStream input = file.getInputStream()) {
StreamUtils.copy(input, output);
}
}
fileModels.add(fileToModel(newFile, root, request.getServletContext()));
}
}
return new ResponseEntity<>(fileModels, HttpStatus.OK);
}
use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel 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);
}
Aggregations