From 17db8fc00f4ec44e1a40e3743648721af5d37986 Mon Sep 17 00:00:00 2001 From: Serhii Plyhun Date: Wed, 3 Jan 2024 09:25:12 +0100 Subject: [PATCH] Revert "Parallel upload fix" This reverts commit adae05ea76c8448264711fbde2a2d9f589ed3a42. --- .../java/com/gentics/mesh/util/FileUtils.java | 17 ------------- .../node/BinaryUploadHandlerImpl.java | 13 +++------- .../mesh/storage/LocalBinaryStorageImpl.java | 25 +++---------------- ...yFieldUploadEndpointParameterizedTest.java | 4 +-- 4 files changed, 8 insertions(+), 51 deletions(-) diff --git a/common/src/main/java/com/gentics/mesh/util/FileUtils.java b/common/src/main/java/com/gentics/mesh/util/FileUtils.java index ec4d6f0e5a..efa5510a8d 100644 --- a/common/src/main/java/com/gentics/mesh/util/FileUtils.java +++ b/common/src/main/java/com/gentics/mesh/util/FileUtils.java @@ -42,23 +42,6 @@ public static Single hash(Flowable stream) { } } - /** - * Generate a SHA 512 checksum from the data in the given buffer and synchronously return the hex encoded hash as a string. - * - * @param buffer - * @return Observable returning the SHA 512 checksum - */ - public static String hashSync(byte[] bytes) { - try { - MessageDigest md = MessageDigest.getInstance("SHA-512"); - md.update(bytes); - return bytesToHex(md.digest()); - } catch (Exception e) { - log.error("Error while hashing data", e); - throw error(INTERNAL_SERVER_ERROR, "generic_error", e); - } - } - /** * Generate a SHA 512 checksum from the data in the given buffer and asynchronously return the hex encoded hash as a string. * diff --git a/core/src/main/java/com/gentics/mesh/core/endpoint/node/BinaryUploadHandlerImpl.java b/core/src/main/java/com/gentics/mesh/core/endpoint/node/BinaryUploadHandlerImpl.java index f72df7b89b..715dfe6b59 100644 --- a/core/src/main/java/com/gentics/mesh/core/endpoint/node/BinaryUploadHandlerImpl.java +++ b/core/src/main/java/com/gentics/mesh/core/endpoint/node/BinaryUploadHandlerImpl.java @@ -9,8 +9,6 @@ import static org.apache.commons.lang3.StringUtils.isEmpty; import java.io.File; -import java.nio.file.FileAlreadyExistsException; -import java.nio.file.NoSuchFileException; import java.util.Arrays; import java.util.List; import java.util.Set; @@ -216,11 +214,9 @@ public void handleUpdateField(InternalActionContext ac, String nodeUuid, String if (log.isDebugEnabled()) { log.debug("Error detected. Purging previously stored upload for tempId {}", tmpId, e); } - return binaryStorage.purgeTemporaryUpload(tmpId) - .onErrorComplete(e1 -> e1 instanceof NoSuchFileException || (e.getCause() instanceof NoSuchFileException)) - .doOnError(e1 -> { - log.error("Error while purging temporary upload for tempId {}", tmpId, e1); - }).andThen(Single.error(e)); + return binaryStorage.purgeTemporaryUpload(tmpId).doOnError(e1 -> { + log.error("Error while purging temporary upload for tempId {}", tmpId, e1); + }).onErrorComplete().andThen(Single.error(e)); } else { return Single.error(e); } @@ -250,7 +246,6 @@ private Completable storeUploadInTemp(UploadContext ctx, FileUpload ul, String h } else { // File has already been stored. Lets remove the upload from the vert.x tmpdir. We no longer need it. return fs.rxDelete(uploadFilePath) - .onErrorComplete(e1 -> e1 instanceof NoSuchFileException || (e1.getCause() instanceof NoSuchFileException)) .doOnComplete(() -> { if (log.isTraceEnabled()) { log.trace("Removed temporary file {}", uploadFilePath); @@ -258,7 +253,7 @@ private Completable storeUploadInTemp(UploadContext ctx, FileUpload ul, String h }) .doOnError(e -> { log.warn("Failed to remove upload from tmpDir {}", uploadFilePath, e); - }); + }).onErrorComplete(); } } diff --git a/services/local-storage/src/main/java/com/gentics/mesh/storage/LocalBinaryStorageImpl.java b/services/local-storage/src/main/java/com/gentics/mesh/storage/LocalBinaryStorageImpl.java index 975828320b..aa89d2f8fd 100644 --- a/services/local-storage/src/main/java/com/gentics/mesh/storage/LocalBinaryStorageImpl.java +++ b/services/local-storage/src/main/java/com/gentics/mesh/storage/LocalBinaryStorageImpl.java @@ -4,7 +4,6 @@ import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.FileAlreadyExistsException; @@ -16,19 +15,15 @@ import javax.inject.Inject; import javax.inject.Singleton; -import org.apache.commons.lang.StringUtils; - import com.gentics.mesh.core.data.node.field.HibBinaryField; import com.gentics.mesh.core.data.storage.AbstractBinaryStorage; import com.gentics.mesh.core.data.storage.LocalBinaryStorage; import com.gentics.mesh.etc.config.MeshOptions; import com.gentics.mesh.etc.config.MeshUploadOptions; -import com.gentics.mesh.util.FileUtils; import com.gentics.mesh.util.RxUtil; import io.reactivex.Completable; import io.reactivex.Flowable; -import io.reactivex.Single; import io.vertx.core.buffer.Buffer; import io.vertx.core.file.OpenOptions; import io.vertx.core.logging.Logger; @@ -68,23 +63,9 @@ public Completable moveInPlace(String uuid, String temporaryId) { } File uploadFolder = new File(options.getDirectory(), getSegmentedPath(uuid)); return createParentPath(uploadFolder.getAbsolutePath()) - .andThen(fileSystem.rxMove(source, target) - .onErrorComplete(e -> { - if (e instanceof FileAlreadyExistsException || (e.getCause() instanceof FileAlreadyExistsException)) { - try (FileInputStream sourceFis = new FileInputStream(new File(source)); FileInputStream targetFis = new FileInputStream(new File(target))) { - String sourceHash = FileUtils.hashSync(sourceFis.readAllBytes()); - String targetHash = FileUtils.hashSync(targetFis.readAllBytes()); - boolean match = sourceHash.equals(targetHash); - log.warn("The file [{}] exists at the target [{}]. The hashes {}match. {}.", match ? StringUtils.EMPTY : "don't ", match ? "Suppressing the error" : "Aborting the upload"); - return match; - } - } else { - return false; - } - }) - .doOnError(e -> { - log.error("Error while moving binary from temp upload dir {} to final dir {}", source, target); - })); + .andThen(fileSystem.rxMove(source, target).doOnError(e -> { + log.error("Error while moving binary from temp upload dir {} to final dir {}", source, target); + })); }); } diff --git a/tests/tests-core/src/main/java/com/gentics/mesh/core/field/binary/AbstractBinaryFieldUploadEndpointParameterizedTest.java b/tests/tests-core/src/main/java/com/gentics/mesh/core/field/binary/AbstractBinaryFieldUploadEndpointParameterizedTest.java index 4fc3229ffc..89b8b3776e 100644 --- a/tests/tests-core/src/main/java/com/gentics/mesh/core/field/binary/AbstractBinaryFieldUploadEndpointParameterizedTest.java +++ b/tests/tests-core/src/main/java/com/gentics/mesh/core/field/binary/AbstractBinaryFieldUploadEndpointParameterizedTest.java @@ -81,8 +81,6 @@ protected void testParallelUpload(boolean useSameName) throws IOException { initialSyncWrites = initialSyncWrites.or(() -> Optional.of(mesh().globalLock().isSyncWrites())); getTestContext().getInstanceProvider().setSyncWrites(isSyncWrites()); - String defaultFilename = Long.toHexString(System.currentTimeMillis()) + "blume.jpg"; - Observable.range(0, numUploads).flatMapSingle(number -> { NodeCreateRequest request = new NodeCreateRequest(); request.setLanguage("en"); @@ -95,7 +93,7 @@ protected void testParallelUpload(boolean useSameName) throws IOException { int size = data.length; InputStream ins = new ByteArrayInputStream(data); return client() - .updateNodeBinaryField(PROJECT_NAME, node.getUuid(), "en", node.getVersion(), "image", ins, size, useSameName ? defaultFilename : (number + defaultFilename), "image/jpeg") + .updateNodeBinaryField(projectName(), node.getUuid(), "en", node.getVersion(), "image", ins, size, useSameName ? "blume.jpg" : (number + "blume.jpg"), "image/jpeg") .toSingle(); }); }).lastOrError().ignoreElement().blockingAwait();