Search in sources :

Example 1 with FileStorageAPI

use of com.haulmont.cuba.core.app.FileStorageAPI in project cuba by cuba-platform.

the class FileStorage method findOrphanFiles.

@Override
public String findOrphanFiles() {
    FileStorageAPI fileStorageAPI = AppBeans.get(FileStorageAPI.class);
    if (!(fileStorageAPI instanceof com.haulmont.cuba.core.app.filestorage.FileStorage)) {
        return "<not supported>";
    }
    File[] roots = getStorageRoots();
    if (roots.length == 0)
        return "No storage directories defined";
    StringBuilder sb = new StringBuilder();
    File storageFolder = roots[0];
    if (!storageFolder.exists())
        return ExceptionUtils.getStackTrace(new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, storageFolder.getAbsolutePath()));
    @SuppressWarnings("unchecked") Collection<File> systemFiles = FileUtils.listFiles(storageFolder, null, true);
    @SuppressWarnings("unchecked") Collection<File> filesInRootFolder = FileUtils.listFiles(storageFolder, null, false);
    // remove files of root storage folder (e.g. storage.log) from files collection
    systemFiles.removeAll(filesInRootFolder);
    List<FileDescriptor> fileDescriptors;
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<FileDescriptor> query = em.createQuery("select fd from sys$FileDescriptor fd", FileDescriptor.class);
        fileDescriptors = query.getResultList();
        tx.commit();
    } catch (Exception e) {
        return ExceptionUtils.getStackTrace(e);
    } finally {
        tx.end();
    }
    Set<String> descriptorsFileNames = new HashSet<>();
    for (FileDescriptor fileDescriptor : fileDescriptors) {
        descriptorsFileNames.add(com.haulmont.cuba.core.app.filestorage.FileStorage.getFileName(fileDescriptor));
    }
    for (File file : systemFiles) {
        if (!descriptorsFileNames.contains(file.getName()))
            // Encode file path if it contains non-ASCII characters
            if (!file.getPath().matches("\\p{ASCII}+")) {
                String encodedFilePath = URLEncodeUtils.encodeUtf8(file.getPath());
                sb.append(encodedFilePath).append("\n");
            } else {
                sb.append(file.getPath()).append("\n");
            }
    }
    return sb.toString();
}
Also used : FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileStorageAPI(com.haulmont.cuba.core.app.FileStorageAPI) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with FileStorageAPI

use of com.haulmont.cuba.core.app.FileStorageAPI in project cuba by cuba-platform.

the class FileStorage method findOrphanDescriptors.

@Override
public String findOrphanDescriptors() {
    com.haulmont.cuba.core.app.filestorage.FileStorage fileStorage;
    FileStorageAPI fileStorageAPI = AppBeans.get(FileStorageAPI.class);
    if (fileStorageAPI instanceof com.haulmont.cuba.core.app.filestorage.FileStorage) {
        fileStorage = (com.haulmont.cuba.core.app.filestorage.FileStorage) fileStorageAPI;
    } else {
        return "<not supported>";
    }
    File[] roots = getStorageRoots();
    if (roots.length == 0)
        return "No storage directories defined";
    StringBuilder sb = new StringBuilder();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<FileDescriptor> query = em.createQuery("select fd from sys$FileDescriptor fd", FileDescriptor.class);
        List<FileDescriptor> fileDescriptors = query.getResultList();
        for (FileDescriptor fileDescriptor : fileDescriptors) {
            File dir = fileStorage.getStorageDir(roots[0], fileDescriptor);
            File file = new File(dir, com.haulmont.cuba.core.app.filestorage.FileStorage.getFileName(fileDescriptor));
            if (!file.exists()) {
                sb.append(fileDescriptor.getId()).append(", ").append(fileDescriptor.getName()).append(", ").append(fileDescriptor.getCreateDate()).append("\n");
            }
        }
        tx.commit();
    } catch (Exception e) {
        return ExceptionUtils.getStackTrace(e);
    } finally {
        tx.end();
    }
    return sb.toString();
}
Also used : FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileStorageAPI(com.haulmont.cuba.core.app.FileStorageAPI) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) File(java.io.File)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)2 Transaction (com.haulmont.cuba.core.Transaction)2 FileStorageAPI (com.haulmont.cuba.core.app.FileStorageAPI)2 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)2 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)2 File (java.io.File)2 HashSet (java.util.HashSet)1