Search in sources :

Example 36 with FileStore

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

the class FileStoreService method deleteFileOrFolder.

public FileStorePath deleteFileOrFolder(String xid, String toDelete, boolean recursive) {
    FileStore fileStore = getWithoutPermissionCheck(xid);
    ensureEditPermission(Common.getUser(), fileStore);
    FileStorePath toDeletePath = getPathWithinFileStore(fileStore, toDelete);
    if (toDeletePath.absolutePath.equals(toDeletePath.getFileStoreRoot())) {
        throw new FileStoreException(new TranslatableMessage("filestore.deleteRootNotPermitted"));
    }
    if (!Files.exists(toDeletePath.absolutePath)) {
        throw new NotFoundException();
    }
    try {
        if (Files.isDirectory(toDeletePath.absolutePath) && recursive) {
            FileUtils.deleteDirectory(toDeletePath.absolutePath.toFile());
        } else {
            Files.delete(toDeletePath.absolutePath);
        }
        return toDeletePath;
    } catch (NoSuchFileException e) {
        throw new NotFoundException();
    } catch (Exception e) {
        throw new FileStoreException(new TranslatableMessage("filestore.errorDeletingFile"));
    }
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore) NoSuchFileException(java.nio.file.NoSuchFileException) 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 37 with FileStore

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

the class FileStoreService method insert.

@Override
public FileStore insert(FileStore vo) throws PermissionException, ValidationException {
    FileStore newFilestore = super.insert(vo);
    Path newPath = getFileStoreRoot(newFilestore);
    try {
        Files.createDirectories(newPath);
    } catch (IOException e) {
        throw new FileStoreException(new TranslatableMessage("filestore.failedToCreateRoot", newFilestore.getXid()), e);
    }
    return newFilestore;
}
Also used : Path(java.nio.file.Path) FileStore(com.serotonin.m2m2.vo.FileStore) IOException(java.io.IOException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 38 with FileStore

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

the class FileStoreService method ensureReadAccess.

/**
 * @throws IllegalArgumentException if path is not located inside the filestore root
 * @throws NotFoundException        if filestore was not found
 * @throws PermissionException      filestore exists but user does not have read access
 */
public void ensureReadAccess(Path path) throws IllegalArgumentException, NotFoundException, PermissionException {
    FileStore fileStore = resolveFileStore(path).getFileStore();
    ensureReadPermission(Common.getUser(), fileStore);
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore)

Example 39 with FileStore

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

the class FileStoreService method forRead.

/**
 * Retrieves the path to a file in the file store, checking that the user has read 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 read from the file store
 * @throws NotFoundException   file store does not exist
 */
public FileStorePath forRead(String xid, String path) throws PermissionException, NotFoundException {
    FileStore fileStore = getWithoutPermissionCheck(xid);
    ensureReadPermission(Common.getUser(), fileStore);
    return getPathWithinFileStore(fileStore, path);
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore)

Example 40 with FileStore

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

the class FileStoreDefinition method toFileStore.

public FileStore toFileStore() {
    FileStore fileStore = new FileStore();
    fileStore.setBuiltIn(true);
    fileStore.setXid(getStoreName());
    // TODO maybe use current user locale?
    fileStore.setName(getStoreDescription().translate(Common.getTranslations()));
    fileStore.setWritePermission(getWritePermission());
    fileStore.setReadPermission(getReadPermission());
    return fileStore;
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore)

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