Search in sources :

Example 1 with FileStorePath

use of com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath 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);
    }
}
Also used : Path(java.nio.file.Path) RemainingPath(com.infiniteautomation.mango.rest.latest.resolver.RemainingPath) FileStorePath(com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath) FileModel(com.infiniteautomation.mango.rest.latest.model.filestore.FileModel) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Date(java.util.Date)

Example 2 with FileStorePath

use of com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath 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);
}
Also used : Path(java.nio.file.Path) RemainingPath(com.infiniteautomation.mango.rest.latest.resolver.RemainingPath) FileStorePath(com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath) FileStorePath(com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) FileModel(com.infiniteautomation.mango.rest.latest.model.filestore.FileModel) FileItem(org.apache.commons.fileupload.FileItem) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ResponseEntity(org.springframework.http.ResponseEntity) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with FileStorePath

use of com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath 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);
}
Also used : Path(java.nio.file.Path) RemainingPath(com.infiniteautomation.mango.rest.latest.resolver.RemainingPath) FileStorePath(com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath) FileModel(com.infiniteautomation.mango.rest.latest.model.filestore.FileModel) HttpHeaders(org.springframework.http.HttpHeaders) NotFoundRestException(com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException) ResponseEntity(org.springframework.http.ResponseEntity) MediaType(org.springframework.http.MediaType) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Aggregations

FileModel (com.infiniteautomation.mango.rest.latest.model.filestore.FileModel)3 RemainingPath (com.infiniteautomation.mango.rest.latest.resolver.RemainingPath)3 FileStorePath (com.infiniteautomation.mango.spring.service.FileStoreService.FileStorePath)3 Path (java.nio.file.Path)3 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 ResponseEntity (org.springframework.http.ResponseEntity)2 NotFoundRestException (com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 FileItem (org.apache.commons.fileupload.FileItem)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MediaType (org.springframework.http.MediaType)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)1