use of com.infiniteautomation.mango.rest.latest.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class FileStoreRestController method fileToModel.
/**
* Convert a file to a model
*/
private FileModel fileToModel(FileStorePath fileStorePath, ServletContext context) {
try {
Path file = fileStorePath.getAbsolutePath();
String fileName = file.getFileName().toString();
FileModel model = new FileModel();
model.setFilename(fileName);
model.setFolderPath(fileStorePath.getParent().standardizedPath());
model.setRelativePath(fileStorePath.standardizedPath());
model.setFileStoreXid(fileStorePath.getFileStore().getXid());
model.setDirectory(Files.isDirectory(file));
model.setLastModified(new Date(Files.getLastModifiedTime(file).toMillis()));
model.setMimeType(context.getMimeType(fileName));
if (Files.isRegularFile(file))
model.setSize(Files.size(file));
return model;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of com.infiniteautomation.mango.rest.latest.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class FileStoreRestController 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, value = "/{name}/**")
public ResponseEntity<List<FileModel>> uploadWithPath(@ApiParam(value = "Valid File Store name", required = true) @PathVariable("name") String name, @RequestParam(required = false, defaultValue = "false") boolean overwrite, @ApiIgnore @RemainingPath String pathInStore, MultipartHttpServletRequest multipartRequest, HttpServletRequest request) throws IOException {
FileStorePath outputDirectory = this.service.createDirectory(name, pathInStore);
// Put the file where it belongs
List<FileModel> fileModels = new ArrayList<>();
MultiValueMap<String, MultipartFile> filesMap = multipartRequest.getMultiFileMap();
for (String nameField : filesMap.keySet()) {
for (MultipartFile file : filesMap.get(nameField)) {
String filename;
if (file instanceof CommonsMultipartFile) {
FileItem fileItem = ((CommonsMultipartFile) file).getFileItem();
filename = fileItem.getName();
} else {
filename = file.getName();
}
Path newFile = findUniqueFileName(outputDirectory.getAbsolutePath(), filename, overwrite);
try (OutputStream output = Files.newOutputStream(newFile)) {
try (InputStream input = file.getInputStream()) {
StreamUtils.copy(input, output);
}
}
fileModels.add(fileToModel(outputDirectory.resolve(newFile), request.getServletContext()));
}
}
return new ResponseEntity<>(fileModels, HttpStatus.OK);
}
use of com.infiniteautomation.mango.rest.latest.model.filestore.FileModel in project ma-modules-public by infiniteautomation.
the class FileStoreRestController method listStoreContents.
protected ResponseEntity<List<FileModel>> listStoreContents(FileStorePath fileStorePath, HttpServletRequest request) {
Path directory = fileStorePath.getAbsolutePath();
if (!Files.isDirectory(directory)) {
throw new NotFoundRestException();
}
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON);
List<FileModel> models;
try {
models = Files.list(directory).map(p -> fileToModel(fileStorePath.resolve(p), request.getServletContext())).collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Set<MediaType> mediaTypes = Sets.newHashSet(MediaType.APPLICATION_JSON);
request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);
return new ResponseEntity<>(models, responseHeaders, HttpStatus.OK);
}
Aggregations