@Override public InputStream getContent() { if (inputStream == null) { try { inputStream = new FileInputStream(file); } catch (FileNotFoundException e) { throw new ContentStorageException("Error while opening file stream", e); } } return inputStream; }
@Override public void deleteContentObject(String id) { try { File contentFile = getFileForId(id, true); contentFile.delete(); } catch (Exception e) { throw new ContentStorageException("Error while deleting content", e); } }
@Override public void deleteContentObject(String id) { try { File contentFile = getContentFile(id); File parentFile = contentFile.getParentFile(); contentFile.delete(); if (parentFile.listFiles().length == 0 && !id.startsWith(UNCATEGORIZED_PREFIX)) { parentFile.delete(); } } catch (Exception e) { throw new ContentStorageException("Error while deleting content", e); } }
public BigInteger getIdForPath(File path) { BigInteger result = BigInteger.ZERO; BigInteger currentFactor = BigInteger.ONE; int depth = 0; File parent = path; while (parent != null && depth < iterationDepth) { try { result = result.add(new BigInteger(parent.getName()).multiply(currentFactor)); // Move on to next iteration parent = parent.getParentFile(); depth++; currentFactor = currentFactor.multiply(blockSize); } catch (NumberFormatException nfe) { throw new ContentStorageException("Illegal format of path segment: " + parent.getName(), nfe); } } return result; }
/** * @return a path representing the content with the given unique content index. */ public File getPathForId(BigInteger id) { BigInteger remainder = null; if (id.compareTo(BigInteger.ZERO) < 0) { throw new ContentStorageException("ID cannot be negative"); } Long[] blocks = new Long[iterationDepth]; for (int i = 0; i < iterationDepth; i++) { remainder = id.remainder(blockSize); blocks[i] = remainder.longValue(); id = id.subtract(remainder).divide(blockSize); } if (!id.equals(BigInteger.ZERO)) { throw new ContentStorageException("ID out of range of content storage limit: " + blockSize.pow(iterationDepth).subtract(BigInteger.ONE)); } StringBuffer buffer = new StringBuffer(); for (int i = iterationDepth - 1; i >= 0; i--) { buffer.append(blocks[i].toString()).append(File.separatorChar); } return new File(buffer.toString()); }
@Override public ContentObject createContentObject(InputStream contentStream, Map<String, Object> metaData) { // Get hold of the next free ID to use BigInteger id = fetchNewId(); File contentFile = new File(rootFolder, converter.getPathForId(id).getPath()); long length = -1; try (FileOutputStream fos = new FileOutputStream(contentFile, false)) { length = IOUtils.copy(contentStream, fos); } catch (FileNotFoundException e) { throw new ContentStorageException("Content file was deleted or no longer accessible prior to writing: " + contentFile, e); } catch (IOException e) { throw new ContentStorageException("Error while writing content to file: " + contentFile, e); } if (length < 0) { // The file was larger than 2GB (max integer value), we need to get the // length from the file instead length = contentFile.length(); } return new FileSystemContentObject(contentFile, id.toString(), length); }
if (!tempContentFile.createNewFile()) { throw new ContentStorageException("Cannot update content with id: " + id + ", being updated by another user"); throw new ContentStorageException("Error while renaming new content file, content not updated"); throw new ContentStorageException("Error while renaming existing content file, content not updated"); throw new ContentStorageException("Error while updating content with id: " + id, ioe);
if (!tempContentFile.createNewFile()) { throw new ContentStorageException("Cannot update content with id: " + id + ", being updated by another user"); throw new ContentStorageException("Error while renaming new content file, content not updated"); throw new ContentStorageException("Error while renaming existing content file, content not updated"); throw new ContentStorageException("Error while updating content with id: " + id, ioe);
@Override public ContentObject createContentObject(InputStream contentStream, Map<String, Object> metaData) { String uuid = UUID_GENERATOR.generate().toString(); File file = getContentFile(metaData, uuid); try (FileOutputStream fos = new FileOutputStream(file, false)) { long length = IOUtils.copy(contentStream, fos); String contentId = generateContentId(uuid, metaData); return new FileSystemContentObject(file, contentId, length); } catch (IOException e) { throw new ContentStorageException("Could not write content to " + file.getAbsolutePath(), e); } }
throw new ContentStorageException("Content already stored at that location, shouldn't have happened: " + contentFile); throw new ContentStorageException("Unable to create content file: " + contentFile); throw new ContentStorageException("Error while creating content file: " + contentFile, ioe);
/** * @return a file reference for the given id, checking for existence based on the given flag. */ protected File getFileForId(String id, boolean shouldExist) { BigInteger idValue = null; try { idValue = new BigInteger(id); } catch (NumberFormatException nfe) { throw new ContentStorageException("Illegal ID value, only positive numbers are supported: " + id, nfe); } File path = converter.getPathForId(idValue); File file = new File(rootFolder, path.getPath()); if (shouldExist != file.exists()) { if (shouldExist) { throw new ContentNotFoundException("Content with id: " + id + " was not found (path: " + file.toString() + ")"); } else { throw new ContentNotFoundException("Content with id: " + id + " already exists."); } } return file; }
throw new ContentStorageException("Took too many attempts to get an available free folder"); throw new ContentStorageException("Maximum number of content items reached, cannot store any more content");