Search in sources :

Example 6 with AttachmentState

use of org.obiba.mica.file.AttachmentState in project mica2 by obiba.

the class FileSystemService method zipDirectory.

/**
 * Creates a zipped file of the path and it's subdirectories/files
 *
 * @param path
 * @param publishedFS
 * @return
 */
public String zipDirectory(String path, boolean publishedFS) {
    List<AttachmentState> attachmentStates = getAllowedStates(path, publishedFS);
    String zipName = Paths.get(path).getFileName().toString() + ".zip";
    FileOutputStream fos = null;
    try {
        byte[] buffer = new byte[1024];
        fos = tempFileService.getFileOutputStreamFromFile(zipName);
        ZipOutputStream zos = new ZipOutputStream(fos);
        for (AttachmentState state : attachmentStates) {
            if (FileUtils.isDirectory(state)) {
                zos.putNextEntry(new ZipEntry(relativizePaths(path, state.getFullPath()) + File.separator));
            } else {
                zos.putNextEntry(new ZipEntry(relativizePaths(path, state.getFullPath())));
                InputStream in = fileStoreService.getFile(publishedFS ? state.getPublishedAttachment().getFileReference() : state.getAttachment().getFileReference());
                int len;
                while ((len = in.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
                in.close();
            }
            zos.closeEntry();
        }
        zos.finish();
    } catch (IOException ioe) {
        Throwables.propagate(ioe);
    } finally {
        IOUtils.closeQuietly(fos);
    }
    return zipName;
}
Also used : AttachmentState(org.obiba.mica.file.AttachmentState) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 7 with AttachmentState

use of org.obiba.mica.file.AttachmentState in project mica2 by obiba.

the class FileIndexer method reIndexAll.

@Async
@Subscribe
public void reIndexAll(IndexFilesEvent event) {
    if (indexer.hasIndex(Indexer.ATTACHMENT_DRAFT_INDEX))
        indexer.dropIndex(Indexer.ATTACHMENT_DRAFT_INDEX);
    if (indexer.hasIndex(Indexer.ATTACHMENT_PUBLISHED_INDEX))
        indexer.dropIndex(Indexer.ATTACHMENT_PUBLISHED_INDEX);
    Pageable pageRequest = new PageRequest(0, 100);
    Page<AttachmentState> attachments;
    do {
        attachments = attachmentStateRepository.findAll(pageRequest);
        attachments.forEach(a -> {
            if (FileUtils.isDirectory(a))
                return;
            indexer.index(Indexer.ATTACHMENT_DRAFT_INDEX, a);
            if (a.getPublishedAttachment() != null) {
                indexer.index(Indexer.ATTACHMENT_PUBLISHED_INDEX, a);
            }
        });
    } while ((pageRequest = attachments.nextPageable()) != null);
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) AttachmentState(org.obiba.mica.file.AttachmentState) Pageable(org.springframework.data.domain.Pageable) Async(org.springframework.scheduling.annotation.Async) Subscribe(com.google.common.eventbus.Subscribe)

Example 8 with AttachmentState

use of org.obiba.mica.file.AttachmentState in project mica2 by obiba.

the class AbstractFileSystemResource method getFileDto.

private Mica.FileDto getFileDto(String basePath) {
    Pair<String, String> pathName = FileSystemService.extractPathName(basePath);
    AttachmentState state = fileSystemService.getAttachmentState(pathName.getKey(), pathName.getValue(), isPublishedFileSystem());
    return dtos.asFileDto(state, isPublishedFileSystem());
}
Also used : AttachmentState(org.obiba.mica.file.AttachmentState)

Example 9 with AttachmentState

use of org.obiba.mica.file.AttachmentState in project mica2 by obiba.

the class AbstractFileSystemResource method doGetAttachment.

protected Attachment doGetAttachment(String path, @Nullable String version, @Nullable String shareKey) {
    String basePath = normalizePath(path);
    if (isPublishedFileSystem())
        subjectAclService.checkAccess("/file", basePath);
    else
        subjectAclService.checkPermission("/draft/file", "VIEW", basePath, shareKey);
    if (path.endsWith("/"))
        throw new IllegalArgumentException("Folder download is not supported");
    Pair<String, String> pathName = FileSystemService.extractPathName(basePath);
    AttachmentState state = fileSystemService.getAttachmentState(pathName.getKey(), pathName.getValue(), isPublishedFileSystem());
    if (isPublishedFileSystem())
        return state.getPublishedAttachment();
    if (Strings.isNullOrEmpty(version))
        return state.getAttachment();
    List<Attachment> attachments = fileSystemService.getAttachmentRevisions(state).stream().filter(a -> a.getId().equals(version)).collect(Collectors.toList());
    if (attachments.isEmpty())
        throw new NoSuchElementException("No file version " + version + " found at path: " + basePath);
    return attachments.get(0);
}
Also used : RevisionStatus(org.obiba.mica.core.domain.RevisionStatus) Attachment(org.obiba.mica.file.Attachment) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Pair(org.apache.commons.math3.util.Pair) AttachmentState(org.obiba.mica.file.AttachmentState) FileUtils(org.obiba.mica.file.FileUtils) FileSystemService(org.obiba.mica.file.service.FileSystemService) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) FileUtils.normalizePath(org.obiba.mica.file.FileUtils.normalizePath) FileUtils.isRoot(org.obiba.mica.file.FileUtils.isRoot) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) List(java.util.List) Lists(com.google.common.collect.Lists) Mica(org.obiba.mica.web.model.Mica) NoSuchElementException(java.util.NoSuchElementException) Dtos(org.obiba.mica.web.model.Dtos) Nullable(javax.annotation.Nullable) AttachmentState(org.obiba.mica.file.AttachmentState) Attachment(org.obiba.mica.file.Attachment) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with AttachmentState

use of org.obiba.mica.file.AttachmentState in project mica2 by obiba.

the class AbstractFileSystemResource method getFolderDto.

private Mica.FileDto getFolderDto(String basePath) {
    AttachmentState state = fileSystemService.getAttachmentState(basePath, FileSystemService.DIR_NAME, isPublishedFileSystem());
    Mica.FileDto.Builder builder = dtos.asFileDto(state, isPublishedFileSystem()).toBuilder();
    // 
    builder.addAllChildren(getChildrenFolders(basePath)).addAllChildren(getChildrenFiles(basePath, false));
    return builder.build();
}
Also used : AttachmentState(org.obiba.mica.file.AttachmentState)

Aggregations

AttachmentState (org.obiba.mica.file.AttachmentState)12 Attachment (org.obiba.mica.file.Attachment)3 ObjectId (org.bson.types.ObjectId)2 RevisionStatus (org.obiba.mica.core.domain.RevisionStatus)2 FileUpdatedEvent (org.obiba.mica.file.event.FileUpdatedEvent)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Subscribe (com.google.common.eventbus.Subscribe)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 Collectors (java.util.stream.Collectors)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Nullable (javax.annotation.Nullable)1 Inject (javax.inject.Inject)1 NotNull (javax.validation.constraints.NotNull)1 Pair (org.apache.commons.math3.util.Pair)1