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;
}
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);
}
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());
}
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);
}
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();
}
Aggregations