/** * Get the URI where a file should be in Catalog, given a study and a path. * * @param studyId Study identifier * @param path Path to locate * @param directory Boolean indicating if the file is a directory * @return URI where the file should be placed * @throws CatalogException CatalogException */ private URI getFileUri(long studyId, String path, boolean directory) throws CatalogException, URISyntaxException { // Get the closest existing parent. If parents == true, may happen that the parent is not registered in catalog yet. File existingParent = getParents(false, null, path, studyId).first(); //Relative path to the existing parent String relativePath = Paths.get(existingParent.getPath()).relativize(Paths.get(path)).toString(); if (path.endsWith("/") && !relativePath.endsWith("/")) { relativePath += "/"; } String uriStr = Paths.get(existingParent.getUri().getPath()).resolve(relativePath).toString(); if (directory) { return UriUtils.createDirectoryUri(uriStr); } else { return UriUtils.createUri(uriStr); } }
@Deprecated public URI getUri(long studyId, String filePath) throws CatalogException { ParamUtils.checkObj(filePath, "filePath"); List<File> parents = getParents(false, INCLUDE_FILE_URI_PATH, filePath, studyId).getResult(); for (File parent : parents) { if (parent.getUri() != null) { if (parent.isExternal()) { throw new CatalogException("Cannot upload files to an external folder"); } String relativePath = filePath.replaceFirst(parent.getPath(), ""); return Paths.get(parent.getUri()).resolve(relativePath).toUri(); } } URI studyUri = getStudyUri(studyId); return filePath.isEmpty() ? studyUri : catalogIOManagerFactory.get(studyUri).getFileUri(studyUri, filePath); }
@Deprecated public QueryResult<File> getParents(long fileId, QueryOptions options, String sessionId) throws CatalogException { QueryResult<File> fileQueryResult = fileDBAdaptor.get(fileId, new QueryOptions(QueryOptions.INCLUDE, Arrays.asList(FileDBAdaptor.QueryParams.PATH.key(), FileDBAdaptor.QueryParams.STUDY_UID.key()))); if (fileQueryResult.getNumResults() == 0) { return fileQueryResult; } String userId = userManager.getUserId(sessionId); authorizationManager.checkFilePermission(fileQueryResult.first().getStudyUid(), fileId, userId, FileAclEntry.FilePermissions.VIEW); return getParents(true, options, fileQueryResult.first().getPath(), fileQueryResult.first().getStudyUid()); }
QueryResult<File> parentFolders = getParents(false, QueryOptions.empty(), file.getPath(), study.getUid()); if (parentFolders.getNumResults() == 0) {
@Test public void testGetFileParents1() throws CatalogException { long fileId; QueryResult<File> fileParents; fileId = catalogManager.getFileManager().getUid("data/test/folder/", studyFqn, sessionIdUser).getResource().getUid(); fileParents = catalogManager.getFileManager().getParents(fileId, null, sessionIdUser); assertEquals(4, fileParents.getNumResults()); assertEquals("", fileParents.getResult().get(0).getPath()); assertEquals("data/", fileParents.getResult().get(1).getPath()); assertEquals("data/test/", fileParents.getResult().get(2).getPath()); assertEquals("data/test/folder/", fileParents.getResult().get(3).getPath()); }
@Test public void testGetFileParents2() throws CatalogException { long fileId; QueryResult<File> fileParents; fileId = catalogManager.getFileManager().getUid("data/test/folder/test_1K.txt.gz", studyFqn, sessionIdUser) .getResource().getUid(); fileParents = catalogManager.getFileManager().getParents(fileId, null, sessionIdUser); assertEquals(5, fileParents.getNumResults()); assertEquals("", fileParents.getResult().get(0).getPath()); assertEquals("data/", fileParents.getResult().get(1).getPath()); assertEquals("data/test/", fileParents.getResult().get(2).getPath()); assertEquals("data/test/folder/", fileParents.getResult().get(3).getPath()); assertEquals("data/test/folder/test_1K.txt.gz", fileParents.getResult().get(4).getPath()); }
@Test public void testGetFileParents3() throws CatalogException { long fileId; QueryResult<File> fileParents; fileId = catalogManager.getFileManager().getUid("data/test/", studyFqn, sessionIdUser).getResource().getUid(); fileParents = catalogManager.getFileManager().getParents(fileId, new QueryOptions("include", "projects.studies.files.path," + "projects.studies.files.id"), sessionIdUser); assertEquals(3, fileParents.getNumResults()); assertEquals("", fileParents.getResult().get(0).getPath()); assertEquals("data/", fileParents.getResult().get(1).getPath()); assertEquals("data/test/", fileParents.getResult().get(2).getPath()); fileParents.getResult().forEach(f -> { assertNull(f.getName()); assertNotNull(f.getPath()); assertTrue(f.getUid() != 0); }); }