Search in sources :

Example 31 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method moveFileOrFolder.

public FileStorePath moveFileOrFolder(String xid, String src, String dst) {
    FileStore fileStore = getWithoutPermissionCheck(xid);
    ensureEditPermission(Common.getUser(), fileStore);
    FileStorePath srcPath = getPathWithinFileStore(fileStore, src);
    if (!Files.exists(srcPath.absolutePath)) {
        throw new NotFoundException();
    }
    FileStorePath dstPath = srcPath.getParent().resolve(Paths.get(dst));
    if (Files.isDirectory(dstPath.absolutePath)) {
        Path pathWithFileName = dstPath.absolutePath.resolve(srcPath.absolutePath.getFileName());
        dstPath = new FileStorePath(fileStore, pathWithFileName);
    }
    try {
        Files.move(srcPath.absolutePath, dstPath.absolutePath);
        return dstPath;
    } catch (FileAlreadyExistsException e) {
        throw new FileStoreException(new TranslatableMessage("filestore.fileExists", dstPath.standardizedPath()));
    } catch (Exception e) {
        throw new FileStoreException(new TranslatableMessage("filestore.errorMovingFile"));
    }
}
Also used : Path(java.nio.file.Path) FileStore(com.serotonin.m2m2.vo.FileStore) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NoSuchFileException(java.nio.file.NoSuchFileException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableRuntimeException(com.infiniteautomation.mango.util.exception.TranslatableRuntimeException) TranslatableIllegalArgumentException(com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Example 32 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method resolveFileStore.

private FileStorePath resolveFileStore(Path path) {
    Path absolutePath = path.toAbsolutePath().normalize();
    if (!absolutePath.startsWith(fileStoreRoot)) {
        throw new TranslatableIllegalArgumentException(new TranslatableMessage("filestore.invalidPath"));
    }
    Path relative = fileStoreRoot.relativize(absolutePath);
    if (relative.getNameCount() == 0) {
        throw new TranslatableIllegalArgumentException(new TranslatableMessage("filestore.invalidPath"));
    }
    String fileStoreName = relative.getName(0).toString();
    FileStore fileStore = getWithoutPermissionCheck(fileStoreName);
    return new FileStorePath(fileStore, absolutePath);
}
Also used : Path(java.nio.file.Path) FileStore(com.serotonin.m2m2.vo.FileStore) TranslatableIllegalArgumentException(com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 33 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method forWrite.

/**
 * Retrieves the path to a file in the file store, checking that the user has write permission for the file store.
 * Does not check if the file exists, only that the filestore exists.
 *
 * @param xid  xid of the user file store, or the storeName of the {@link FileStoreDefinition}
 * @throws PermissionException user does not have permission to write to the file store
 * @throws NotFoundException   file store does not exist
 */
public FileStorePath forWrite(String xid, String path) throws PermissionException, NotFoundException {
    FileStore fileStore = getWithoutPermissionCheck(xid);
    ensureEditPermission(Common.getUser(), fileStore);
    return getPathWithinFileStore(fileStore, path);
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore)

Example 34 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method copyFileOrFolder.

public FileStorePath copyFileOrFolder(String xid, String src, String dst) {
    FileStore fileStore = getWithoutPermissionCheck(xid);
    ensureEditPermission(Common.getUser(), fileStore);
    FileStorePath srcPath = getPathWithinFileStore(fileStore, src);
    if (!Files.exists(srcPath.absolutePath)) {
        throw new NotFoundException();
    }
    FileStorePath dstPath = srcPath.getParent().resolve(Paths.get(dst));
    if (Files.isDirectory(dstPath.absolutePath)) {
        Path pathWithFileName = dstPath.absolutePath.resolve(srcPath.absolutePath.getFileName());
        dstPath = new FileStorePath(fileStore, pathWithFileName);
    }
    if (Files.exists(dstPath.absolutePath)) {
        throw new FileStoreException(new TranslatableMessage("filestore.fileExists", dstPath.standardizedPath()));
    }
    try {
        if (Files.isDirectory(srcPath.absolutePath)) {
            FileUtils.copyDirectory(srcPath.absolutePath.toFile(), dstPath.absolutePath.toFile());
        } else {
            Files.copy(srcPath.absolutePath, dstPath.absolutePath);
        }
        return dstPath;
    } catch (FileAlreadyExistsException e) {
        throw new FileStoreException(new TranslatableMessage("filestore.fileExists", dstPath.standardizedPath()));
    } catch (Exception e) {
        throw new FileStoreException(new TranslatableMessage("filestore.errorCopyingFile"));
    }
}
Also used : Path(java.nio.file.Path) FileStore(com.serotonin.m2m2.vo.FileStore) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NoSuchFileException(java.nio.file.NoSuchFileException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableRuntimeException(com.infiniteautomation.mango.util.exception.TranslatableRuntimeException) TranslatableIllegalArgumentException(com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Example 35 with FileStore

use of com.serotonin.m2m2.vo.FileStore in project ma-core-public by infiniteautomation.

the class FileStoreService method getStores.

/**
 * List all file-stores that the user has read permission for
 */
public List<FileStore> getStores() {
    List<FileStore> stores = new ArrayList<>();
    this.customizedQuery(new ConditionSortLimit(null, null, null, null), (item) -> stores.add(item));
    PermissionHolder user = Common.getUser();
    Collection<FileStoreDefinition> moduleDefs = ModuleRegistry.getFileStoreDefinitions().values();
    for (FileStoreDefinition def : moduleDefs) {
        if (this.permissionService.hasPermission(user, def.getReadPermission())) {
            stores.add(def.toFileStore());
        }
    }
    return stores;
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore) ArrayList(java.util.ArrayList) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit) FileStoreDefinition(com.serotonin.m2m2.module.FileStoreDefinition)

Aggregations

FileStore (com.serotonin.m2m2.vo.FileStore)38 Path (java.nio.file.Path)18 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)17 IOException (java.io.IOException)15 TranslatableIllegalArgumentException (com.infiniteautomation.mango.util.exception.TranslatableIllegalArgumentException)10 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)7 TranslatableRuntimeException (com.infiniteautomation.mango.util.exception.TranslatableRuntimeException)6 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)6 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)6 NoSuchFileException (java.nio.file.NoSuchFileException)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)4 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)4 ApiOperation (io.swagger.annotations.ApiOperation)4 ArrayList (java.util.ArrayList)4 FileStoreModel (com.infiniteautomation.mango.rest.latest.model.filestore.FileStoreModel)3 ConditionSortLimit (com.infiniteautomation.mango.db.query.ConditionSortLimit)2 MangoScript (com.infiniteautomation.mango.spring.script.MangoScript)2 LoadFileStorePermission (com.infiniteautomation.mango.spring.script.permissions.LoadFileStorePermission)2