long chunkOffset = prop.getChunk().getOffset();
if (chunkOffset == 0) {
// first chunk
// check if another chunk upload is already in progress. throw
// exception
NodeIterator itr = res.getNodes(SlingPostConstants.CHUNK_NODE_NAME
+ "*");
if (itr.hasNext()) {
throw new RepositoryException(
"Chunk upload already in progress at {" + res.getPath()
+ "}");
}
res.addMixin(SlingPostConstants.NT_SLING_CHUNK_MIXIN);
changes.add(Modification.onModified(res.setProperty(
SlingPostConstants.NT_SLING_CHUNKS_LENGTH, 0).getPath()));
if (!res.hasProperty(JCR_DATA)) {
// create a empty jcr:data property
res.setProperty(JCR_DATA,
new ByteArrayInputStream("".getBytes()));
}
}
if (!res.hasProperty(SlingPostConstants.NT_SLING_CHUNKS_LENGTH)) {
throw new RepositoryException("no chunk upload found at {"
+ res.getPath() + "}");
}
long currentLength = res.getProperty(
SlingPostConstants.NT_SLING_CHUNKS_LENGTH).getLong();
long totalLength = prop.getChunk().getLength();
if (chunkOffset != currentLength) {
throw new RepositoryException("Chunk's offset {"
+ chunkOffset
+ "} doesn't match expected offset {"
+ res.getProperty(
SlingPostConstants.NT_SLING_CHUNKS_LENGTH).getLong()
+ "}");
}
if (totalLength != 0) {
if (res.hasProperty(SlingPostConstants.NT_SLING_FILE_LENGTH)) {
long expectedLength = res.getProperty(
SlingPostConstants.NT_SLING_FILE_LENGTH).getLong();
if (totalLength != expectedLength) {
throw new RepositoryException("File length {"
+ totalLength + "} doesn't match expected length {"
+ expectedLength + "}");
}
} else {
res.setProperty(SlingPostConstants.NT_SLING_FILE_LENGTH,
totalLength);
}
}
NodeIterator itr = res.getNodes(SlingPostConstants.CHUNK_NODE_NAME
+ "_" + String.valueOf(chunkOffset) + "*");
if (itr.hasNext()) {
throw new RepositoryException("Chunk already present at {"
+ itr.nextNode().getPath() + "}");
}
String nodeName = SlingPostConstants.CHUNK_NODE_NAME + "_"
+ String.valueOf(chunkOffset) + "_"
+ String.valueOf(chunkOffset + value.getSize() - 1);
if (totalLength == (currentLength + value.getSize())
|| prop.getChunk().isCompleted()) {
File file = null;
InputStream fileIns = null;
try {
file = mergeChunks(res, value.getInputStream());
fileIns = new FileInputStream(file);
changes.add(Modification.onModified(res.setProperty(
JCR_DATA, fileIns).getPath()));
NodeIterator nodeItr = res.getNodes(SlingPostConstants.CHUNK_NODE_NAME
+ "*");
while (nodeItr.hasNext()) {
Node nodeRange = nodeItr.nextNode();
changes.add(Modification.onDeleted(nodeRange.getPath()));
nodeRange.remove();
}
if (res.hasProperty(SlingPostConstants.NT_SLING_FILE_LENGTH)) {
javax.jcr.Property expLenProp = res.getProperty(SlingPostConstants.NT_SLING_FILE_LENGTH);