private void loadPart(final int blockIndex) { int size = blockSize; int fileOffset = blockIndex * blockSize; if ((blockIndex + 1) * blockSize > srcReference.getSize()) { size = srcReference.getSize() - blockIndex * blockSize; } // TODO: Validate file part load ordering inputFile .read(fileOffset, size) .then( filePart -> { if (isCompleted) { return; } if (LOG) { Log.d(TAG, "Block #" + blockIndex + " read"); } if (isWriteToDestProvider) { if (!outputFile.write( fileOffset, filePart.getContents(), 0, filePart.getPartLength())) { if (LOG) { Log.w(TAG, "write #" + blockIndex + " error"); } reportError(); return; } } crc32.update(filePart.getContents(), 0, filePart.getPartLength()); if (LOG) { Log.d(TAG, "Starting block upload #" + blockIndex); } uploadCount++; uploadPart(blockIndex, filePart.getContents(), 0); checkQueue(); }) .failure( e -> { if (isCompleted) { return; } if (LOG) { Log.w(TAG, "Block #" + blockIndex + " read failure"); } reportError(); }); }
@Override public void preStart() { alreadyInTemp = false; // Storage.isAlreadyInTemp(descriptor); isWriteToDestProvider = Storage.isFsPersistent() && !alreadyInTemp; srcReference = Storage.fileFromDescriptor(descriptor); if (srcReference == null) { if (LOG) { Log.d(TAG, "Error during file reference creating"); } reportError(); return; } if (isWriteToDestProvider) { destReference = Storage.createTempFile(); if (destReference == null) { if (LOG) { Log.w(TAG, "Error during file dest reference creating"); } reportError(); return; } } srcReference .openRead() .flatMap( f -> { inputFile = f; if (isWriteToDestProvider) { return destReference.openWrite(srcReference.getSize()); } else { return Promise.success(null); } }) .flatMap( f -> { outputFile = f; crc32 = new CRC32(); blocksCount = srcReference.getSize() / blockSize; if (srcReference.getSize() % blockSize != 0) { blocksCount++; } if (LOG) { Log.d(TAG, "Starting uploading " + blocksCount + " blocks"); Log.d(TAG, "Requesting upload config..."); } return api(new RequestGetFileUploadUrl(srcReference.getSize())); }) .then( r -> { if (LOG) { Log.d(TAG, "Upload config loaded"); } uploadConfig = r.getUploadKey(); checkQueue(); }) .failure( e -> { if (LOG) { Log.w(TAG, "Error during initialization of upload"); } reportError(); }); }