Search in sources :

Example 1 with DirectoryContent

use of com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent in project musiccabinet by hakko.

the class DirectoryBrowserService method getContent.

private DirectoryContent getContent(Directory dir) {
    Set<File> foundFiles = new HashSet<>();
    Set<String> foundSubDirs = new HashSet<>();
    DirectoryContent content = new DirectoryContent(dir.getPath(), foundSubDirs, foundFiles);
    Path path = Paths.get(dir.getPath());
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        for (Path file : stream) {
            BasicFileAttributeView view = getFileAttributeView(file, BasicFileAttributeView.class);
            BasicFileAttributes attr = view.readAttributes();
            if (attr.isDirectory()) {
                foundSubDirs.add(file.toAbsolutePath().toString());
            } else if (attr.isRegularFile()) {
                foundFiles.add(new File(file, attr));
            }
        }
    } catch (IOException | DirectoryIteratorException e) {
        throw new ApplicationContextException("Couldn't read " + dir.getPath(), e);
    }
    return content;
}
Also used : Path(java.nio.file.Path) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) IOException(java.io.IOException) ApplicationContextException(org.springframework.context.ApplicationContextException) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) DirectoryContent(com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent) File(com.github.hakko.musiccabinet.domain.model.library.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet)

Example 2 with DirectoryContent

use of com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent in project musiccabinet by hakko.

the class LibraryScanner method postVisitDirectory.

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) {
    DirectoryContent content = map.get(dir);
    libraryPresenceChannel.send(new GenericMessage<DirectoryContent>(content));
    map.remove(dir);
    return CONTINUE;
}
Also used : DirectoryContent(com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent)

Example 3 with DirectoryContent

use of com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent in project musiccabinet by hakko.

the class LibraryScanner method visitFile.

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
    DirectoryContent directoryContent = map.get(file.getParent());
    if (attr.size() > Integer.MAX_VALUE) {
        LOG.warn(file.getFileName() + " has actual file size " + attr.size());
    }
    directoryContent.getFiles().add(new File(file, attr));
    return CONTINUE;
}
Also used : DirectoryContent(com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent) File(com.github.hakko.musiccabinet.domain.model.library.File)

Example 4 with DirectoryContent

use of com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent in project musiccabinet by hakko.

the class LibraryPresenceServiceTest method delegatesHandlingOfAddedAndDeletedResources.

@Test
public void delegatesHandlingOfAddedAndDeletedResources() {
    LibraryPresenceDao presenceDao = mock(LibraryPresenceDao.class);
    when(presenceDao.getFiles(dir1)).thenReturn(set(file1, file2, file3));
    when(presenceDao.getSubdirectories(dir1)).thenReturn(set(dir2));
    presenceService.setLibraryPresenceDao(presenceDao);
    file2b.setSize(file2.getSize() + 1);
    PollableChannel presenceChannel = presenceService.libraryPresenceChannel;
    presenceChannel.send(LibraryUtil.msg(dir1, set(dir2), set(file1, file2b)));
    presenceChannel.send(FINISHED_MESSAGE);
    presenceService.receive();
    Message<?> additionMessage, deletionMessage;
    assertNotNull(additionMessage = presenceService.libraryMetadataChannel.receive());
    assertNotNull(deletionMessage = presenceService.libraryDeletionChannel.receive());
    assertEquals(FINISHED_MESSAGE, presenceService.libraryMetadataChannel.receive());
    assertEquals(FINISHED_MESSAGE, presenceService.libraryDeletionChannel.receive());
    Set<File> addedFiles = ((DirectoryContent) additionMessage.getPayload()).getFiles();
    Set<File> deletedFiles = ((DirectoryContent) deletionMessage.getPayload()).getFiles();
    assertEquals(set(file2b), addedFiles);
    assertEquals(set(file2, file3), deletedFiles);
}
Also used : PollableChannel(org.springframework.integration.core.PollableChannel) DirectoryContent(com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent) LibraryPresenceDao(com.github.hakko.musiccabinet.dao.LibraryPresenceDao) UnittestLibraryUtil.getFile(com.github.hakko.musiccabinet.util.UnittestLibraryUtil.getFile) File(com.github.hakko.musiccabinet.domain.model.library.File) Test(org.junit.Test)

Example 5 with DirectoryContent

use of com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent in project musiccabinet by hakko.

the class DirectoryBrowserServiceTest method addedSubdirectoryDetectedWhenListingFiles.

@Test
public void addedSubdirectoryDetectedWhenListingFiles() throws ApplicationException, IOException {
    scannerService.add(set(media1));
    File newDir = new File(media1 + separatorChar + "newdir");
    newDir.mkdir();
    newDir.deleteOnExit();
    Directory dirMedia1 = getFirstRootDirectory();
    DirectoryContent found = browserService.getDirectoryDiff(dirMedia1.getId());
    Assert.assertTrue(found.getFiles().isEmpty());
    Assert.assertFalse(found.getSubDirectories().isEmpty());
    Assert.assertEquals(1, found.getSubDirectories().size());
    Assert.assertTrue(found.getSubDirectories().contains(newDir.getAbsolutePath()));
    newDir.delete();
}
Also used : DirectoryContent(com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent) File(java.io.File) Directory(com.github.hakko.musiccabinet.domain.model.library.Directory) Test(org.junit.Test)

Aggregations

DirectoryContent (com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent)12 File (com.github.hakko.musiccabinet.domain.model.library.File)5 Directory (com.github.hakko.musiccabinet.domain.model.library.Directory)4 Test (org.junit.Test)4 File (java.io.File)2 LibraryPresenceDao (com.github.hakko.musiccabinet.dao.LibraryPresenceDao)1 UnittestLibraryUtil.getFile (com.github.hakko.musiccabinet.util.UnittestLibraryUtil.getFile)1 IOException (java.io.IOException)1 DirectoryIteratorException (java.nio.file.DirectoryIteratorException)1 Path (java.nio.file.Path)1 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 HashSet (java.util.HashSet)1 ApplicationContextException (org.springframework.context.ApplicationContextException)1 PollableChannel (org.springframework.integration.core.PollableChannel)1