Search in sources :

Example 6 with FileModel

use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel 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);
}
Also used : FileModel(com.infiniteautomation.mango.rest.v2.model.filestore.FileModel) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) ResourceNotFoundException(com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException) File(java.io.File) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 7 with FileModel

use of com.infiniteautomation.mango.rest.v2.model.filestore.FileModel 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);
}
Also used : Path(java.nio.file.Path) FileModel(com.infiniteautomation.mango.rest.v2.model.filestore.FileModel) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ResponseEntity(org.springframework.http.ResponseEntity) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) File(java.io.File) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException)

Aggregations

FileModel (com.infiniteautomation.mango.rest.v2.model.filestore.FileModel)6 File (java.io.File)6 MultipartFile (org.springframework.web.multipart.MultipartFile)5 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)5 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)4 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 ResponseEntity (org.springframework.http.ResponseEntity)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 FileStoreDefinition (com.serotonin.m2m2.module.FileStoreDefinition)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 ResourceNotFoundException (com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Date (java.util.Date)1 FileItem (org.apache.commons.fileupload.FileItem)1