@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); } }