@Override public boolean flagAsExisting(GenericId id) { Path path = this.id2AbsolutePath(id); try { FileUtils.createDirectory(path); } catch (IOException e) { FilebasedRepository.LOGGER.debug(e.toString()); return false; } return true; }
/** * Creates the given directory including its parent directories, if they do * not exist. */ public static void createDirectory(Path path) throws IOException { Path parent = path.getParent(); if (parent == null) { throw new IOException("No parent found"); } if (!Files.exists(parent)) { FileUtils.createDirectory(parent); } if (!Files.exists(path)) { Files.createDirectory(path); } }
@Override public void doImport(InputStream in) { ZipInputStream zis = new ZipInputStream(in); ZipEntry entry; try { while ((entry = zis.getNextEntry()) != null) { if (!entry.isDirectory()) { Path path = this.repositoryRoot.resolve(entry.getName()); try { FileUtils.createDirectory(path.getParent()); try { Files.copy(zis, path, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { FilebasedRepository.LOGGER.error("Files.copy did not succeed", e); } } catch (IOException e) { FilebasedRepository.LOGGER.error("Directory creation did not succeed", e); } } } } catch (IOException e) { FilebasedRepository.LOGGER.error("Unzipping did not succeed", e); } }
@Override public void putContentToFile(RepositoryFileReference ref, String content, MediaType mediaType) throws IOException { if (mediaType == null) { // quick hack for storing mime type called this method assert (ref.getFileName().endsWith(Constants.SUFFIX_MIMETYPE)); // we do not need to store the mime type of the file containing the mime type information } else { this.setMimeType(ref, mediaType); } Path path = this.ref2AbsolutePath(ref); FileUtils.createDirectory(path.getParent()); Files.write(path, content.getBytes()); }
@Override public void putContentToFile(RepositoryFileReference ref, InputStream inputStream, MediaType mediaType) throws IOException { if (mediaType == null) { // quick hack for storing mime type called this method assert (ref.getFileName().endsWith(Constants.SUFFIX_MIMETYPE)); // we do not need to store the mime type of the file containing the mime type information } else { this.setMimeType(ref, mediaType); } Path targetPath = this.ref2AbsolutePath(ref); // ensure that parent directory exists FileUtils.createDirectory(targetPath.getParent()); try { Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING); } catch (IllegalStateException e) { FilebasedRepository.LOGGER.debug("Guessing that stream with length 0 is to be written to a file", e); // copy throws an "java.lang.IllegalStateException: Stream already closed" if the InputStream contains 0 bytes // For instance, this case happens if SugarCE-6.4.2.zip.removed is tried to be uploaded // We work around the Java7 issue and create an empty file if (Files.exists(targetPath)) { // semantics of putContentToFile: existing content is replaced without notification Files.delete(targetPath); } Files.createFile(targetPath); } }