public TransferredResource createFile(String parentUUID, String fileName, InputStream inputStream) throws GenericException, NotFoundException, AlreadyExistsException, AuthorizationDeniedException { Path parentPath; RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); if (StringUtils.isNotBlank(parentUUID)) { TransferredResource parent = index.retrieve(TransferredResource.class, parentUUID, fieldsToReturn); parentPath = basePath.resolve(parent.getRelativePath()); } else { parentPath = basePath; } Path file = parentPath.resolve(fileName); try { try { Files.createDirectories(parentPath); } catch (FileAlreadyExistsException e) { // do nothing and carry on } Files.copy(inputStream, file); BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class); TransferredResource resource = createTransferredResource(file, attrs, attrs.size(), basePath, new Date()); index.create(TransferredResource.class, resource); return resource; } catch (FileAlreadyExistsException e) { LOGGER.error("Cannot create file", e); throw new AlreadyExistsException(file.toString()); } catch (IOException e) { LOGGER.error("Cannot create file", e); throw new GenericException("Cannot create file", e); } }
@Override public Directory createDirectory(StoragePath storagePath) throws AlreadyExistsException, GenericException { Path dirPath = FSUtils.getEntityPath(basePath, storagePath); Path directory = null; if (FSUtils.exists(dirPath)) { throw new AlreadyExistsException("Could not create directory at " + dirPath); } try { directory = Files.createDirectories(dirPath); return new DefaultDirectory(storagePath); } catch (IOException e) { // cleanup try { FSUtils.deletePath(directory); } catch (NotFoundException | GenericException e1) { LOGGER.warn("Error while cleaning up", e1); } throw new GenericException("Could not create directory at " + dirPath, e); } }
@Override public Container createContainer(StoragePath storagePath) throws GenericException, AlreadyExistsException { Path containerPath = FSUtils.getEntityPath(basePath, storagePath); Path directory = null; try { directory = Files.createDirectory(containerPath); return new DefaultContainer(storagePath); } catch (FileAlreadyExistsException e) { // cleanup try { FSUtils.deletePath(directory); } catch (NotFoundException e1) { LOGGER.warn("Error while trying to clean up", e1); } throw new AlreadyExistsException("Could not create container at " + containerPath, e); } catch (IOException e) { // cleanup try { FSUtils.deletePath(directory); } catch (NotFoundException e1) { LOGGER.warn("Error while trying to clean up", e1); } throw new GenericException("Could not create container at " + containerPath, e); } }
Path binPath = FSUtils.getEntityPath(basePath, storagePath); if (FSUtils.exists(binPath)) { throw new AlreadyExistsException("Binary already exists: " + binPath); } else { try { throw new AlreadyExistsException("Binary already exists: " + binPath); } catch (IOException e) { throw new GenericException("Could not create binary", e);
throw new AlreadyExistsException("Cannot copy because target path already exists: " + targetPath);
throw new AlreadyExistsException("Cannot copy because target path already exists: " + targetPath);
/*****************************************************/ public void importLogEntries(InputStream inputStream, String filename) throws AuthorizationDeniedException, GenericException, AlreadyExistsException, RequestNotValidException, NotFoundException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); StoragePath logPath = ModelUtils.getLogStoragePath(filename); if (storage.exists(logPath)) { throw new AlreadyExistsException("Binary already exists: " + logPath); } Path tempDir = null; try { tempDir = Files.createTempDirectory(new Date().getTime() + ""); Path path = tempDir.resolve(filename); IOUtils.copyLarge(inputStream, Files.newOutputStream(path)); for (OptionalWithCause<LogEntry> optionalLogEntry : new LogEntryFileSystemIterable(tempDir)) { // index if (optionalLogEntry.isPresent()) { notifyLogEntryCreated(optionalLogEntry.get()).failOnError(); } } // store storage.createBinary(logPath, new FSPathContentPayload(path), false); } catch (IOException e) { throw new GenericException(e); } finally { FSUtils.deletePathQuietly(tempDir); } }
@Override public Directory createRandomDirectory(StoragePath parentStoragePath) throws RequestNotValidException, GenericException, NotFoundException, AlreadyExistsException { Path parentDirPath = FSUtils.getEntityPath(basePath, parentStoragePath); Path directory = null; try { directory = FSUtils.createRandomDirectory(parentDirPath); return new DefaultDirectory(FSUtils.getStoragePath(basePath, directory)); } catch (FileAlreadyExistsException e) { // cleanup FSUtils.deletePath(directory); throw new AlreadyExistsException("Could not create random directory under " + parentDirPath, e); } catch (IOException e) { // cleanup FSUtils.deletePath(directory); throw new GenericException("Could not create random directory under " + parentDirPath, e); } }